Load TXT document with options

GroupDocs.Conversion provides the TxtLoadOptions class to give you better control over how the source text document will be processed. The following options could be set: 

  • setDetectNumberingWithWhitespaces allows specifying how numbered list items are recognized when a plain-text document is converted. If this option is set to false, the lists recognition algorithm detects list paragraphs, when list numbers end with either dot, right bracket or bullet symbols (such as “•”, “*”, “-” or “o”). If this option is set to true, the white spaces are also used as list number delimiters: the list recognition algorithm for Arabic style numbering (1., 1.1.2.) uses both white spaces and dot (".") symbols.
  • setLeadingSpacesOptions specifies how leading spaces will be processed. The available options are: ConvertToIdent, Preserve, Trim.
  • setTrailingSpacesOptions specifies how trailing spaces will be processed. The available options are: Preserve, Trim.
  • setEncoding specifies the encoding to be used to load the document.

Control behavior of processing leading spaces

The following code snippet shows how to convert a TXT document and control the way the leading spaces are processed:

import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.options.convert.PdfConvertOptions;
import com.groupdocs.conversion.options.load.TxtLeadingSpacesOptions;
import com.groupdocs.conversion.options.load.TxtLoadOptions;
...
TxtLoadOptions loadOptions =  new TxtLoadOptions();
loadOptions.setLeadingSpacesOptions(TxtLeadingSpacesOptions.ConvertToIndent);
loadOptions.setDetectNumberingWithWhitespaces(true);

Converter converter = new Converter("sample.txt", loadOptions);
PdfConvertOptions options = new PdfConvertOptions();
converter.convert("converted.pdf", options);

Control behavior of processing trailing spaces

The following code snippet shows how to convert a TXT document and the way the trailing spaces are processed:

import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.options.convert.PdfConvertOptions;
import com.groupdocs.conversion.options.load.TxtLoadOptions;
import com.groupdocs.conversion.options.load.TxtTrailingSpacesOptions;
...
TxtLoadOptions loadOptions =  new TxtLoadOptions();
loadOptions.setTrailingSpacesOptions(TxtTrailingSpacesOptions.Trim);

Converter converter = new Converter("sample.txt", loadOptions);
PdfConvertOptions options = new PdfConvertOptions();
converter.convert("converted.pdf", options);

Specify encoding

The following code snippet shows how to convert a TXT document and specify the encoding:

import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.options.convert.PdfConvertOptions;
import com.groupdocs.conversion.options.load.TxtLoadOptions;
import java.nio.charset.Charset;
...
TxtLoadOptions loadOptions =  new TxtLoadOptions();
loadOptions.setEncoding(Charset.forName("shift_jis"));

Converter converter = new Converter("sample.txt", loadOptions);
PdfConvertOptions options = new PdfConvertOptions();
converter.convert("converted.pdf", options);