com.aspose.words
Class TxtLoadOptions

java.lang.Object
  extended by LoadOptions
      extended by com.aspose.words.TxtLoadOptions

public class TxtLoadOptions 
extends LoadOptions

Allows to specify additional options when loading LoadFormat.TEXT document into a Document object.

Constructor Summary
TxtLoadOptions()
           Initializes a new instance of this class with default values.
 
Property Getters/Setters Summary
booleangetAnnotationsAtBlockLevel()→ inherited from LoadOptions
voidsetAnnotationsAtBlockLevel(boolean value)
           Gets or sets a flag indicating whether cross structure annotation nodes can be added at block/cell/row level.
java.lang.StringgetBaseUri()→ inherited from LoadOptions
voidsetBaseUri(java.lang.String value)
           Gets or sets the string that will be used to resolve relative URIs found in the document into absolute URIs when required. Can be null or empty string. Default is null.
booleangetConvertShapeToOfficeMath()→ inherited from LoadOptions
voidsetConvertShapeToOfficeMath(boolean value)
           Gets or sets whether to convert shapes with EquationXML to Office Math objects.
booleangetDetectNumberingWithWhitespaces()
voidsetDetectNumberingWithWhitespaces(boolean value)
           Allows to specify how numbered list items are recognized when document is imported from plain text format. The default value is true.
java.nio.charset.CharsetgetEncoding()→ inherited from LoadOptions
voidsetEncoding(java.nio.charset.Charset value)
           Gets or sets the encoding that will be used to load an HTML or TXT document if the encoding is not specified in HTML/TXT. Can be null. Default is null.
FontSettingsgetFontSettings()→ inherited from LoadOptions
voidsetFontSettings(FontSettings value)
           Allows to specify document font settings.
LanguagePreferencesgetLanguagePreferences()→ inherited from LoadOptions
           Gets language preferences that will be used when document is loading.
intgetLeadingSpacesOptions()
voidsetLeadingSpacesOptions(int value)
           Gets or sets preferred option of a leading space handling. Default value is TxtLeadingSpacesOptions.CONVERT_TO_INDENT. The value of the property is TxtLeadingSpacesOptions integer constant.
intgetLoadFormat()→ inherited from LoadOptions
voidsetLoadFormat(int value)
           Specifies the format of the document to be loaded. Default is LoadFormat.AUTO. The value of the property is LoadFormat integer constant.
intgetMswVersion()→ inherited from LoadOptions
voidsetMswVersion(int value)
           Allows to specify that the document loading process should match a specific MS Word version. Default value is MsWordVersion.WORD_2007The value of the property is MsWordVersion integer constant.
java.lang.StringgetPassword()→ inherited from LoadOptions
voidsetPassword(java.lang.String value)
           Gets or sets the password for opening an encrypted document. Can be null or empty string. Default is null.
booleangetPreserveIncludePictureField()→ inherited from LoadOptions
voidsetPreserveIncludePictureField(boolean value)
           Gets or sets whether to preserve the INCLUDEPICTURE field when reading Microsoft Word formats. The default value is false.
IResourceLoadingCallbackgetResourceLoadingCallback()→ inherited from LoadOptions
voidsetResourceLoadingCallback(IResourceLoadingCallback value)
           Allows to control how external resources (images, style sheets) are loaded when a document is imported from HTML, MHTML.
intgetTrailingSpacesOptions()
voidsetTrailingSpacesOptions(int value)
           Gets or sets preferred option of a trailing space handling. Default value is TxtTrailingSpacesOptions.TRIM. The value of the property is TxtTrailingSpacesOptions integer constant.
booleangetUpdateDirtyFields()→ inherited from LoadOptions
voidsetUpdateDirtyFields(boolean value)
           Specifies whether to update the fields with the dirty attribute.
IWarningCallbackgetWarningCallback()→ inherited from LoadOptions
voidsetWarningCallback(IWarningCallback value)
           Called during a load operation, when an issue is detected that might result in data or formatting fidelity loss.
 

Constructor Detail

TxtLoadOptions

public TxtLoadOptions()
Initializes a new instance of this class with default values.

Property Getters/Setters Detail

