public class HtmlLoadOptions extends LoadOptions
Document object.
To learn more, visit the Specify Load Options documentation article.
Examples:
Shows how to support conditional comments while loading an HTML document.
HtmlLoadOptions loadOptions = new HtmlLoadOptions();
// If the value is true, then we take VML code into account while parsing the loaded document.
loadOptions.setSupportVml(supportVml);
// This document contains a JPEG image within "<!--[if gte vml 1]>" tags,
// and a different PNG image within "<![if !vml]>" tags.
// If we set the "SupportVml" flag to "true", then Aspose.Words will load the JPEG.
// If we set this flag to "false", then Aspose.Words will only load the PNG.
Document doc = new Document(getMyDir() + "VML conditional.htm", loadOptions);
if (supportVml)
Assert.assertEquals(ImageType.JPEG, ((Shape) doc.getChild(NodeType.SHAPE, 0, true)).getImageData().getImageType());
else
Assert.assertEquals(ImageType.PNG, ((Shape) doc.getChild(NodeType.SHAPE, 0, true)).getImageData().getImageType());
| Constructor and Description |
|---|
HtmlLoadOptions()
Initializes a new instance of this class with default values.
|
HtmlLoadOptions(int loadFormat,
java.lang.String password,
java.lang.String baseUri)
Initializes a new instance of this class.
|
HtmlLoadOptions(java.lang.String password)
A shortcut to initialize a new instance of this class with the specified password to load an encrypted document.
|
| Modifier and Type | Method and Description |
|---|---|
int |
getBlockImportMode()
Gets a value that specifies how properties of block-level elements are imported.
|
boolean |
getConvertSvgToEmf()
Gets a value indicating whether to convert loaded SVG images to the EMF format.
|
boolean |
getIgnoreNoscriptElements()
Gets a value indicating whether to ignore
|
int |
getPreferredControlType()
Gets preferred type of document nodes that will represent imported and
|
boolean |
getSupportFontFaceRules()
Gets a value indicating whether to support @font-face rules and whether to load declared fonts.
|
boolean |
getSupportVml()
Gets a value indicating whether to support VML images.
|
int |
getWebRequestTimeout()
The number of milliseconds to wait before the web request times out.
|
void |
setBlockImportMode(int value)
Sets a value that specifies how properties of block-level elements are imported.
|
void |
setConvertSvgToEmf(boolean value)
Sets a value indicating whether to convert loaded SVG images to the EMF format.
|
void |
setIgnoreNoscriptElements(boolean value)
Sets a value indicating whether to ignore
|
void |
setPreferredControlType(int value)
Sets preferred type of document nodes that will represent imported and
|
void |
setSupportFontFaceRules(boolean value)
Sets a value indicating whether to support @font-face rules and whether to load declared fonts.
|
void |
setSupportVml(boolean value)
Sets a value indicating whether to support VML images.
|
void |
setWebRequestTimeout(int value)
The number of milliseconds to wait before the web request times out.
|
equals, getBaseUri, getConvertMetafilesToPng, getConvertShapeToOfficeMath, getEncoding, getFontSettings, getIgnoreOleData, getLanguagePreferences, getLoadFormat, getMswVersion, getPassword, getPreserveIncludePictureField, getProgressCallback, getRecoveryMode, getResourceLoadingCallback, getTempFolder, getUpdateDirtyFields, getUseSystemLcid, getWarningCallback, setBaseUri, setConvertMetafilesToPng, setConvertShapeToOfficeMath, setEncoding, setFontSettings, setIgnoreOleData, setLoadFormat, setMswVersion, setPassword, setPreserveIncludePictureField, setProgressCallback, setRecoveryMode, setResourceLoadingCallback, setTempFolder, setUpdateDirtyFields, setUseSystemLcid, setWarningCallbackpublic HtmlLoadOptions()
Examples:
Shows how to support conditional comments while loading an HTML document.
HtmlLoadOptions loadOptions = new HtmlLoadOptions();
// If the value is true, then we take VML code into account while parsing the loaded document.
loadOptions.setSupportVml(supportVml);
// This document contains a JPEG image within "<!--[if gte vml 1]>" tags,
// and a different PNG image within "<![if !vml]>" tags.
// If we set the "SupportVml" flag to "true", then Aspose.Words will load the JPEG.
// If we set this flag to "false", then Aspose.Words will only load the PNG.
Document doc = new Document(getMyDir() + "VML conditional.htm", loadOptions);
if (supportVml)
Assert.assertEquals(ImageType.JPEG, ((Shape) doc.getChild(NodeType.SHAPE, 0, true)).getImageData().getImageType());
else
Assert.assertEquals(ImageType.PNG, ((Shape) doc.getChild(NodeType.SHAPE, 0, true)).getImageData().getImageType());
public HtmlLoadOptions(java.lang.String password)
Examples:
Shows how to encrypt an Html document, and then open it using a password.
// Create and sign an encrypted HTML document from an encrypted .docx.
CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw");
SignOptions signOptions = new SignOptions();
{
signOptions.setComments("Comment");
signOptions.setSignTime(new Date());
signOptions.setDecryptionPassword("docPassword");
}
String inputFileName = getMyDir() + "Encrypted.docx";
String outputFileName = getArtifactsDir() + "HtmlLoadOptions.EncryptedHtml.html";
DigitalSignatureUtil.sign(inputFileName, outputFileName, certificateHolder, signOptions);
// To load and read this document, we will need to pass its decryption
// password using a HtmlLoadOptions object.
HtmlLoadOptions loadOptions = new HtmlLoadOptions("docPassword");
Assert.assertEquals(loadOptions.getPassword(), signOptions.getDecryptionPassword());
Document doc = new Document(outputFileName, loadOptions);
Assert.assertEquals(doc.getText().trim(), "Test encrypted document.");
password - The password to open an encrypted document. Can be null or empty string.public HtmlLoadOptions(int loadFormat,
java.lang.String password,
java.lang.String baseUri)
public boolean getSupportVml()
Examples:
Shows how to support conditional comments while loading an HTML document.
HtmlLoadOptions loadOptions = new HtmlLoadOptions();
// If the value is true, then we take VML code into account while parsing the loaded document.
loadOptions.setSupportVml(supportVml);
// This document contains a JPEG image within "<!--[if gte vml 1]>" tags,
// and a different PNG image within "<![if !vml]>" tags.
// If we set the "SupportVml" flag to "true", then Aspose.Words will load the JPEG.
// If we set this flag to "false", then Aspose.Words will only load the PNG.
Document doc = new Document(getMyDir() + "VML conditional.htm", loadOptions);
if (supportVml)
Assert.assertEquals(ImageType.JPEG, ((Shape) doc.getChild(NodeType.SHAPE, 0, true)).getImageData().getImageType());
else
Assert.assertEquals(ImageType.PNG, ((Shape) doc.getChild(NodeType.SHAPE, 0, true)).getImageData().getImageType());
public void setSupportVml(boolean value)
Examples:
Shows how to support conditional comments while loading an HTML document.
HtmlLoadOptions loadOptions = new HtmlLoadOptions();
// If the value is true, then we take VML code into account while parsing the loaded document.
loadOptions.setSupportVml(supportVml);
// This document contains a JPEG image within "<!--[if gte vml 1]>" tags,
// and a different PNG image within "<![if !vml]>" tags.
// If we set the "SupportVml" flag to "true", then Aspose.Words will load the JPEG.
// If we set this flag to "false", then Aspose.Words will only load the PNG.
Document doc = new Document(getMyDir() + "VML conditional.htm", loadOptions);
if (supportVml)
Assert.assertEquals(ImageType.JPEG, ((Shape) doc.getChild(NodeType.SHAPE, 0, true)).getImageData().getImageType());
else
Assert.assertEquals(ImageType.PNG, ((Shape) doc.getChild(NodeType.SHAPE, 0, true)).getImageData().getImageType());
value - A value indicating whether to support VML images.public int getWebRequestTimeout()
Remarks:
The number of milliseconds that Aspose.Words waits for a response, when loading external resources (images, style sheets) linked in HTML and MHTML documents.
int value.public void setWebRequestTimeout(int value)
Remarks:
The number of milliseconds that Aspose.Words waits for a response, when loading external resources (images, style sheets) linked in HTML and MHTML documents.
value - The corresponding int value.public int getPreferredControlType()
HtmlControlType constants.public void setPreferredControlType(int value)
HtmlControlType.FORM_FIELD.
Remarks:
Please note that setting this property does not guarantee that all imported controls will be of the specified type. If an HTML control is not representable with document nodes of the preferred type, Aspose.Words will use a compatible HtmlControlType for that control.
Examples:
Shows how to set preferred type of document nodes that will represent imported and
final String html = "\r\n<html>\r\n<select name='ComboBox' size='1'>\r\n"
+ "<option value='val1'>item1</option>\r\n<option value='val2'></option>\r\n</select>\r\n</html>\r\n";
HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions();
htmlLoadOptions.setPreferredControlType(HtmlControlType.STRUCTURED_DOCUMENT_TAG);
Document doc = new Document(new ByteArrayInputStream(html.getBytes(StandardCharsets.UTF_8)), htmlLoadOptions);
NodeCollection nodes = doc.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true);
StructuredDocumentTag tag = (StructuredDocumentTag) nodes.get(0);
value - Preferred type of document nodes that will represent imported and HtmlControlType constants.public boolean getIgnoreNoscriptElements()
public void setIgnoreNoscriptElements(boolean value)
false.
Remarks:
Like MS Word, Aspose.Words does not support scripts and by default loads content of true forces Aspose.Words to ignore all
Examples:
Shows how to ignore
final String html = "\r\n<html>\r\n<head>\r\n<title>NOSCRIPT</title>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n<script type=\"text/javascript\">\r\nalert(\"Hello, world!\");\r\n</script>\r\n</head>\r\n<body>\r\n<noscript><p>Your browser does not support JavaScript!</p></noscript>\r\n</body>\r\n</html>";
HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions();
htmlLoadOptions.setIgnoreNoscriptElements(ignoreNoscriptElements);
Document doc = new Document(new ByteArrayInputStream(html.getBytes(StandardCharsets.UTF_8)), htmlLoadOptions);
doc.save(getArtifactsDir() + "HtmlLoadOptions.IgnoreNoscriptElements.pdf");
value - A value indicating whether to ignore public boolean getConvertSvgToEmf()
false and, if possible, loaded SVG images are stored as is without conversion.
Remarks:
Newer versions of MS Word support SVG images natively. If the MS Word version specified in load options supports SVG, Aspose.Words will store SVG images as is without conversion. If SVG is not supported, loaded SVG images will be converted to the EMF format.
If, however, this option is set to true, Aspose.Words will convert loaded SVG images to EMF even if SVG images are supported by the specified version of MS Word.
Examples:
Shows how to convert SVG objects to a different format when saving HTML documents.
String html =
"<html>\n <svg xmlns='http://www.w3.org/2000/svg' width='500' height='40' viewBox='0 0 500 40'>\n <text x='0' y='35' font-family='Verdana' font-size='35'>Hello world!</text>\n </svg>\n </html>";
// Use 'ConvertSvgToEmf' to turn back the legacy behavior
// where all SVG images loaded from an HTML document were converted to EMF.
// Now SVG images are loaded without conversion
// if the MS Word version specified in load options supports SVG images natively.
HtmlLoadOptions loadOptions = new HtmlLoadOptions(); { loadOptions.setConvertSvgToEmf(true); }
Document doc = new Document(new ByteArrayInputStream(html.getBytes()));
// This document contains a <svg> element in the form of text.
// When we save the document to HTML, we can pass a SaveOptions object
// to determine how the saving operation handles this object.
// Setting the "MetafileFormat" property to "HtmlMetafileFormat.Png" to convert it to a PNG image.
// Setting the "MetafileFormat" property to "HtmlMetafileFormat.Svg" preserve it as a SVG object.
// Setting the "MetafileFormat" property to "HtmlMetafileFormat.EmfOrWmf" to convert it to a metafile.
HtmlSaveOptions options = new HtmlSaveOptions();
{
options.setMetafileFormat(htmlMetafileFormat);
}
doc.save(getArtifactsDir() + "HtmlSaveOptions.MetafileFormat.html", options);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlSaveOptions.MetafileFormat.html"), StandardCharsets.UTF_8);
switch (htmlMetafileFormat) {
case HtmlMetafileFormat.PNG:
Assert.assertTrue(outDocContents.contains(
"<p style=\"margin-top:0pt; margin-bottom:0pt\">" +
"<img src=\"HtmlSaveOptions.MetafileFormat.001.png\" width=\"501\" height=\"41\" alt=\"\" " +
"style=\"-aw-left-pos:0pt; -aw-rel-hpos:column; -aw-rel-vpos:paragraph; -aw-top-pos:0pt; -aw-wrap-type:inline\" />" +
"</p>"));
break;
case HtmlMetafileFormat.SVG:
Assert.assertTrue(outDocContents.contains(
"<span style=\"-aw-left-pos:0pt; -aw-rel-hpos:column; -aw-rel-vpos:paragraph; -aw-top-pos:0pt; -aw-wrap-type:inline\">" +
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"1.1\" width=\"499\" height=\"40\">"));
break;
case HtmlMetafileFormat.EMF_OR_WMF:
Assert.assertTrue(outDocContents.contains(
"<p style=\"margin-top:0pt; margin-bottom:0pt\">" +
"<img src=\"HtmlSaveOptions.MetafileFormat.001.emf\" width=\"500\" height=\"40\" alt=\"\" " +
"style=\"-aw-left-pos:0pt; -aw-rel-hpos:column; -aw-rel-vpos:paragraph; -aw-top-pos:0pt; -aw-wrap-type:inline\" />" +
"</p>"));
break;
}
public void setConvertSvgToEmf(boolean value)
false and, if possible, loaded SVG images are stored as is without conversion.
Remarks:
Newer versions of MS Word support SVG images natively. If the MS Word version specified in load options supports SVG, Aspose.Words will store SVG images as is without conversion. If SVG is not supported, loaded SVG images will be converted to the EMF format.
If, however, this option is set to true, Aspose.Words will convert loaded SVG images to EMF even if SVG images are supported by the specified version of MS Word.
Examples:
Shows how to convert SVG objects to a different format when saving HTML documents.
String html =
"<html>\n <svg xmlns='http://www.w3.org/2000/svg' width='500' height='40' viewBox='0 0 500 40'>\n <text x='0' y='35' font-family='Verdana' font-size='35'>Hello world!</text>\n </svg>\n </html>";
// Use 'ConvertSvgToEmf' to turn back the legacy behavior
// where all SVG images loaded from an HTML document were converted to EMF.
// Now SVG images are loaded without conversion
// if the MS Word version specified in load options supports SVG images natively.
HtmlLoadOptions loadOptions = new HtmlLoadOptions(); { loadOptions.setConvertSvgToEmf(true); }
Document doc = new Document(new ByteArrayInputStream(html.getBytes()));
// This document contains a <svg> element in the form of text.
// When we save the document to HTML, we can pass a SaveOptions object
// to determine how the saving operation handles this object.
// Setting the "MetafileFormat" property to "HtmlMetafileFormat.Png" to convert it to a PNG image.
// Setting the "MetafileFormat" property to "HtmlMetafileFormat.Svg" preserve it as a SVG object.
// Setting the "MetafileFormat" property to "HtmlMetafileFormat.EmfOrWmf" to convert it to a metafile.
HtmlSaveOptions options = new HtmlSaveOptions();
{
options.setMetafileFormat(htmlMetafileFormat);
}
doc.save(getArtifactsDir() + "HtmlSaveOptions.MetafileFormat.html", options);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlSaveOptions.MetafileFormat.html"), StandardCharsets.UTF_8);
switch (htmlMetafileFormat) {
case HtmlMetafileFormat.PNG:
Assert.assertTrue(outDocContents.contains(
"<p style=\"margin-top:0pt; margin-bottom:0pt\">" +
"<img src=\"HtmlSaveOptions.MetafileFormat.001.png\" width=\"501\" height=\"41\" alt=\"\" " +
"style=\"-aw-left-pos:0pt; -aw-rel-hpos:column; -aw-rel-vpos:paragraph; -aw-top-pos:0pt; -aw-wrap-type:inline\" />" +
"</p>"));
break;
case HtmlMetafileFormat.SVG:
Assert.assertTrue(outDocContents.contains(
"<span style=\"-aw-left-pos:0pt; -aw-rel-hpos:column; -aw-rel-vpos:paragraph; -aw-top-pos:0pt; -aw-wrap-type:inline\">" +
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"1.1\" width=\"499\" height=\"40\">"));
break;
case HtmlMetafileFormat.EMF_OR_WMF:
Assert.assertTrue(outDocContents.contains(
"<p style=\"margin-top:0pt; margin-bottom:0pt\">" +
"<img src=\"HtmlSaveOptions.MetafileFormat.001.emf\" width=\"500\" height=\"40\" alt=\"\" " +
"style=\"-aw-left-pos:0pt; -aw-rel-hpos:column; -aw-rel-vpos:paragraph; -aw-top-pos:0pt; -aw-wrap-type:inline\" />" +
"</p>"));
break;
}
value - A value indicating whether to convert loaded SVG images to the EMF format.public int getBlockImportMode()
BlockImportMode.MERGE.
Examples:
Shows how properties of block-level elements are imported from HTML-based documents.
final String html = "\n<html>\n<div style='border:dotted'>\n<div style='border:solid'>\n<p>paragraph 1</p>\n<p>paragraph 2</p>\n</div>\n</div>\n</html>";
HtmlLoadOptions loadOptions = new HtmlLoadOptions();
// Set the new mode of import HTML block-level elements.
loadOptions.setBlockImportMode(blockImportMode);
Document doc = new Document(new ByteArrayInputStream(html.getBytes(StandardCharsets.UTF_8)), loadOptions);
doc.save(getArtifactsDir() + "HtmlLoadOptions.BlockImport.docx");
BlockImportMode constants.public void setBlockImportMode(int value)
BlockImportMode.MERGE.
Examples:
Shows how properties of block-level elements are imported from HTML-based documents.
final String html = "\n<html>\n<div style='border:dotted'>\n<div style='border:solid'>\n<p>paragraph 1</p>\n<p>paragraph 2</p>\n</div>\n</div>\n</html>";
HtmlLoadOptions loadOptions = new HtmlLoadOptions();
// Set the new mode of import HTML block-level elements.
loadOptions.setBlockImportMode(blockImportMode);
Document doc = new Document(new ByteArrayInputStream(html.getBytes(StandardCharsets.UTF_8)), loadOptions);
doc.save(getArtifactsDir() + "HtmlLoadOptions.BlockImport.docx");
value - A value that specifies how properties of block-level elements are imported. The value must be one of BlockImportMode constants.public boolean getSupportFontFaceRules()
false.
Remarks:
If this option is enabled, fonts declared in @font-face rules are loaded and embedded into the resulting document's font definitions (see DocumentBase.getFontInfos()). This makes the loaded fonts available for rendering but doesn't automatically enable embedding of the fonts upon saving. In order to save the document with loaded fonts, the FontInfoCollection.getEmbedTrueTypeFonts() / FontInfoCollection.setEmbedTrueTypeFonts(boolean) property of the DocumentBase.getFontInfos() collection should be set to true.
Supported font formats are TTF, EOT, and WOFF.
public void setSupportFontFaceRules(boolean value)
false.
Remarks:
If this option is enabled, fonts declared in @font-face rules are loaded and embedded into the resulting document's font definitions (see DocumentBase.getFontInfos()). This makes the loaded fonts available for rendering but doesn't automatically enable embedding of the fonts upon saving. In order to save the document with loaded fonts, the FontInfoCollection.getEmbedTrueTypeFonts() / FontInfoCollection.setEmbedTrueTypeFonts(boolean) property of the DocumentBase.getFontInfos() collection should be set to true.
Supported font formats are TTF, EOT, and WOFF.
value - A value indicating whether to support @font-face rules and whether to load declared fonts.