Working with Text Document

In this article, we will learn what options can be useful for working with a text document via Aspose.Words. Please note that this is not a complete list of available options, but only an example of working with some of them.

Add Bi-Directional Marks

You can use the AddBidiMarks property to specify whether to add bi-directional marks before each BiDi run when exporting in plain text format. Aspose.Words inserts Unicode Character ‘RIGHT-TO-LEFT MARK’ (U+200F) before each bi-directional Run in text. This option corresponds to “Add bi-directional marks” option in the MS Word File Conversion dialog when you export to a Plain Text format. Note that it appears in dialog only if any of the Arabic or Hebrew editing languages are added in MS Word.

The following code example shows how to use TxtSaveOptions.AddBidiMarks property. The default value of this property is true:

Recognize List Items During Loading TXT

Aspose.Words can import list item of a text file as list numbers or plain text in its document object model. The DetectNumberingWithWhitespaces property allows to specify how numbered list items are recognized when a document is imported from plain text format:

  • If this option is set to true, whitespaces are also used as list number delimiters: list recognition algorithm for Arabic style numbering (1., 1.1.2.) uses both whitespaces and dot (".") symbols.
  • If this option is set to false, the lists recognition algorithm detects list paragraphs, when list numbers ends with either dot, right bracket or bullet symbols (such as “•”, “*”, “-” or “o”).

The following code example shows how to use this property:

Handle Leading and Trailing Spaces during Loading TXT

You can control the way of handling leading and trailing spaces during loading TXT files. The leading spaces could be trimmed, preserved or converted to indent and trailing spaces could be trimmed or preserved.

The code example given below shows how to trim leading and trailing spaces while importing the TXT file:

Detect Document Text Direction

Aspose.Words provides DocumentDirection property in TxtLoadOptions class to detect the text direction (RTL / LTR) in the document. This property sets or gets document text directions provided in DocumentDirection enumeration. The default value is left to right.

The following code example shows how to detect the text direction of the document while importing the TXT file:

If you want to export the header and footer in the output TXT document, you can use the ExportHeadersFootersMode property. This property specifies the way headers and footers are exported to the plain text format.

The following code example shows how to export headers and footers to plain text format:

Export List Indentation in Output TXT

Aspose.Words introduced TxtListIndentation class that allows specifying how list levels are indented while exporting to a plain text format. While working with TxtSaveOption, the ListIndentation property is provided to specify the character to be used for indenting list levels and count specifying how many characters to use as indentation per one list level.

The default value for character property is ‘\0’ indicating that there is no indentation. For count property, the default value is 0 which means no indentation.

Using Tab Character

The following code example shows how to export list levels using tab characters:

Using Space Character

The following code example shows how to export list levels using space characters:

Using Default Indentation

The following code example shows how to export list levels using default indentation:


FAQ

  1. Q: How can I include headers and footers when saving a document as plain text?
    A: Set the ExportHeadersFootersMode property of TxtSaveOptions to the desired mode (e.g., ExportHeadersFootersMode.All). This tells Aspose.Words to write header and footer text into the resulting TXT file.

  2. Q: How do I control list indentation in the exported TXT file?
    A: Use the ListIndentation property of TxtSaveOptions. You can specify a character (tab, space, or custom) and the number of such characters per list level to achieve the required indentation.

  3. Q: How can I preserve leading and trailing spaces when loading a TXT file?
    A: Configure TxtLoadOptions with the appropriate LeadingSpaces and TrailingSpaces settings (e.g., LeadingSpaces.Preserve and TrailingSpaces.Preserve). This prevents Aspose.Words from trimming or converting those spaces.

  4. Q: What does the AddBidiMarks option do and when should I use it?
    A: When AddBidiMarks is true, Aspose.Words inserts a RIGHT‑TO‑LEFT MARK (U+200F) before each bi‑directional run in the exported text. Enable it when the document contains Arabic or Hebrew text and you need the correct visual order in plain‑text files.

  5. Q: How can I detect the text direction (RTL/LTR) of a TXT document during import?
    A: Use the DocumentDirection property of TxtLoadOptions. After loading, the property reflects the detected direction, allowing you to handle right‑to‑left or left‑to‑right content appropriately.