getAnnotationsAtBlockLevel/setAnnotationsAtBlockLevel

→ inherited from LoadOptions
public boolean getAnnotationsAtBlockLevel() / public void setAnnotationsAtBlockLevel(boolean value)
Gets or sets a flag indicating whether cross structure annotation nodes can be added at block/cell/row level. Currently only bookmarks are affected by this option.

Example:

Shows how to place bookmark nodes on the block, cell and row levels.
// Any LoadOptions instances we create will have a default AnnotationsAtBlockLevel value equal to this
LoadOptions.setAnnotationsAtBlockLevelAsDefault(false);

LoadOptions loadOptions = new LoadOptions();
Assert.assertEquals(loadOptions.getAnnotationsAtBlockLevel(), LoadOptions.getAnnotationsAtBlockLevelAsDefault());

// If we want to work with annotations that transcend structures like tables, we will need to set this to true
loadOptions.setAnnotationsAtBlockLevel(true);

// Open a document with a structured document tag and get that tag
Document doc = new Document(getMyDir() + "Document.AnnotationsAtBlockLevel.docx", loadOptions);
DocumentBuilder builder = new DocumentBuilder(doc);

StructuredDocumentTag sdt = (StructuredDocumentTag) doc.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true).get(1);

// Insert a bookmark and make it envelop our tag
BookmarkStart start = builder.startBookmark("MyBookmark");
BookmarkEnd end = builder.endBookmark("MyBookmark");

sdt.getParentNode().insertBefore(start, sdt);
sdt.getParentNode().insertAfter(end, sdt);

doc.save(getArtifactsDir() + "Document.AnnotationsAtBlockLevel.docx", SaveFormat.DOCX);

getBaseUri/setBaseUri

→ inherited from LoadOptions
public java.lang.String getBaseUri() / public void setBaseUri(java.lang.String value)
Gets or sets the string that will be used to resolve relative URIs found in the document into absolute URIs when required. Can be null or empty string. Default is null.

This property is used to resolve relative URIs into absolute in the following cases:

  1. When loading an HTML document from a stream and the document contains images with relative URIs and does not have a base URI specified in the BASE HTML element.
  2. When saving a document to PDF and other formats, to retrieve images linked using relative URIs so the images can be saved into the output document.

Example:

Opens an HTML document with images from a stream using a base URI.
// We are opening this HTML file:
//    <html>
//    <body>
//    <p>Simple file.</p>
//    <p><img src="Aspose.Words.gif" width="80" height="60"></p>
//    </body>
//    </html>
String fileName = getMyDir() + "Document.OpenFromStreamWithBaseUri.html";

// Open the stream.
InputStream stream = new FileInputStream(fileName);

// Open the document. Note the Document constructor detects HTML format automatically.
// Pass the URI of the base folder so any images with relative URIs in the HTML document can be found.
LoadOptions loadOptions = new LoadOptions();
loadOptions.setBaseUri(getMyDir());
Document doc = new Document(stream, loadOptions);

// You can close the stream now, it is no longer needed because the document is in memory.
stream.close();

// Save in the DOC format.
doc.save(getArtifactsDir() + "Document.OpenFromStreamWithBaseUri.doc");

getConvertShapeToOfficeMath/setConvertShapeToOfficeMath

→ inherited from LoadOptions
public boolean getConvertShapeToOfficeMath() / public void setConvertShapeToOfficeMath(boolean value)
Gets or sets whether to convert shapes with EquationXML to Office Math objects.

Example:

Shows how to convert shapes with EquationXML to Office Math objects.
LoadOptions loadOptions = new LoadOptions();
loadOptions.setConvertShapeToOfficeMath(false);

// Specify load option to convert math shapes to office math objects on loading stage.
Document doc = new Document(getMyDir() + "Document.ConvertShapeToOfficeMath.docx", loadOptions);
doc.save(getArtifactsDir() + "Document.ConvertShapeToOfficeMath.docx", SaveFormat.DOCX);

getDetectNumberingWithWhitespaces/setDetectNumberingWithWhitespaces

