java.lang.Object
com.aspose.words.LoadOptions
public class LoadOptions
Example:
Document doc;
// Aspose.Words throw an exception if we try to open an encrypted document without its password.
Assert.assertThrows(IncorrectPasswordException.class, () -> new Document(getMyDir() + "Encrypted.docx"));
// When loading such a document, the password is passed to the document's constructor using a LoadOptions object.
LoadOptions options = new LoadOptions("docPassword");
// There are two ways of loading an encrypted document with a LoadOptions object.
// 1 - Load the document from the local file system by filename.
doc = new Document(getMyDir() + "Encrypted.docx", options);
// 2 - Load the document from a stream.
InputStream stream = new FileInputStream(getMyDir() + "Encrypted.docx");
try
{
doc = new Document(stream, options);
}
finally { if (stream != null) stream.close(); }
| Constructor Summary |
|---|
LoadOptions()
Initializes a new instance of this class with default values. |
LoadOptions(java.lang.String password)
A shortcut to initialize a new instance of this class with the specified password to load an encrypted document. |
LoadOptions(int loadFormat, java.lang.String password, java.lang.String baseUri)
A shortcut to initialize a new instance of this class with properties set to the specified values. |
| Property Getters/Setters Summary | ||
|---|---|---|
java.lang.String | getBaseUri() | |
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. | ||
boolean | getConvertMetafilesToPng() | |
void | setConvertMetafilesToPng(boolean value) | |
|
Gets or sets whether to convert metafile ( |
||
boolean | getConvertShapeToOfficeMath() | |
void | setConvertShapeToOfficeMath(boolean value) | |
| Gets or sets whether to convert shapes with EquationXML to Office Math objects. | ||
java.nio.charset.Charset | getEncoding() | |
void | setEncoding(java.nio.charset.Charset value) | |
| Gets or sets the encoding that will be used to load an HTML, TXT, or CHM document if the encoding is not specified inside the document. Can be null. Default is null. | ||
FontSettings | getFontSettings() | |
void | setFontSettings(FontSettings value) | |
| Allows to specify document font settings. | ||
LanguagePreferences | getLanguagePreferences() | |
| Gets language preferences that will be used when document is loading. | ||
int | getLoadFormat() | |
void | setLoadFormat(int value) | |
|
Specifies the format of the document to be loaded.
Default is |
||
int | getMswVersion() | |
void | setMswVersion(int value) | |
|
Allows to specify that the document loading process should match a specific MS Word version.
Default value is |
||
java.lang.String | getPassword() | |
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. | ||
boolean | getPreserveIncludePictureField() | |
void | setPreserveIncludePictureField(boolean value) | |
| Gets or sets whether to preserve the INCLUDEPICTURE field when reading Microsoft Word formats. The default value is false. | ||
IResourceLoadingCallback | getResourceLoadingCallback() | |
void | setResourceLoadingCallback(IResourceLoadingCallback value) | |
| Allows to control how external resources (images, style sheets) are loaded when a document is imported from HTML, MHTML. | ||
java.lang.String | getTempFolder() | |
void | setTempFolder(java.lang.String value) | |
Allows to use temporary files when reading document.
By default this property is null and no temporary files are used.
|
||
boolean | getUpdateDirtyFields() | |
void | setUpdateDirtyFields(boolean value) | |
Specifies whether to update the fields with the dirty attribute.
|
||
IWarningCallback | getWarningCallback() | |
void | setWarningCallback(IWarningCallback value) | |
| Called during a load operation, when an issue is detected that might result in data or formatting fidelity loss. | ||
| Constructor Detail |
|---|
public LoadOptions()
Example:
Shows how to open an HTML document with images from a stream using a base URI.
InputStream stream = new FileInputStream(getMyDir() + "Document.html");
try /*JAVA: was using*/
{
// Pass the URI of the base folder while loading it
// so that any images with relative URIs in the HTML document can be found.
LoadOptions loadOptions = new LoadOptions();
loadOptions.setBaseUri(getImageDir());
Document doc = new Document(stream, loadOptions);
// Verify that the first shape of the document contains a valid image.
Shape shape = (Shape)doc.getChild(NodeType.SHAPE, 0, true);
Assert.assertTrue(shape.isImage());
Assert.assertNotNull(shape.getImageData().getImageBytes());
Assert.assertEquals(32.0, ConvertUtil.pointToPixel(shape.getWidth()), 0.01);
Assert.assertEquals(32.0, ConvertUtil.pointToPixel(shape.getHeight()), 0.01);
}
finally { if (stream != null) stream.close(); }public LoadOptions(java.lang.String password)
password - The password to open an encrypted document. Can be null or empty string.Example:
Shows how to load an encrypted Microsoft Word document.
Document doc;
// Aspose.Words throw an exception if we try to open an encrypted document without its password.
Assert.assertThrows(IncorrectPasswordException.class, () -> new Document(getMyDir() + "Encrypted.docx"));
// When loading such a document, the password is passed to the document's constructor using a LoadOptions object.
LoadOptions options = new LoadOptions("docPassword");
// There are two ways of loading an encrypted document with a LoadOptions object.
// 1 - Load the document from the local file system by filename.
doc = new Document(getMyDir() + "Encrypted.docx", options);
// 2 - Load the document from a stream.
InputStream stream = new FileInputStream(getMyDir() + "Encrypted.docx");
try
{
doc = new Document(stream, options);
}
finally { if (stream != null) stream.close(); }public LoadOptions(int loadFormat, java.lang.String password, java.lang.String baseUri)
loadFormat - A password - The password to open an encrypted document. Can be null or empty string.baseUri - The string that will be used to resolve relative URIs to absolute. Can be null or empty string.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 the local file system while converting it to .docx
doc.save(getArtifactsDir() + "Document.InsertHtmlFromWebPage.docx");| Property Getters/Setters Detail |
|---|
getBaseUri/setBaseUri | |
public java.lang.String getBaseUri() / public void setBaseUri(java.lang.String value) | |
This property is used to resolve relative URIs into absolute in the following cases:
Example:
Shows how to open an HTML document with images from a stream using a base URI.
InputStream stream = new FileInputStream(getMyDir() + "Document.html");
try /*JAVA: was using*/
{
// Pass the URI of the base folder while loading it
// so that any images with relative URIs in the HTML document can be found.
LoadOptions loadOptions = new LoadOptions();
loadOptions.setBaseUri(getImageDir());
Document doc = new Document(stream, loadOptions);
// Verify that the first shape of the document contains a valid image.
Shape shape = (Shape)doc.getChild(NodeType.SHAPE, 0, true);
Assert.assertTrue(shape.isImage());
Assert.assertNotNull(shape.getImageData().getImageBytes());
Assert.assertEquals(32.0, ConvertUtil.pointToPixel(shape.getWidth()), 0.01);
Assert.assertEquals(32.0, ConvertUtil.pointToPixel(shape.getHeight()), 0.01);
}
finally { if (stream != null) stream.close(); }getConvertMetafilesToPng/setConvertMetafilesToPng | |
public boolean getConvertMetafilesToPng() / public void setConvertMetafilesToPng(boolean value) | |
getConvertShapeToOfficeMath/setConvertShapeToOfficeMath | |
public boolean getConvertShapeToOfficeMath() / public void setConvertShapeToOfficeMath(boolean value) | |
getEncoding/setEncoding | |
public java.nio.charset.Charset getEncoding() / public void setEncoding(java.nio.charset.Charset value) | |
This property is used only when loading HTML, TXT, or CHM documents.
If encoding is not specified inside the document and this property is null, then the system will try to
automatically detect the encoding.
getFontSettings/setFontSettings | |
public FontSettings getFontSettings() / public void setFontSettings(FontSettings value) | |
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
The default value is null.
Example:
Shows how to designate font substitutes during loading.
LoadOptions loadOptions = new LoadOptions();
loadOptions.setFontSettings(new FontSettings());
// Set a font substitution rule for a LoadOptions object that replaces a font that's not installed in our system with one that is
TableSubstitutionRule substitutionRule = loadOptions.getFontSettings().getSubstitutionSettings().getTableSubstitution();
substitutionRule.addSubstitutes("MissingFont", new String[]{"Comic Sans MS"});
// If we pass that object while loading a document, any text with the "MissingFont" font will change to "Comic Sans MS"
Document doc = new Document(getMyDir() + "Missing font.html", loadOptions);
// At this point such text will still be in "MissingFont", and font substitution will be carried out once we save
Assert.assertEquals("MissingFont", doc.getFirstSection().getBody().getFirstParagraph().getRuns().get(0).getFont().getName());
doc.save(getArtifactsDir() + "Font.ResolveFontsBeforeLoadingDocument.pdf");getLanguagePreferences | |
public LanguagePreferences getLanguagePreferences() | |
getLoadFormat/setLoadFormat | |
public int getLoadFormat() / public void setLoadFormat(int value) | |
It is recommended that you specify the
Example:
Shows how to specify a base URI when opening an html document.// If we want to load an .html document which contains an image linked by a relative URI // while the image is in a different location, we will need to resolve the relative URI into an absolute one // by creating an HtmlLoadOptions and providing a base URI HtmlLoadOptions loadOptions = new HtmlLoadOptions(LoadFormat.HTML, "", getImageDir()); Assert.assertEquals(LoadFormat.HTML, loadOptions.getLoadFormat()); Document doc = new Document(getMyDir() + "Missing image.html", loadOptions); // While the image was broken in the input .html, it was successfully found in our base URI Shape imageShape = (Shape) doc.getChildNodes(NodeType.SHAPE, true).get(0); Assert.assertTrue(imageShape.isImage()); // The image will be displayed correctly by the output document doc.save(getArtifactsDir() + "HtmlLoadOptions.BaseUri.docx");
getMswVersion/setMswVersion | |
public int getMswVersion() / public void setMswVersion(int value) | |
getPassword/setPassword | |
public java.lang.String getPassword() / public void setPassword(java.lang.String value) | |
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 file.
// Create an X.509 certificate from a PKCS#12 store, which should contain a private key.
CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw");
// Create a comment, date, and decryption password which will be applied with our new digital signature.
SignOptions signOptions = new SignOptions();
{
signOptions.setComments("Comment");
signOptions.setSignTime(new Date());
signOptions.setDecryptionPassword("docPassword");
}
// Set a local system filename for the unsigned input document, and an output filename for its new digitally signed copy.
String inputFileName = getMyDir() + "Encrypted.docx";
String outputFileName = getArtifactsDir() + "DigitalSignatureUtil.DecryptionPassword.docx";
DigitalSignatureUtil.sign(inputFileName, outputFileName, certificateHolder, signOptions);getPreserveIncludePictureField/setPreserveIncludePictureField | |
public boolean getPreserveIncludePictureField() / public void setPreserveIncludePictureField(boolean value) | |
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 sample - INCLUDEPICTURE.docx", loadOptions);
for (Field field : doc.getRange().getFields()) {
if (((field.getType()) == (FieldType.FIELD_INCLUDE_PICTURE))) {
FieldIncludePicture includePicture = (FieldIncludePicture) field;
includePicture.setSourceFullName(getImageDir() + "Transparent background logo.png");
includePicture.update(true);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.UpdateFieldIgnoringMergeFormat.docx");getResourceLoadingCallback/setResourceLoadingCallback | |
public IResourceLoadingCallback getResourceLoadingCallback() / public void setResourceLoadingCallback(IResourceLoadingCallback value) | |
getTempFolder/setTempFolder | |
public java.lang.String getTempFolder() / public void setTempFolder(java.lang.String value) | |
null and no temporary files are used.
The folder must exist and be writable, otherwise an exception will be thrown.
Aspose.Words automatically deletes all temporary files when reading is complete.
Example:
Shows how to load a document using temporary files.
// Note that such an approach can reduce memory usage but degrades speed.
LoadOptions loadOptions = new LoadOptions();
loadOptions.setTempFolder("C:\\TempFolder\\");
// Ensure that the directory exists and load.
new File(loadOptions.getTempFolder()).mkdir();
Document doc = new Document(getMyDir() + "Document.docx", loadOptions);getUpdateDirtyFields/setUpdateDirtyFields | |
public boolean getUpdateDirtyFields() / public void setUpdateDirtyFields(boolean value) | |
dirty attribute.
Example:
Shows how to use special property for updating field result.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Give the document's built in property "Author" a value and display it with a field
doc.getBuiltInDocumentProperties().setAuthor("John Doe");
FieldAuthor field = (FieldAuthor) builder.insertField(FieldType.FIELD_AUTHOR, true);
Assert.assertFalse(field.isDirty());
Assert.assertEquals("John Doe", field.getResult());
// Update the "Author" property
doc.getBuiltInDocumentProperties().setAuthor("John & Jane Doe");
// AUTHOR is one of the field types whose fields do not update according to their source values in real time,
// and need to be updated manually beforehand every time an accurate value is required
Assert.assertEquals("John Doe", field.getResult());
// Since the field's value is out of date, we can mark it as "Dirty"
field.isDirty(true);
OutputStream docStream = new FileOutputStream(getArtifactsDir() + "Filed.UpdateDirtyFields.docx");
try {
doc.save(docStream, SaveFormat.DOCX);
// Re-open the document from the stream while using a LoadOptions object to specify
// whether to update all fields marked as "Dirty" in the process, so they can display accurate values immediately
LoadOptions options = new LoadOptions();
options.setUpdateDirtyFields(doUpdateDirtyFields);
doc = new Document(String.valueOf(docStream), options);
Assert.assertEquals("John & Jane Doe", doc.getBuiltInDocumentProperties().getAuthor());
field = (FieldAuthor) doc.getRange().getFields().get(0);
if (doUpdateDirtyFields) {
Assert.assertEquals("John & Jane Doe", field.getResult());
Assert.assertFalse(field.isDirty());
} else {
Assert.assertEquals("John Doe", field.getResult());
Assert.assertTrue(field.isDirty());
}
} finally {
if (docStream != null) docStream.close();
}getWarningCallback/setWarningCallback | |
public IWarningCallback getWarningCallback() / public void setWarningCallback(IWarningCallback value) | |