public boolean getDetectNumberingWithWhitespaces() / public void setDetectNumberingWithWhitespaces(boolean value)
Allows to specify how numbered list items are recognized when document is imported from plain text format. The default value is true.

If this option is set to false, lists recognition algorithm detects list paragraphs, when list numbers ends with either dot, right bracket or bullet symbols (such as "•", "*", "-" or "o").

If this option is set to true, whitespaces are also used as list number delimeters: list recognition algorithm for Arabic style numbering (1., 1.1.2.) uses both whitespaces and dot (".") symbols.

Example:

Shows how to load plain text as is.
TxtLoadOptions loadOptions = new TxtLoadOptions();
// If it sets to true Aspose.Words insert additional periods after numbers in the content.
loadOptions.setDetectNumberingWithWhitespaces(false);
loadOptions.setTrailingSpacesOptions(TxtTrailingSpacesOptions.PRESERVE);
loadOptions.setLeadingSpacesOptions(TxtLeadingSpacesOptions.PRESERVE);

Document doc = new Document(getMyDir() + "TxtLoadOptions.DetectNumberingWithWhitespaces.txt", loadOptions);
doc.save(getArtifactsDir() + "TxtLoadOptions.DetectNumberingWithWhitespaces.txt");

getEncoding/setEncoding

→ inherited from LoadOptions
public java.nio.charset.Charset getEncoding() / public void setEncoding(java.nio.charset.Charset value)
Gets or sets the encoding that will be used to load an HTML or TXT document if the encoding is not specified in HTML/TXT. Can be null. Default is null.

This property is used only when loading HTML or TXT documents.

If encoding is not specified in HTML/TXT and this property is null, then the system will try to automatically detect the encoding.


getFontSettings/setFontSettings

→ inherited from LoadOptions
public FontSettings getFontSettings() / public void setFontSettings(FontSettings value)
Allows to specify document font settings.

When loading some formats, Aspose.Words may require to resolve the fonts. For example, when loading HTML documents Aspose.Words may resolve the fonts to perform font fallback.

If set to null, default static font settings FontSettings.DefaultInstance will be used.

The default value is null.

Example:

Shows how to resolve fonts before loading HTML and SVG documents.
FontSettings fontSettings = new FontSettings();
TableSubstitutionRule substitutionRule = fontSettings.getSubstitutionSettings().getTableSubstitution();
// If "HaettenSchweiler" is not installed on the local machine,
// it is still considered available, because it is substituted with "Comic Sans MS"
substitutionRule.addSubstitutes("HaettenSchweiler", new String[]{"Comic Sans MS"});

LoadOptions loadOptions = new LoadOptions();
loadOptions.setFontSettings(fontSettings);
// The same for SVG document
Document doc = new Document(getMyDir() + "Document.LoadFormat.html", loadOptions);

Example:

Shows how to set font settings and apply them during the loading of a document.
// Create a FontSettings object that will substitute the "Times New Roman" font with the font "Arvo" from our "MyFonts" folder
FontSettings fontSettings = new FontSettings();
fontSettings.setFontsFolder(getFontsDir(), false);
fontSettings.getSubstitutionSettings().getTableSubstitution().addSubstitutes("Times New Roman", "Arvo");

// Set that FontSettings object as a member of a newly created LoadOptions object
LoadOptions loadOptions = new LoadOptions();
{
    loadOptions.setFontSettings(fontSettings);
}

// We can now open a document while also passing the LoadOptions object into the constructor so the font substitution occurs upon loading
Document doc = new Document(getMyDir() + "Document.docx", loadOptions);

// The effects of our font settings can be observed after rendering
doc.save(getArtifactsDir() + "Document.LoadOptionsFontSettings.pdf");

getLanguagePreferences

→ inherited from LoadOptions
public LanguagePreferences getLanguagePreferences()
Gets language preferences that will be used when document is loading.

Example:

Shows how to set up language preferences that will be used when document is loading
LoadOptions loadOptions = new LoadOptions();
loadOptions.getLanguagePreferences().addEditingLanguage(EditingLanguage.JAPANESE);

Document doc = new Document(getMyDir() + "Document.EditingLanguage.docx", loadOptions);

int localeIdFarEast = doc.getStyles().getDefaultFont().getLocaleIdFarEast();
if (localeIdFarEast == EditingLanguage.JAPANESE)
    System.out.println("The document either has no any FarEast language set in defaults or it was set to Japanese originally.");
else
    System.out.println("The document default FarEast language was set to another than Japanese language originally, so it is not overridden.");

getLeadingSpacesOptions/setLeadingSpacesOptions

public int getLeadingSpacesOptions() / public void setLeadingSpacesOptions(int value)
Gets or sets preferred option of a leading space handling. Default value is TxtLeadingSpacesOptions.CONVERT_TO_INDENT. The value of the property is TxtLeadingSpacesOptions integer constant.

Example:

Shows how to load plain text as is.
TxtLoadOptions loadOptions = new TxtLoadOptions();
// If it sets to true Aspose.Words insert additional periods after numbers in the content.
loadOptions.setDetectNumberingWithWhitespaces(false);
loadOptions.setTrailingSpacesOptions(TxtTrailingSpacesOptions.PRESERVE);
loadOptions.setLeadingSpacesOptions(TxtLeadingSpacesOptions.PRESERVE);

Document doc = new Document(getMyDir() + "TxtLoadOptions.DetectNumberingWithWhitespaces.txt", loadOptions);
doc.save(getArtifactsDir() + "TxtLoadOptions.DetectNumberingWithWhitespaces.txt");

getLoadFormat/setLoadFormat

→ inherited from LoadOptions
public int getLoadFormat() / public void setLoadFormat(int value)
Specifies the format of the document to be loaded. Default is LoadFormat.AUTO. The value of the property is LoadFormat integer constant.

It is recommended that you specify the LoadFormat.AUTO value and let Aspose.Words detect the file format automatically. If you know the format of the document you are about to load, you can specify the format explicitly and this will slightly reduce the loading time by the overhead associated with auto detecting the format. If you specify an explicit load format and it will turn out to be wrong, the auto detection will be invoked and a second attempt to load the file will be made.

Example:

Explicitly loads a document as HTML without automatic file format detection.
LoadOptions loadOptions = new LoadOptions();
loadOptions.setLoadFormat(com.aspose.words.LoadFormat.HTML);
Document doc = new Document(getMyDir() + "Document.LoadFormat.html", loadOptions);

Example:

Shows how to insert the HTML contents from a web page into a new document.
// The url of the page to load
URL url = new URL("http://www.aspose.com/");

// The easiest way to load our document from the internet is make use of the URLConnection class.
URLConnection webClient = url.openConnection();

// Download the bytes from the location referenced by the URL.
InputStream inputStream = webClient.getInputStream();

// Convert the input stream to a byte array.
int pos;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((pos = inputStream.read()) != -1) bos.write(pos);

byte[] dataBytes = bos.toByteArray();

// Wrap the bytes representing the document in memory into a stream object.
ByteArrayInputStream byteStream = new ByteArrayInputStream(dataBytes);

// The baseUri property should be set to ensure any relative img paths are retrieved correctly.
LoadOptions options = new LoadOptions(LoadFormat.HTML, "", url.getPath());

// Load the HTML document from stream and pass the LoadOptions object.
Document doc = new Document(byteStream, options);

// Save the document to disk.
// The extension of the filename can be changed to save the document into other formats. e.g PDF, DOCX, ODT, RTF.
doc.save(getArtifactsDir() + "Document.HtmlPageFromWebpage.doc");

getMswVersion/setMswVersion

→ inherited from LoadOptions
public int getMswVersion() / public void setMswVersion(int value)
Allows to specify that the document loading process should match a specific MS Word version. Default value is MsWordVersion.WORD_2007The value of the property is MsWordVersion integer constant. Different Word versions may handle certain aspects of document content and formatting slightly differently during the loading process, which may result in minor differences in Document Object Model.

Example:

Shows how to emulate the loading procedure of a specific Microsoft Word version during document loading.
// Create a new LoadOptions object, which will load documents according to MS Word 2007 specification by default
LoadOptions loadOptions = new LoadOptions();
Assert.assertEquals(loadOptions.getMswVersion(), MsWordVersion.WORD_2007);

// This document is missing the default paragraph format style,
// so when it is opened with either Microsoft Word or Aspose Words, that default style will be regenerated,
// and will show up in the Styles collection, with values according to Microsoft Word 2007 specifications
Document doc = new Document(getMyDir() + "Document.docx", loadOptions);
Assert.assertEquals(13.8, doc.getStyles().getDefaultParagraphFormat().getLineSpacing(), 0.005f);

// We can change the loading version like this, to Microsoft Word 2016
loadOptions.setMswVersion(MsWordVersion.WORD_2016);

// The generated default style now has a different spacing, which will impact the appearance of our document
doc = new Document(getMyDir() + "Document.docx", loadOptions);
Assert.assertEquals(12.95, doc.getStyles().getDefaultParagraphFormat().getLineSpacing(), 0.005f);

getPassword/setPassword

→ inherited from LoadOptions
public java.lang.String getPassword() / public void setPassword(java.lang.String value)
Gets or sets the password for opening an encrypted document. Can be null or empty string. Default is null.

You need to know the password to open an encrypted document. If the document is not encrypted, set this to null or empty string.

Example:

Shows how to sign encrypted document opened from a file.
String outputFileName = getArtifactsDir() + "Document.Encrypted.docx";

Document doc = new Document(getMyDir() + "Document.Encrypted.docx", new LoadOptions("docPassword"));

// Create certificate holder from a file.
CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw");

SignOptions signOptions = new SignOptions();
signOptions.setComments("Comment");
signOptions.setSignTime(new Date());
signOptions.setDecryptionPassword("docPassword");

// Digitally sign encrypted with "docPassword" document in the specified path.
DigitalSignatureUtil.sign(doc.getOriginalFileName(), outputFileName, certificateHolder, signOptions);

// Open encrypted document from a file.
LoadOptions loadOptions = new LoadOptions("docPassword");
Assert.assertEquals(loadOptions.getPassword(), signOptions.getDecryptionPassword());

Document signedDoc = new Document(outputFileName, loadOptions);

getPreserveIncludePictureField/setPreserveIncludePictureField

→ inherited from LoadOptions
public boolean getPreserveIncludePictureField() / public void setPreserveIncludePictureField(boolean value)
Gets or sets whether to preserve the INCLUDEPICTURE field when reading Microsoft Word formats. The default value is false.

By default, the INCLUDEPICTURE field is converted into a shape object. You can override that if you need the field to be preserved, for example, if you wish to update it programmatically. Note however that this approach is not common for Aspose.Words. Use it on your own risk.

One of the possible use cases may be using a MERGEFIELD as a child field to dynamically change the source path of the picture. In this case you need the INCLUDEPICTURE to be preserved in the model.

Example:

Shows a way to update a field ignoring the MERGEFORMAT switch
LoadOptions loadOptions = new LoadOptions();
loadOptions.setPreserveIncludePictureField(true);

Document doc = new Document(getMyDir() + "Field.UpdateFieldIgnoringMergeFormat.docx", loadOptions);

for (Field field : doc.getRange().getFields()) {
    if (((field.getType()) == (FieldType.FIELD_INCLUDE_PICTURE))) {
        FieldIncludePicture includePicture = (FieldIncludePicture) field;
        includePicture.setSourceFullName(getImageDir() + "dotnet-logo.png");
        includePicture.update(true);
    }
}

doc.updateFields();
doc.save(getArtifactsDir() + "Field.UpdateFieldIgnoringMergeFormat.docx");

getResourceLoadingCallback/setResourceLoadingCallback

→ inherited from LoadOptions
public IResourceLoadingCallback getResourceLoadingCallback() / public void setResourceLoadingCallback(IResourceLoadingCallback value)
Allows to control how external resources (images, style sheets) are loaded when a document is imported from HTML, MHTML.

Example:

Shows how to handle external resources in Html documents during loading.
public void loadOptionsCallback() throws Exception {
    // Create a new LoadOptions object and set its ResourceLoadingCallback attribute
    // as an instance of our IResourceLoadingCallback implementation
    LoadOptions loadOptions = new LoadOptions();
    {
        loadOptions.setResourceLoadingCallback(new HtmlLinkedResourceLoadingCallback());
    }

    // When we open an Html document, external resources such as references to CSS stylesheet files and external images
    // will be handled in a custom manner by the loading callback as the document is loaded
    Document doc = new Document(getMyDir() + "ResourcesForCallback.html", loadOptions);
    doc.save(getArtifactsDir() + "Document.LoadOptionsCallback.pdf");
}

/// <summary>
/// Resource loading callback that, upon encountering external resources,
/// acknowledges CSS style sheets and replaces all images with a substitute.
/// </summary>
private static class HtmlLinkedResourceLoadingCallback implements IResourceLoadingCallback {
    public int resourceLoading(ResourceLoadingArgs args) throws IOException {
        switch (args.getResourceType()) {
            case ResourceType.CSS_STYLE_SHEET:
                System.out.println(MessageFormat.format("External CSS Stylesheet found upon loading: {0}", args.getOriginalUri()));
                return ResourceLoadingAction.DEFAULT;
            case ResourceType.IMAGE:
                System.out.println(MessageFormat.format("External Image found upon loading: {0}", args.getOriginalUri()));

                byte[] imageBytes = DocumentHelper.getBytesFromStream(getAsposelogoUri().toURL().openStream());
                args.setData(imageBytes);

                return ResourceLoadingAction.USER_PROVIDED;

        }
        return ResourceLoadingAction.DEFAULT;
    }
}

getTrailingSpacesOptions/setTrailingSpacesOptions

public int getTrailingSpacesOptions() / public void setTrailingSpacesOptions(int value)
Gets or sets preferred option of a trailing space handling. Default value is TxtTrailingSpacesOptions.TRIM. The value of the property is TxtTrailingSpacesOptions integer constant.

Example:

Shows how to load plain text as is.
TxtLoadOptions loadOptions = new TxtLoadOptions();
// If it sets to true Aspose.Words insert additional periods after numbers in the content.
loadOptions.setDetectNumberingWithWhitespaces(false);
loadOptions.setTrailingSpacesOptions(TxtTrailingSpacesOptions.PRESERVE);
loadOptions.setLeadingSpacesOptions(TxtLeadingSpacesOptions.PRESERVE);

Document doc = new Document(getMyDir() + "TxtLoadOptions.DetectNumberingWithWhitespaces.txt", loadOptions);
doc.save(getArtifactsDir() + "TxtLoadOptions.DetectNumberingWithWhitespaces.txt");

getUpdateDirtyFields/setUpdateDirtyFields

→ inherited from LoadOptions
public boolean getUpdateDirtyFields() / public void setUpdateDirtyFields(boolean value)
Specifies whether to update the fields with the dirty attribute.

Example:

Shows how to use special property for updating field result
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Field fieldToc = builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u");
fieldToc.isDirty(true);

getWarningCallback/setWarningCallback

→ inherited from LoadOptions
public IWarningCallback getWarningCallback() / public void setWarningCallback(IWarningCallback value)
Called during a load operation, when an issue is detected that might result in data or formatting fidelity loss.

Example:

Shows how to print warnings that occur during document loading.
public void loadOptionsWarningCallback() throws Exception {
    // Create a new LoadOptions object and set its WarningCallback attribute as an instance of our IWarningCallback implementation
    LoadOptions loadOptions = new LoadOptions();
    {
        loadOptions.setWarningCallback(new DocumentLoadingWarningCallback());
    }

    // Minor warnings that might not prevent the effective loading of the document will now be printed
    Document doc = new Document(getMyDir() + "Document.docx", loadOptions);
}

/// <summary>
/// IWarningCallback that prints warnings and their details as they arise during document loading.
/// </summary>
private static class DocumentLoadingWarningCallback implements IWarningCallback {
    public void warning(WarningInfo info) {
        System.out.println(MessageFormat.format("WARNING: {0}, source: {1}", info.getWarningType(), info.getSource()));
        System.out.println(MessageFormat.format("\tDescription: {0}", info.getDescription()));
    }
}

See Also:
          Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
          Aspose.Words Support Forum - our preferred method of support.