public class HtmlFixedSaveOptions extends FixedPageSaveOptions
SaveFormat.HTML_FIXED format.
To learn more, visit the Specify Save Options documentation article.
Examples:
Shows how to use a callback to print the URIs of external resources created while converting a document to HTML.
public void htmlFixedResourceFolder() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
ResourceUriPrinter callback = new ResourceUriPrinter();
HtmlFixedSaveOptions options = new HtmlFixedSaveOptions();
{
options.setSaveFormat(SaveFormat.HTML_FIXED);
options.setExportEmbeddedImages(false);
options.setResourcesFolder(getArtifactsDir() + "HtmlFixedResourceFolder");
options.setResourcesFolderAlias(getArtifactsDir() + "HtmlFixedResourceFolderAlias");
options.setShowPageBorder(false);
options.setResourceSavingCallback(callback);
}
// A folder specified by ResourcesFolderAlias will contain the resources instead of ResourcesFolder.
// We must ensure the folder exists before the streams can put their resources into it.
new File(options.getResourcesFolderAlias()).mkdir();
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.HtmlFixedResourceFolder.html", options);
System.out.println(callback.getText());
String[] resourceFiles = new File(getArtifactsDir() + "HtmlFixedResourceFolderAlias").list();
Assert.assertFalse(new File(getArtifactsDir() + "HtmlFixedResourceFolder").exists());
Assert.assertEquals(6, IterableUtils.countMatches(Arrays.asList(resourceFiles),
f -> f.endsWith(".jpeg") || f.endsWith(".png") || f.endsWith(".css")));
}
/// <summary>
/// Counts and prints URIs of resources contained by as they are converted to fixed HTML.
/// </summary>
private static class ResourceUriPrinter implements IResourceSavingCallback {
public void resourceSaving(ResourceSavingArgs args) throws Exception {
// If we set a folder alias in the SaveOptions object, we will be able to print it from here.
mText.append(MessageFormat.format("Resource #{0} \"{1}\"", ++mSavedResourceCount, args.getResourceFileName()));
String extension = FilenameUtils.getExtension(args.getResourceFileName());
switch (extension) {
case "ttf":
case "woff": {
// By default, 'ResourceFileUri' uses system folder for fonts.
// To avoid problems in other platforms you must explicitly specify the path for the fonts.
args.setResourceFileUri(getArtifactsDir() + File.separatorChar + args.getResourceFileName());
break;
}
}
mText.append("\t" + args.getResourceFileUri());
// If we have specified a folder in the "ResourcesFolderAlias" property,
// we will also need to redirect each stream to put its resource in that folder.
args.setResourceStream(new FileOutputStream(args.getResourceFileUri()));
args.setKeepResourceStreamOpen(false);
}
public String getText() {
return mText.toString();
}
private int mSavedResourceCount;
private final StringBuilder mText = new StringBuilder();
}
| Modifier and Type | Method and Description |
|---|---|
java.lang.String |
getCssClassNamesPrefix()
Specifies prefix which is added to all class names in style.css file.
|
java.nio.charset.Charset |
getEncoding() |
boolean |
getExportEmbeddedCss()
Specifies whether the CSS (Cascading Style Sheet) should be embedded into Html document.
|
boolean |
getExportEmbeddedFonts()
Specifies whether fonts should be embedded into Html document in Base64 format.
|
boolean |
getExportEmbeddedImages()
Specifies whether images should be embedded into Html document in Base64 format.
|
boolean |
getExportEmbeddedSvg()
Specifies whether SVG resources should be embedded into Html document.
|
boolean |
getExportFormFields()
Gets indication of whether form fields are exported as interactive items (as 'input' tag) rather than converted to text or graphics.
|
int |
getFontFormat()
Gets
ExportFontFormat used for font exporting. |
java.lang.String |
getIdPrefix()
Specifies a prefix that is prepended to all generated element IDs in the output document.
|
boolean |
getOptimizeOutput()
Flag indicates whether it is required to optimize output.
|
int |
getPageHorizontalAlignment()
Specifies the horizontal alignment of pages in an HTML document.
|
double |
getPageMargins()
Specifies the margins around pages in an HTML document.
|
boolean |
getRemoveJavaScriptFromLinks()
Specifies whether JavaScript will be removed from links.
|
IResourceSavingCallback |
getResourceSavingCallback()
Allows to control how resources (images, fonts and css) are saved when a document is exported to fixed page Html format.
|
java.lang.String |
getResourcesFolder()
Specifies the physical folder where resources (images, fonts, css) are saved when exporting a document to Html format.
|
java.lang.String |
getResourcesFolderAlias()
Specifies the name of the folder used to construct image URIs written into an Html document.
|
boolean |
getSaveFontFaceCssSeparately()
Flag indicates whether "@font-face" CSS rules should be placed into a separate file "fontFaces.css" when a document is being saved with external stylesheet (that is, when
getExportEmbeddedCss() / setExportEmbeddedCss(boolean) is false). |
int |
getSaveFormat()
Specifies the format in which the document will be saved if this save options object is used.
|
boolean |
getShowPageBorder()
Specifies whether border around pages should be shown.
|
boolean |
getUseTargetMachineFonts()
Flag indicates whether fonts from target machine must be used to display the document.
|
void |
setCssClassNamesPrefix(java.lang.String value)
Specifies prefix which is added to all class names in style.css file.
|
void |
setEncoding(java.nio.charset.Charset value) |
void |
setExportEmbeddedCss(boolean value)
Specifies whether the CSS (Cascading Style Sheet) should be embedded into Html document.
|
void |
setExportEmbeddedFonts(boolean value)
Specifies whether fonts should be embedded into Html document in Base64 format.
|
void |
setExportEmbeddedImages(boolean value)
Specifies whether images should be embedded into Html document in Base64 format.
|
void |
setExportEmbeddedSvg(boolean value)
Specifies whether SVG resources should be embedded into Html document.
|
void |
setExportFormFields(boolean value)
Sets indication of whether form fields are exported as interactive items (as 'input' tag) rather than converted to text or graphics.
|
void |
setFontFormat(int value)
Sets
ExportFontFormat used for font exporting. |
void |
setIdPrefix(java.lang.String value)
Specifies a prefix that is prepended to all generated element IDs in the output document.
|
void |
setOptimizeOutput(boolean value)
Flag indicates whether it is required to optimize output.
|
void |
setPageHorizontalAlignment(int value)
Specifies the horizontal alignment of pages in an HTML document.
|
void |
setPageMargins(double value)
Specifies the margins around pages in an HTML document.
|
void |
setRemoveJavaScriptFromLinks(boolean value)
Specifies whether JavaScript will be removed from links.
|
void |
setResourceSavingCallback(IResourceSavingCallback value)
Allows to control how resources (images, fonts and css) are saved when a document is exported to fixed page Html format.
|
void |
setResourcesFolder(java.lang.String value)
Specifies the physical folder where resources (images, fonts, css) are saved when exporting a document to Html format.
|
void |
setResourcesFolderAlias(java.lang.String value)
Specifies the name of the folder used to construct image URIs written into an Html document.
|
void |
setSaveFontFaceCssSeparately(boolean value)
Flag indicates whether "@font-face" CSS rules should be placed into a separate file "fontFaces.css" when a document is being saved with external stylesheet (that is, when
getExportEmbeddedCss() / setExportEmbeddedCss(boolean) is false). |
void |
setSaveFormat(int value)
Specifies the format in which the document will be saved if this save options object is used.
|
void |
setShowPageBorder(boolean value)
Specifies whether border around pages should be shown.
|
void |
setUseTargetMachineFonts(boolean value)
Flag indicates whether fonts from target machine must be used to display the document.
|
assertValidIdPrefix, equals, getColorMode, getJpegQuality, getMetafileRenderingOptions, getNumeralFormat, getPageSavingCallback, getPageSet, isValidIdPrefix, setColorMode, setJpegQuality, setMetafileRenderingOptions, setNumeralFormat, setPageSavingCallback, setPageSetcreateSaveOptions, createSaveOptions, getAllowEmbeddingPostScriptFonts, getCustomTimeZoneInfo, getDefaultTemplate, getDml3DEffectsRenderingMode, getDmlEffectsRenderingMode, getDmlRenderingMode, getExportGeneratorName, getImlRenderingMode, getMemoryOptimization, getPrettyFormat, getProgressCallback, getTempFolder, getUpdateAmbiguousTextFont, getUpdateCreatedTimeProperty, getUpdateFields, getUpdateLastPrintedProperty, getUpdateLastSavedTimeProperty, getUseAntiAliasing, getUseHighQualityRendering, setAllowEmbeddingPostScriptFonts, setCustomTimeZoneInfo, setDefaultTemplate, setDml3DEffectsRenderingMode, setDmlEffectsRenderingMode, setDmlRenderingMode, setExportGeneratorName, setImlRenderingMode, setMemoryOptimization, setPrettyFormat, setProgressCallback, setTempFolder, setUpdateAmbiguousTextFont, setUpdateCreatedTimeProperty, setUpdateFields, setUpdateLastPrintedProperty, setUpdateLastSavedTimeProperty, setUseAntiAliasing, setUseHighQualityRenderingpublic int getSaveFormat()
SaveFormat.HTML_FIXED.
Examples:
Shows how to use a callback to print the URIs of external resources created while converting a document to HTML.
public void htmlFixedResourceFolder() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
ResourceUriPrinter callback = new ResourceUriPrinter();
HtmlFixedSaveOptions options = new HtmlFixedSaveOptions();
{
options.setSaveFormat(SaveFormat.HTML_FIXED);
options.setExportEmbeddedImages(false);
options.setResourcesFolder(getArtifactsDir() + "HtmlFixedResourceFolder");
options.setResourcesFolderAlias(getArtifactsDir() + "HtmlFixedResourceFolderAlias");
options.setShowPageBorder(false);
options.setResourceSavingCallback(callback);
}
// A folder specified by ResourcesFolderAlias will contain the resources instead of ResourcesFolder.
// We must ensure the folder exists before the streams can put their resources into it.
new File(options.getResourcesFolderAlias()).mkdir();
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.HtmlFixedResourceFolder.html", options);
System.out.println(callback.getText());
String[] resourceFiles = new File(getArtifactsDir() + "HtmlFixedResourceFolderAlias").list();
Assert.assertFalse(new File(getArtifactsDir() + "HtmlFixedResourceFolder").exists());
Assert.assertEquals(6, IterableUtils.countMatches(Arrays.asList(resourceFiles),
f -> f.endsWith(".jpeg") || f.endsWith(".png") || f.endsWith(".css")));
}
/// <summary>
/// Counts and prints URIs of resources contained by as they are converted to fixed HTML.
/// </summary>
private static class ResourceUriPrinter implements IResourceSavingCallback {
public void resourceSaving(ResourceSavingArgs args) throws Exception {
// If we set a folder alias in the SaveOptions object, we will be able to print it from here.
mText.append(MessageFormat.format("Resource #{0} \"{1}\"", ++mSavedResourceCount, args.getResourceFileName()));
String extension = FilenameUtils.getExtension(args.getResourceFileName());
switch (extension) {
case "ttf":
case "woff": {
// By default, 'ResourceFileUri' uses system folder for fonts.
// To avoid problems in other platforms you must explicitly specify the path for the fonts.
args.setResourceFileUri(getArtifactsDir() + File.separatorChar + args.getResourceFileName());
break;
}
}
mText.append("\t" + args.getResourceFileUri());
// If we have specified a folder in the "ResourcesFolderAlias" property,
// we will also need to redirect each stream to put its resource in that folder.
args.setResourceStream(new FileOutputStream(args.getResourceFileUri()));
args.setKeepResourceStreamOpen(false);
}
public String getText() {
return mText.toString();
}
private int mSavedResourceCount;
private final StringBuilder mText = new StringBuilder();
}
getSaveFormat in class SaveOptionsint value. The returned value is one of SaveFormat constants.public void setSaveFormat(int value)
SaveFormat.HTML_FIXED.
Examples:
Shows how to use a callback to print the URIs of external resources created while converting a document to HTML.
public void htmlFixedResourceFolder() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
ResourceUriPrinter callback = new ResourceUriPrinter();
HtmlFixedSaveOptions options = new HtmlFixedSaveOptions();
{
options.setSaveFormat(SaveFormat.HTML_FIXED);
options.setExportEmbeddedImages(false);
options.setResourcesFolder(getArtifactsDir() + "HtmlFixedResourceFolder");
options.setResourcesFolderAlias(getArtifactsDir() + "HtmlFixedResourceFolderAlias");
options.setShowPageBorder(false);
options.setResourceSavingCallback(callback);
}
// A folder specified by ResourcesFolderAlias will contain the resources instead of ResourcesFolder.
// We must ensure the folder exists before the streams can put their resources into it.
new File(options.getResourcesFolderAlias()).mkdir();
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.HtmlFixedResourceFolder.html", options);
System.out.println(callback.getText());
String[] resourceFiles = new File(getArtifactsDir() + "HtmlFixedResourceFolderAlias").list();
Assert.assertFalse(new File(getArtifactsDir() + "HtmlFixedResourceFolder").exists());
Assert.assertEquals(6, IterableUtils.countMatches(Arrays.asList(resourceFiles),
f -> f.endsWith(".jpeg") || f.endsWith(".png") || f.endsWith(".css")));
}
/// <summary>
/// Counts and prints URIs of resources contained by as they are converted to fixed HTML.
/// </summary>
private static class ResourceUriPrinter implements IResourceSavingCallback {
public void resourceSaving(ResourceSavingArgs args) throws Exception {
// If we set a folder alias in the SaveOptions object, we will be able to print it from here.
mText.append(MessageFormat.format("Resource #{0} \"{1}\"", ++mSavedResourceCount, args.getResourceFileName()));
String extension = FilenameUtils.getExtension(args.getResourceFileName());
switch (extension) {
case "ttf":
case "woff": {
// By default, 'ResourceFileUri' uses system folder for fonts.
// To avoid problems in other platforms you must explicitly specify the path for the fonts.
args.setResourceFileUri(getArtifactsDir() + File.separatorChar + args.getResourceFileName());
break;
}
}
mText.append("\t" + args.getResourceFileUri());
// If we have specified a folder in the "ResourcesFolderAlias" property,
// we will also need to redirect each stream to put its resource in that folder.
args.setResourceStream(new FileOutputStream(args.getResourceFileUri()));
args.setKeepResourceStreamOpen(false);
}
public String getText() {
return mText.toString();
}
private int mSavedResourceCount;
private final StringBuilder mText = new StringBuilder();
}
setSaveFormat in class SaveOptionsvalue - The corresponding int value. The value must be one of SaveFormat constants.public boolean getShowPageBorder()
true.
Examples:
Shows how to use a callback to print the URIs of external resources created while converting a document to HTML.
public void htmlFixedResourceFolder() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
ResourceUriPrinter callback = new ResourceUriPrinter();
HtmlFixedSaveOptions options = new HtmlFixedSaveOptions();
{
options.setSaveFormat(SaveFormat.HTML_FIXED);
options.setExportEmbeddedImages(false);
options.setResourcesFolder(getArtifactsDir() + "HtmlFixedResourceFolder");
options.setResourcesFolderAlias(getArtifactsDir() + "HtmlFixedResourceFolderAlias");
options.setShowPageBorder(false);
options.setResourceSavingCallback(callback);
}
// A folder specified by ResourcesFolderAlias will contain the resources instead of ResourcesFolder.
// We must ensure the folder exists before the streams can put their resources into it.
new File(options.getResourcesFolderAlias()).mkdir();
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.HtmlFixedResourceFolder.html", options);
System.out.println(callback.getText());
String[] resourceFiles = new File(getArtifactsDir() + "HtmlFixedResourceFolderAlias").list();
Assert.assertFalse(new File(getArtifactsDir() + "HtmlFixedResourceFolder").exists());
Assert.assertEquals(6, IterableUtils.countMatches(Arrays.asList(resourceFiles),
f -> f.endsWith(".jpeg") || f.endsWith(".png") || f.endsWith(".css")));
}
/// <summary>
/// Counts and prints URIs of resources contained by as they are converted to fixed HTML.
/// </summary>
private static class ResourceUriPrinter implements IResourceSavingCallback {
public void resourceSaving(ResourceSavingArgs args) throws Exception {
// If we set a folder alias in the SaveOptions object, we will be able to print it from here.
mText.append(MessageFormat.format("Resource #{0} \"{1}\"", ++mSavedResourceCount, args.getResourceFileName()));
String extension = FilenameUtils.getExtension(args.getResourceFileName());
switch (extension) {
case "ttf":
case "woff": {
// By default, 'ResourceFileUri' uses system folder for fonts.
// To avoid problems in other platforms you must explicitly specify the path for the fonts.
args.setResourceFileUri(getArtifactsDir() + File.separatorChar + args.getResourceFileName());
break;
}
}
mText.append("\t" + args.getResourceFileUri());
// If we have specified a folder in the "ResourcesFolderAlias" property,
// we will also need to redirect each stream to put its resource in that folder.
args.setResourceStream(new FileOutputStream(args.getResourceFileUri()));
args.setKeepResourceStreamOpen(false);
}
public String getText() {
return mText.toString();
}
private int mSavedResourceCount;
private final StringBuilder mText = new StringBuilder();
}
boolean value.public void setShowPageBorder(boolean value)
true.
Examples:
Shows how to use a callback to print the URIs of external resources created while converting a document to HTML.
public void htmlFixedResourceFolder() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
ResourceUriPrinter callback = new ResourceUriPrinter();
HtmlFixedSaveOptions options = new HtmlFixedSaveOptions();
{
options.setSaveFormat(SaveFormat.HTML_FIXED);
options.setExportEmbeddedImages(false);
options.setResourcesFolder(getArtifactsDir() + "HtmlFixedResourceFolder");
options.setResourcesFolderAlias(getArtifactsDir() + "HtmlFixedResourceFolderAlias");
options.setShowPageBorder(false);
options.setResourceSavingCallback(callback);
}
// A folder specified by ResourcesFolderAlias will contain the resources instead of ResourcesFolder.
// We must ensure the folder exists before the streams can put their resources into it.
new File(options.getResourcesFolderAlias()).mkdir();
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.HtmlFixedResourceFolder.html", options);
System.out.println(callback.getText());
String[] resourceFiles = new File(getArtifactsDir() + "HtmlFixedResourceFolderAlias").list();
Assert.assertFalse(new File(getArtifactsDir() + "HtmlFixedResourceFolder").exists());
Assert.assertEquals(6, IterableUtils.countMatches(Arrays.asList(resourceFiles),
f -> f.endsWith(".jpeg") || f.endsWith(".png") || f.endsWith(".css")));
}
/// <summary>
/// Counts and prints URIs of resources contained by as they are converted to fixed HTML.
/// </summary>
private static class ResourceUriPrinter implements IResourceSavingCallback {
public void resourceSaving(ResourceSavingArgs args) throws Exception {
// If we set a folder alias in the SaveOptions object, we will be able to print it from here.
mText.append(MessageFormat.format("Resource #{0} \"{1}\"", ++mSavedResourceCount, args.getResourceFileName()));
String extension = FilenameUtils.getExtension(args.getResourceFileName());
switch (extension) {
case "ttf":
case "woff": {
// By default, 'ResourceFileUri' uses system folder for fonts.
// To avoid problems in other platforms you must explicitly specify the path for the fonts.
args.setResourceFileUri(getArtifactsDir() + File.separatorChar + args.getResourceFileName());
break;
}
}
mText.append("\t" + args.getResourceFileUri());
// If we have specified a folder in the "ResourcesFolderAlias" property,
// we will also need to redirect each stream to put its resource in that folder.
args.setResourceStream(new FileOutputStream(args.getResourceFileUri()));
args.setKeepResourceStreamOpen(false);
}
public String getText() {
return mText.toString();
}
private int mSavedResourceCount;
private final StringBuilder mText = new StringBuilder();
}
value - The corresponding boolean value.public int getPageHorizontalAlignment()
HtmlFixedPageHorizontalAlignment.CENTER.
Examples:
Shows how to set the horizontal alignment of pages when saving a document to HTML.
Document doc = new Document(getMyDir() + "Rendering.docx");
HtmlFixedSaveOptions htmlFixedSaveOptions = new HtmlFixedSaveOptions();
{
htmlFixedSaveOptions.setPageHorizontalAlignment(pageHorizontalAlignment);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.HorizontalAlignment.html", htmlFixedSaveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.HorizontalAlignment/styles.css"), StandardCharsets.UTF_8);
switch (pageHorizontalAlignment)
{
case HtmlFixedPageHorizontalAlignment.CENTER:
Assert.assertTrue(Pattern.compile(
"[.]awpage [{] position:relative; border:solid 1pt black; margin:10pt auto 10pt auto; overflow:hidden; [}]").matcher(outDocContents).find());
break;
case HtmlFixedPageHorizontalAlignment.LEFT:
Assert.assertTrue(Pattern.compile(
"[.]awpage [{] position:relative; border:solid 1pt black; margin:10pt auto 10pt 10pt; overflow:hidden; [}]").matcher(outDocContents).find());
break;
case HtmlFixedPageHorizontalAlignment.RIGHT:
Assert.assertTrue(Pattern.compile(
"[.]awpage [{] position:relative; border:solid 1pt black; margin:10pt 10pt 10pt auto; overflow:hidden; [}]").matcher(outDocContents).find());
break;
}
int value. The returned value is one of HtmlFixedPageHorizontalAlignment constants.public void setPageHorizontalAlignment(int value)
HtmlFixedPageHorizontalAlignment.CENTER.
Examples:
Shows how to set the horizontal alignment of pages when saving a document to HTML.
Document doc = new Document(getMyDir() + "Rendering.docx");
HtmlFixedSaveOptions htmlFixedSaveOptions = new HtmlFixedSaveOptions();
{
htmlFixedSaveOptions.setPageHorizontalAlignment(pageHorizontalAlignment);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.HorizontalAlignment.html", htmlFixedSaveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.HorizontalAlignment/styles.css"), StandardCharsets.UTF_8);
switch (pageHorizontalAlignment)
{
case HtmlFixedPageHorizontalAlignment.CENTER:
Assert.assertTrue(Pattern.compile(
"[.]awpage [{] position:relative; border:solid 1pt black; margin:10pt auto 10pt auto; overflow:hidden; [}]").matcher(outDocContents).find());
break;
case HtmlFixedPageHorizontalAlignment.LEFT:
Assert.assertTrue(Pattern.compile(
"[.]awpage [{] position:relative; border:solid 1pt black; margin:10pt auto 10pt 10pt; overflow:hidden; [}]").matcher(outDocContents).find());
break;
case HtmlFixedPageHorizontalAlignment.RIGHT:
Assert.assertTrue(Pattern.compile(
"[.]awpage [{] position:relative; border:solid 1pt black; margin:10pt 10pt 10pt auto; overflow:hidden; [}]").matcher(outDocContents).find());
break;
}
value - The corresponding int value. The value must be one of HtmlFixedPageHorizontalAlignment constants.public double getPageMargins()
Remarks:
Depends on the value of getPageHorizontalAlignment() / setPageHorizontalAlignment(int) property:
HtmlFixedPageHorizontalAlignment.LEFT.
HtmlFixedPageHorizontalAlignment.RIGHT.
HtmlFixedPageHorizontalAlignment.CENTER.
Examples:
Shows how to adjust page margins when saving a document to HTML.
Document doc = new Document(getMyDir() + "Document.docx");
HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions();
{
saveOptions.setPageMargins(15.0);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.PageMargins.html", saveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.PageMargins/styles.css"), StandardCharsets.UTF_8);
Assert.assertTrue(Pattern.compile(
"[.]awpage [{] position:relative; border:solid 1pt black; margin:15pt auto 15pt auto; overflow:hidden; [}]").matcher(outDocContents).find());
double value.public void setPageMargins(double value)
Remarks:
Depends on the value of getPageHorizontalAlignment() / setPageHorizontalAlignment(int) property:
HtmlFixedPageHorizontalAlignment.LEFT.
HtmlFixedPageHorizontalAlignment.RIGHT.
HtmlFixedPageHorizontalAlignment.CENTER.
Examples:
Shows how to adjust page margins when saving a document to HTML.
Document doc = new Document(getMyDir() + "Document.docx");
HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions();
{
saveOptions.setPageMargins(15.0);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.PageMargins.html", saveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.PageMargins/styles.css"), StandardCharsets.UTF_8);
Assert.assertTrue(Pattern.compile(
"[.]awpage [{] position:relative; border:solid 1pt black; margin:15pt auto 15pt auto; overflow:hidden; [}]").matcher(outDocContents).find());
value - The corresponding double value.public java.lang.String getResourcesFolder()
null.
Remarks:
Has effect only if getExportEmbeddedImages() / setExportEmbeddedImages(boolean) property is false.
When you save a Document in Html format, Aspose.Words needs to save all images embedded in the document as standalone files. getResourcesFolder() / setResourcesFolder(java.lang.String) allows you to specify where the images will be saved and getResourcesFolderAlias() / setResourcesFolderAlias(java.lang.String) allows to specify how the image URIs will be constructed.
If you save a document into a file and provide a file name, Aspose.Words, by default, saves the images in the same folder where the document file is saved. Use getResourcesFolder() / setResourcesFolder(java.lang.String) to override this behavior.
If you save a document into a stream, Aspose.Words does not have a folder where to save the images, but still needs to save the images somewhere. In this case, you need to specify an accessible folder by using the getResourcesFolder() / setResourcesFolder(java.lang.String) property
Examples:
Shows how to use a callback to print the URIs of external resources created while converting a document to HTML.
public void htmlFixedResourceFolder() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
ResourceUriPrinter callback = new ResourceUriPrinter();
HtmlFixedSaveOptions options = new HtmlFixedSaveOptions();
{
options.setSaveFormat(SaveFormat.HTML_FIXED);
options.setExportEmbeddedImages(false);
options.setResourcesFolder(getArtifactsDir() + "HtmlFixedResourceFolder");
options.setResourcesFolderAlias(getArtifactsDir() + "HtmlFixedResourceFolderAlias");
options.setShowPageBorder(false);
options.setResourceSavingCallback(callback);
}
// A folder specified by ResourcesFolderAlias will contain the resources instead of ResourcesFolder.
// We must ensure the folder exists before the streams can put their resources into it.
new File(options.getResourcesFolderAlias()).mkdir();
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.HtmlFixedResourceFolder.html", options);
System.out.println(callback.getText());
String[] resourceFiles = new File(getArtifactsDir() + "HtmlFixedResourceFolderAlias").list();
Assert.assertFalse(new File(getArtifactsDir() + "HtmlFixedResourceFolder").exists());
Assert.assertEquals(6, IterableUtils.countMatches(Arrays.asList(resourceFiles),
f -> f.endsWith(".jpeg") || f.endsWith(".png") || f.endsWith(".css")));
}
/// <summary>
/// Counts and prints URIs of resources contained by as they are converted to fixed HTML.
/// </summary>
private static class ResourceUriPrinter implements IResourceSavingCallback {
public void resourceSaving(ResourceSavingArgs args) throws Exception {
// If we set a folder alias in the SaveOptions object, we will be able to print it from here.
mText.append(MessageFormat.format("Resource #{0} \"{1}\"", ++mSavedResourceCount, args.getResourceFileName()));
String extension = FilenameUtils.getExtension(args.getResourceFileName());
switch (extension) {
case "ttf":
case "woff": {
// By default, 'ResourceFileUri' uses system folder for fonts.
// To avoid problems in other platforms you must explicitly specify the path for the fonts.
args.setResourceFileUri(getArtifactsDir() + File.separatorChar + args.getResourceFileName());
break;
}
}
mText.append("\t" + args.getResourceFileUri());
// If we have specified a folder in the "ResourcesFolderAlias" property,
// we will also need to redirect each stream to put its resource in that folder.
args.setResourceStream(new FileOutputStream(args.getResourceFileUri()));
args.setKeepResourceStreamOpen(false);
}
public String getText() {
return mText.toString();
}
private int mSavedResourceCount;
private final StringBuilder mText = new StringBuilder();
}
String value.getResourcesFolderAlias(),
setResourcesFolderAlias(java.lang.String)public void setResourcesFolder(java.lang.String value)
null.
Remarks:
Has effect only if getExportEmbeddedImages() / setExportEmbeddedImages(boolean) property is false.
When you save a Document in Html format, Aspose.Words needs to save all images embedded in the document as standalone files. getResourcesFolder() / setResourcesFolder(java.lang.String) allows you to specify where the images will be saved and getResourcesFolderAlias() / setResourcesFolderAlias(java.lang.String) allows to specify how the image URIs will be constructed.
If you save a document into a file and provide a file name, Aspose.Words, by default, saves the images in the same folder where the document file is saved. Use getResourcesFolder() / setResourcesFolder(java.lang.String) to override this behavior.
If you save a document into a stream, Aspose.Words does not have a folder where to save the images, but still needs to save the images somewhere. In this case, you need to specify an accessible folder by using the getResourcesFolder() / setResourcesFolder(java.lang.String) property
Examples:
Shows how to use a callback to print the URIs of external resources created while converting a document to HTML.
public void htmlFixedResourceFolder() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
ResourceUriPrinter callback = new ResourceUriPrinter();
HtmlFixedSaveOptions options = new HtmlFixedSaveOptions();
{
options.setSaveFormat(SaveFormat.HTML_FIXED);
options.setExportEmbeddedImages(false);
options.setResourcesFolder(getArtifactsDir() + "HtmlFixedResourceFolder");
options.setResourcesFolderAlias(getArtifactsDir() + "HtmlFixedResourceFolderAlias");
options.setShowPageBorder(false);
options.setResourceSavingCallback(callback);
}
// A folder specified by ResourcesFolderAlias will contain the resources instead of ResourcesFolder.
// We must ensure the folder exists before the streams can put their resources into it.
new File(options.getResourcesFolderAlias()).mkdir();
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.HtmlFixedResourceFolder.html", options);
System.out.println(callback.getText());
String[] resourceFiles = new File(getArtifactsDir() + "HtmlFixedResourceFolderAlias").list();
Assert.assertFalse(new File(getArtifactsDir() + "HtmlFixedResourceFolder").exists());
Assert.assertEquals(6, IterableUtils.countMatches(Arrays.asList(resourceFiles),
f -> f.endsWith(".jpeg") || f.endsWith(".png") || f.endsWith(".css")));
}
/// <summary>
/// Counts and prints URIs of resources contained by as they are converted to fixed HTML.
/// </summary>
private static class ResourceUriPrinter implements IResourceSavingCallback {
public void resourceSaving(ResourceSavingArgs args) throws Exception {
// If we set a folder alias in the SaveOptions object, we will be able to print it from here.
mText.append(MessageFormat.format("Resource #{0} \"{1}\"", ++mSavedResourceCount, args.getResourceFileName()));
String extension = FilenameUtils.getExtension(args.getResourceFileName());
switch (extension) {
case "ttf":
case "woff": {
// By default, 'ResourceFileUri' uses system folder for fonts.
// To avoid problems in other platforms you must explicitly specify the path for the fonts.
args.setResourceFileUri(getArtifactsDir() + File.separatorChar + args.getResourceFileName());
break;
}
}
mText.append("\t" + args.getResourceFileUri());
// If we have specified a folder in the "ResourcesFolderAlias" property,
// we will also need to redirect each stream to put its resource in that folder.
args.setResourceStream(new FileOutputStream(args.getResourceFileUri()));
args.setKeepResourceStreamOpen(false);
}
public String getText() {
return mText.toString();
}
private int mSavedResourceCount;
private final StringBuilder mText = new StringBuilder();
}
value - The corresponding String value.getResourcesFolderAlias(),
setResourcesFolderAlias(java.lang.String)public java.lang.String getResourcesFolderAlias()
null.
Remarks:
When you save a Document in Html format, Aspose.Words needs to save all images embedded in the document as standalone files. getResourcesFolder() / setResourcesFolder(java.lang.String) allows you to specify where the images will be saved and getResourcesFolderAlias() / setResourcesFolderAlias(java.lang.String) allows to specify how the image URIs will be constructed.
Examples:
Shows how to use a callback to print the URIs of external resources created while converting a document to HTML.
public void htmlFixedResourceFolder() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
ResourceUriPrinter callback = new ResourceUriPrinter();
HtmlFixedSaveOptions options = new HtmlFixedSaveOptions();
{
options.setSaveFormat(SaveFormat.HTML_FIXED);
options.setExportEmbeddedImages(false);
options.setResourcesFolder(getArtifactsDir() + "HtmlFixedResourceFolder");
options.setResourcesFolderAlias(getArtifactsDir() + "HtmlFixedResourceFolderAlias");
options.setShowPageBorder(false);
options.setResourceSavingCallback(callback);
}
// A folder specified by ResourcesFolderAlias will contain the resources instead of ResourcesFolder.
// We must ensure the folder exists before the streams can put their resources into it.
new File(options.getResourcesFolderAlias()).mkdir();
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.HtmlFixedResourceFolder.html", options);
System.out.println(callback.getText());
String[] resourceFiles = new File(getArtifactsDir() + "HtmlFixedResourceFolderAlias").list();
Assert.assertFalse(new File(getArtifactsDir() + "HtmlFixedResourceFolder").exists());
Assert.assertEquals(6, IterableUtils.countMatches(Arrays.asList(resourceFiles),
f -> f.endsWith(".jpeg") || f.endsWith(".png") || f.endsWith(".css")));
}
/// <summary>
/// Counts and prints URIs of resources contained by as they are converted to fixed HTML.
/// </summary>
private static class ResourceUriPrinter implements IResourceSavingCallback {
public void resourceSaving(ResourceSavingArgs args) throws Exception {
// If we set a folder alias in the SaveOptions object, we will be able to print it from here.
mText.append(MessageFormat.format("Resource #{0} \"{1}\"", ++mSavedResourceCount, args.getResourceFileName()));
String extension = FilenameUtils.getExtension(args.getResourceFileName());
switch (extension) {
case "ttf":
case "woff": {
// By default, 'ResourceFileUri' uses system folder for fonts.
// To avoid problems in other platforms you must explicitly specify the path for the fonts.
args.setResourceFileUri(getArtifactsDir() + File.separatorChar + args.getResourceFileName());
break;
}
}
mText.append("\t" + args.getResourceFileUri());
// If we have specified a folder in the "ResourcesFolderAlias" property,
// we will also need to redirect each stream to put its resource in that folder.
args.setResourceStream(new FileOutputStream(args.getResourceFileUri()));
args.setKeepResourceStreamOpen(false);
}
public String getText() {
return mText.toString();
}
private int mSavedResourceCount;
private final StringBuilder mText = new StringBuilder();
}
String value.getResourcesFolder(),
setResourcesFolder(java.lang.String)public void setResourcesFolderAlias(java.lang.String value)
null.
Remarks:
When you save a Document in Html format, Aspose.Words needs to save all images embedded in the document as standalone files. getResourcesFolder() / setResourcesFolder(java.lang.String) allows you to specify where the images will be saved and getResourcesFolderAlias() / setResourcesFolderAlias(java.lang.String) allows to specify how the image URIs will be constructed.
Examples:
Shows how to use a callback to print the URIs of external resources created while converting a document to HTML.
public void htmlFixedResourceFolder() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
ResourceUriPrinter callback = new ResourceUriPrinter();
HtmlFixedSaveOptions options = new HtmlFixedSaveOptions();
{
options.setSaveFormat(SaveFormat.HTML_FIXED);
options.setExportEmbeddedImages(false);
options.setResourcesFolder(getArtifactsDir() + "HtmlFixedResourceFolder");
options.setResourcesFolderAlias(getArtifactsDir() + "HtmlFixedResourceFolderAlias");
options.setShowPageBorder(false);
options.setResourceSavingCallback(callback);
}
// A folder specified by ResourcesFolderAlias will contain the resources instead of ResourcesFolder.
// We must ensure the folder exists before the streams can put their resources into it.
new File(options.getResourcesFolderAlias()).mkdir();
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.HtmlFixedResourceFolder.html", options);
System.out.println(callback.getText());
String[] resourceFiles = new File(getArtifactsDir() + "HtmlFixedResourceFolderAlias").list();
Assert.assertFalse(new File(getArtifactsDir() + "HtmlFixedResourceFolder").exists());
Assert.assertEquals(6, IterableUtils.countMatches(Arrays.asList(resourceFiles),
f -> f.endsWith(".jpeg") || f.endsWith(".png") || f.endsWith(".css")));
}
/// <summary>
/// Counts and prints URIs of resources contained by as they are converted to fixed HTML.
/// </summary>
private static class ResourceUriPrinter implements IResourceSavingCallback {
public void resourceSaving(ResourceSavingArgs args) throws Exception {
// If we set a folder alias in the SaveOptions object, we will be able to print it from here.
mText.append(MessageFormat.format("Resource #{0} \"{1}\"", ++mSavedResourceCount, args.getResourceFileName()));
String extension = FilenameUtils.getExtension(args.getResourceFileName());
switch (extension) {
case "ttf":
case "woff": {
// By default, 'ResourceFileUri' uses system folder for fonts.
// To avoid problems in other platforms you must explicitly specify the path for the fonts.
args.setResourceFileUri(getArtifactsDir() + File.separatorChar + args.getResourceFileName());
break;
}
}
mText.append("\t" + args.getResourceFileUri());
// If we have specified a folder in the "ResourcesFolderAlias" property,
// we will also need to redirect each stream to put its resource in that folder.
args.setResourceStream(new FileOutputStream(args.getResourceFileUri()));
args.setKeepResourceStreamOpen(false);
}
public String getText() {
return mText.toString();
}
private int mSavedResourceCount;
private final StringBuilder mText = new StringBuilder();
}
value - The corresponding String value.getResourcesFolder(),
setResourcesFolder(java.lang.String)public boolean getExportEmbeddedImages()
Examples:
Shows how to determine where to store images when exporting a document to Html.
Document doc = new Document(getMyDir() + "Images.docx");
// When we export a document with embedded images to .html,
// Aspose.Words can place the images in two possible locations.
// Setting the "ExportEmbeddedImages" flag to "true" will store the raw data
// for all images within the output HTML document, in the "src" attribute of <image> tags.
// Setting this flag to "false" will create an image file in the local file system for every image,
// and store all these files in a separate folder.
HtmlFixedSaveOptions htmlFixedSaveOptions = new HtmlFixedSaveOptions();
{
htmlFixedSaveOptions.setExportEmbeddedImages(exportImages);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedImages.html", htmlFixedSaveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedImages.html"), StandardCharsets.UTF_8);
if (exportImages)
{
Assert.assertFalse(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedImages/image001.jpeg").exists());
Assert.assertTrue(Pattern.compile("<img class=\"awimg\" style=\"left:0pt; top:0pt; width:493.1pt; height:300.55pt;\" src=\".+\" />").matcher(outDocContents).find());
}
else
{
Assert.assertTrue(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedImages/image001.jpeg").exists());
Assert.assertTrue(Pattern.compile("<img class=\"awimg\" style=\"left:0pt; top:0pt; width:493.1pt; height:300.55pt;\" " +
"src=\"HtmlFixedSaveOptions[.]ExportEmbeddedImages/image001[.]jpeg\" />").matcher(outDocContents).find());
}
boolean value.public void setExportEmbeddedImages(boolean value)
Examples:
Shows how to determine where to store images when exporting a document to Html.
Document doc = new Document(getMyDir() + "Images.docx");
// When we export a document with embedded images to .html,
// Aspose.Words can place the images in two possible locations.
// Setting the "ExportEmbeddedImages" flag to "true" will store the raw data
// for all images within the output HTML document, in the "src" attribute of <image> tags.
// Setting this flag to "false" will create an image file in the local file system for every image,
// and store all these files in a separate folder.
HtmlFixedSaveOptions htmlFixedSaveOptions = new HtmlFixedSaveOptions();
{
htmlFixedSaveOptions.setExportEmbeddedImages(exportImages);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedImages.html", htmlFixedSaveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedImages.html"), StandardCharsets.UTF_8);
if (exportImages)
{
Assert.assertFalse(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedImages/image001.jpeg").exists());
Assert.assertTrue(Pattern.compile("<img class=\"awimg\" style=\"left:0pt; top:0pt; width:493.1pt; height:300.55pt;\" src=\".+\" />").matcher(outDocContents).find());
}
else
{
Assert.assertTrue(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedImages/image001.jpeg").exists());
Assert.assertTrue(Pattern.compile("<img class=\"awimg\" style=\"left:0pt; top:0pt; width:493.1pt; height:300.55pt;\" " +
"src=\"HtmlFixedSaveOptions[.]ExportEmbeddedImages/image001[.]jpeg\" />").matcher(outDocContents).find());
}
value - The corresponding boolean value.public boolean getExportEmbeddedFonts()
Examples:
Shows how to determine where to store embedded fonts when exporting a document to Html.
Document doc = new Document(getMyDir() + "Embedded font.docx");
// When we export a document with embedded fonts to .html,
// Aspose.Words can place the fonts in two possible locations.
// Setting the "ExportEmbeddedFonts" flag to "true" will store the raw data for embedded fonts within the CSS stylesheet,
// in the "url" property of the "@font-face" rule. This may create a huge CSS stylesheet file
// and reduce the number of external files that this HTML conversion will create.
// Setting this flag to "false" will create a file for each font.
// The CSS stylesheet will link to each font file using the "url" property of the "@font-face" rule.
HtmlFixedSaveOptions htmlFixedSaveOptions = new HtmlFixedSaveOptions();
{
htmlFixedSaveOptions.setExportEmbeddedFonts(exportEmbeddedFonts);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedFonts.html", htmlFixedSaveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedFonts/styles.css"), StandardCharsets.UTF_8);
if (exportEmbeddedFonts)
{
Assert.assertTrue(Pattern.compile("@font-face [{] font-family:'Arial'; font-style:normal; font-weight:normal; src:local[(]'☺'[)], url[(].+[)] format[(]'woff'[)]; [}]").matcher(outDocContents).find());
Assert.assertEquals(0, DocumentHelper.directoryGetFiles(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedFonts", "*.woff").size());
}
else
{
Assert.assertTrue(Pattern.compile("@font-face [{] font-family:'Arial'; font-style:normal; font-weight:normal; src:local[(]'☺'[)], url[(]'font001[.]woff'[)] format[(]'woff'[)]; [}]").matcher(outDocContents).find());
Assert.assertEquals(2, DocumentHelper.directoryGetFiles(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedFonts", "*.woff").size());
}
boolean value.public void setExportEmbeddedFonts(boolean value)
Examples:
Shows how to determine where to store embedded fonts when exporting a document to Html.
Document doc = new Document(getMyDir() + "Embedded font.docx");
// When we export a document with embedded fonts to .html,
// Aspose.Words can place the fonts in two possible locations.
// Setting the "ExportEmbeddedFonts" flag to "true" will store the raw data for embedded fonts within the CSS stylesheet,
// in the "url" property of the "@font-face" rule. This may create a huge CSS stylesheet file
// and reduce the number of external files that this HTML conversion will create.
// Setting this flag to "false" will create a file for each font.
// The CSS stylesheet will link to each font file using the "url" property of the "@font-face" rule.
HtmlFixedSaveOptions htmlFixedSaveOptions = new HtmlFixedSaveOptions();
{
htmlFixedSaveOptions.setExportEmbeddedFonts(exportEmbeddedFonts);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedFonts.html", htmlFixedSaveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedFonts/styles.css"), StandardCharsets.UTF_8);
if (exportEmbeddedFonts)
{
Assert.assertTrue(Pattern.compile("@font-face [{] font-family:'Arial'; font-style:normal; font-weight:normal; src:local[(]'☺'[)], url[(].+[)] format[(]'woff'[)]; [}]").matcher(outDocContents).find());
Assert.assertEquals(0, DocumentHelper.directoryGetFiles(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedFonts", "*.woff").size());
}
else
{
Assert.assertTrue(Pattern.compile("@font-face [{] font-family:'Arial'; font-style:normal; font-weight:normal; src:local[(]'☺'[)], url[(]'font001[.]woff'[)] format[(]'woff'[)]; [}]").matcher(outDocContents).find());
Assert.assertEquals(2, DocumentHelper.directoryGetFiles(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedFonts", "*.woff").size());
}
value - The corresponding boolean value.public boolean getExportEmbeddedCss()
Examples:
Shows how to determine where to store CSS stylesheets when exporting a document to Html.
Document doc = new Document(getMyDir() + "Rendering.docx");
// When we export a document to html, Aspose.Words will also create a CSS stylesheet to format the document with.
// Setting the "ExportEmbeddedCss" flag to "true" save the CSS stylesheet to a .css file,
// and link to the file from the html document using a <link> element.
// Setting the flag to "false" will embed the CSS stylesheet within the Html document,
// which will create only one file instead of two.
HtmlFixedSaveOptions htmlFixedSaveOptions = new HtmlFixedSaveOptions();
{
htmlFixedSaveOptions.setExportEmbeddedCss(exportEmbeddedCss);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedCss.html", htmlFixedSaveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedCss.html"), "utf-8");
if (exportEmbeddedCss) {
Assert.assertTrue(outDocContents.contains("<style type=\"text/css\">"));
Assert.assertFalse(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedCss/styles.css").exists());
} else {
Assert.assertTrue(outDocContents.contains("<link rel=\"stylesheet\" type=\"text/css\" href=\"HtmlFixedSaveOptions.ExportEmbeddedCss/styles.css\" media=\"all\" />"));
Assert.assertTrue(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedCss/styles.css").exists());
}
boolean value.public void setExportEmbeddedCss(boolean value)
Examples:
Shows how to determine where to store CSS stylesheets when exporting a document to Html.
Document doc = new Document(getMyDir() + "Rendering.docx");
// When we export a document to html, Aspose.Words will also create a CSS stylesheet to format the document with.
// Setting the "ExportEmbeddedCss" flag to "true" save the CSS stylesheet to a .css file,
// and link to the file from the html document using a <link> element.
// Setting the flag to "false" will embed the CSS stylesheet within the Html document,
// which will create only one file instead of two.
HtmlFixedSaveOptions htmlFixedSaveOptions = new HtmlFixedSaveOptions();
{
htmlFixedSaveOptions.setExportEmbeddedCss(exportEmbeddedCss);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedCss.html", htmlFixedSaveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedCss.html"), "utf-8");
if (exportEmbeddedCss) {
Assert.assertTrue(outDocContents.contains("<style type=\"text/css\">"));
Assert.assertFalse(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedCss/styles.css").exists());
} else {
Assert.assertTrue(outDocContents.contains("<link rel=\"stylesheet\" type=\"text/css\" href=\"HtmlFixedSaveOptions.ExportEmbeddedCss/styles.css\" media=\"all\" />"));
Assert.assertTrue(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedCss/styles.css").exists());
}
value - The corresponding boolean value.public boolean getExportEmbeddedSvg()
true.
Examples:
Shows how to determine where to store SVG objects when exporting a document to Html.
Document doc = new Document(getMyDir() + "Images.docx");
// When we export a document with SVG objects to .html,
// Aspose.Words can place these objects in two possible locations.
// Setting the "ExportEmbeddedSvg" flag to "true" will embed all SVG object raw data
// within the output HTML, inside <image> tags.
// Setting this flag to "false" will create a file in the local file system for each SVG object.
// The HTML will link to each file using the "data" attribute of an <object> tag.
HtmlFixedSaveOptions htmlFixedSaveOptions = new HtmlFixedSaveOptions();
{
htmlFixedSaveOptions.setExportEmbeddedSvg(exportSvgs);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedSvgs.html", htmlFixedSaveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedSvgs.html"), StandardCharsets.UTF_8);
if (exportSvgs) {
Assert.assertFalse(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedSvgs/svg001.svg").exists());
Assert.assertTrue(Pattern.compile("<image id=\"image004\" xlink:href=.+/>").matcher(outDocContents).find());
} else {
Assert.assertTrue(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedSvgs/svg001.svg").exists());
Assert.assertTrue(Pattern.compile("<object type=\"image/svg[+]xml\" data=\"HtmlFixedSaveOptions.ExportEmbeddedSvgs/svg001[.]svg\"></object>").matcher(outDocContents).find());
}
boolean value.public void setExportEmbeddedSvg(boolean value)
true.
Examples:
Shows how to determine where to store SVG objects when exporting a document to Html.
Document doc = new Document(getMyDir() + "Images.docx");
// When we export a document with SVG objects to .html,
// Aspose.Words can place these objects in two possible locations.
// Setting the "ExportEmbeddedSvg" flag to "true" will embed all SVG object raw data
// within the output HTML, inside <image> tags.
// Setting this flag to "false" will create a file in the local file system for each SVG object.
// The HTML will link to each file using the "data" attribute of an <object> tag.
HtmlFixedSaveOptions htmlFixedSaveOptions = new HtmlFixedSaveOptions();
{
htmlFixedSaveOptions.setExportEmbeddedSvg(exportSvgs);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedSvgs.html", htmlFixedSaveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedSvgs.html"), StandardCharsets.UTF_8);
if (exportSvgs) {
Assert.assertFalse(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedSvgs/svg001.svg").exists());
Assert.assertTrue(Pattern.compile("<image id=\"image004\" xlink:href=.+/>").matcher(outDocContents).find());
} else {
Assert.assertTrue(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportEmbeddedSvgs/svg001.svg").exists());
Assert.assertTrue(Pattern.compile("<object type=\"image/svg[+]xml\" data=\"HtmlFixedSaveOptions.ExportEmbeddedSvgs/svg001[.]svg\"></object>").matcher(outDocContents).find());
}
value - The corresponding boolean value.public int getFontFormat()
ExportFontFormat used for font exporting. Default value is ExportFontFormat.WOFF.
Examples:
Shows how use fonts only from the target machine when saving a document to HTML.
Document doc = new Document(getMyDir() + "Bullet points with alternative font.docx");
HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions();
{
saveOptions.setExportEmbeddedCss(true);
saveOptions.setUseTargetMachineFonts(useTargetMachineFonts);
saveOptions.setFontFormat(ExportFontFormat.TTF);
saveOptions.setExportEmbeddedFonts(false);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.UsingMachineFonts.html", saveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.UsingMachineFonts.html"), StandardCharsets.UTF_8);
if (useTargetMachineFonts)
Assert.assertFalse(Pattern.compile("@font-face").matcher(outDocContents).find());
else
Assert.assertTrue(Pattern.compile(
"@font-face [{] font-family:'Arial'; font-style:normal; font-weight:normal; src:local[(]'☺'[)], " +
"url[(]'HtmlFixedSaveOptions.UsingMachineFonts/font001.ttf'[)] format[(]'truetype'[)]; [}]").matcher(outDocContents).find());
ExportFontFormat used for font exporting. The returned value is one of ExportFontFormat constants.public void setFontFormat(int value)
ExportFontFormat used for font exporting. Default value is ExportFontFormat.WOFF.
Examples:
Shows how use fonts only from the target machine when saving a document to HTML.
Document doc = new Document(getMyDir() + "Bullet points with alternative font.docx");
HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions();
{
saveOptions.setExportEmbeddedCss(true);
saveOptions.setUseTargetMachineFonts(useTargetMachineFonts);
saveOptions.setFontFormat(ExportFontFormat.TTF);
saveOptions.setExportEmbeddedFonts(false);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.UsingMachineFonts.html", saveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.UsingMachineFonts.html"), StandardCharsets.UTF_8);
if (useTargetMachineFonts)
Assert.assertFalse(Pattern.compile("@font-face").matcher(outDocContents).find());
else
Assert.assertTrue(Pattern.compile(
"@font-face [{] font-family:'Arial'; font-style:normal; font-weight:normal; src:local[(]'☺'[)], " +
"url[(]'HtmlFixedSaveOptions.UsingMachineFonts/font001.ttf'[)] format[(]'truetype'[)]; [}]").matcher(outDocContents).find());
value - ExportFontFormat used for font exporting. The value must be one of ExportFontFormat constants.public java.lang.String getCssClassNamesPrefix()
"aw".
Examples:
Shows how to place CSS into a separate file and add a prefix to all of its CSS class names.
Document doc = new Document(getMyDir() + "Bookmarks.docx");
HtmlFixedSaveOptions htmlFixedSaveOptions = new HtmlFixedSaveOptions();
{
htmlFixedSaveOptions.setCssClassNamesPrefix("myprefix");
htmlFixedSaveOptions.setSaveFontFaceCssSeparately(true);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.AddCssClassNamesPrefix.html", htmlFixedSaveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.AddCssClassNamesPrefix.html"), StandardCharsets.UTF_8);
Assert.assertTrue(Pattern.compile(
"<div class=\"myprefixdiv myprefixpage\" style=\"width:595[.]3pt; height:841[.]9pt;\">" +
"<div class=\"myprefixdiv\" style=\"left:85[.]05pt; top:36pt; clip:rect[(]0pt,510[.]25pt,74[.]95pt,-85.05pt[)];\">" +
"<span class=\"myprefixspan myprefixtext001\" style=\"font-size:11pt; left:294[.]73pt; top:0[.]36pt; line-height:12[.]29pt;\">").matcher(outDocContents).find());
outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.AddCssClassNamesPrefix/styles.css"), StandardCharsets.UTF_8);
Assert.assertTrue(Pattern.compile(
".myprefixdiv [{] position:absolute; [}] " +
".myprefixspan [{] position:absolute; white-space:pre; color:#000000; font-size:12pt; [}]").matcher(outDocContents).find());
String value.public void setCssClassNamesPrefix(java.lang.String value)
"aw".
Examples:
Shows how to place CSS into a separate file and add a prefix to all of its CSS class names.
Document doc = new Document(getMyDir() + "Bookmarks.docx");
HtmlFixedSaveOptions htmlFixedSaveOptions = new HtmlFixedSaveOptions();
{
htmlFixedSaveOptions.setCssClassNamesPrefix("myprefix");
htmlFixedSaveOptions.setSaveFontFaceCssSeparately(true);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.AddCssClassNamesPrefix.html", htmlFixedSaveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.AddCssClassNamesPrefix.html"), StandardCharsets.UTF_8);
Assert.assertTrue(Pattern.compile(
"<div class=\"myprefixdiv myprefixpage\" style=\"width:595[.]3pt; height:841[.]9pt;\">" +
"<div class=\"myprefixdiv\" style=\"left:85[.]05pt; top:36pt; clip:rect[(]0pt,510[.]25pt,74[.]95pt,-85.05pt[)];\">" +
"<span class=\"myprefixspan myprefixtext001\" style=\"font-size:11pt; left:294[.]73pt; top:0[.]36pt; line-height:12[.]29pt;\">").matcher(outDocContents).find());
outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.AddCssClassNamesPrefix/styles.css"), StandardCharsets.UTF_8);
Assert.assertTrue(Pattern.compile(
".myprefixdiv [{] position:absolute; [}] " +
".myprefixspan [{] position:absolute; white-space:pre; color:#000000; font-size:12pt; [}]").matcher(outDocContents).find());
value - The corresponding String value.public IResourceSavingCallback getResourceSavingCallback()
Examples:
Shows how to use a callback to print the URIs of external resources created while converting a document to HTML.
public void htmlFixedResourceFolder() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
ResourceUriPrinter callback = new ResourceUriPrinter();
HtmlFixedSaveOptions options = new HtmlFixedSaveOptions();
{
options.setSaveFormat(SaveFormat.HTML_FIXED);
options.setExportEmbeddedImages(false);
options.setResourcesFolder(getArtifactsDir() + "HtmlFixedResourceFolder");
options.setResourcesFolderAlias(getArtifactsDir() + "HtmlFixedResourceFolderAlias");
options.setShowPageBorder(false);
options.setResourceSavingCallback(callback);
}
// A folder specified by ResourcesFolderAlias will contain the resources instead of ResourcesFolder.
// We must ensure the folder exists before the streams can put their resources into it.
new File(options.getResourcesFolderAlias()).mkdir();
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.HtmlFixedResourceFolder.html", options);
System.out.println(callback.getText());
String[] resourceFiles = new File(getArtifactsDir() + "HtmlFixedResourceFolderAlias").list();
Assert.assertFalse(new File(getArtifactsDir() + "HtmlFixedResourceFolder").exists());
Assert.assertEquals(6, IterableUtils.countMatches(Arrays.asList(resourceFiles),
f -> f.endsWith(".jpeg") || f.endsWith(".png") || f.endsWith(".css")));
}
/// <summary>
/// Counts and prints URIs of resources contained by as they are converted to fixed HTML.
/// </summary>
private static class ResourceUriPrinter implements IResourceSavingCallback {
public void resourceSaving(ResourceSavingArgs args) throws Exception {
// If we set a folder alias in the SaveOptions object, we will be able to print it from here.
mText.append(MessageFormat.format("Resource #{0} \"{1}\"", ++mSavedResourceCount, args.getResourceFileName()));
String extension = FilenameUtils.getExtension(args.getResourceFileName());
switch (extension) {
case "ttf":
case "woff": {
// By default, 'ResourceFileUri' uses system folder for fonts.
// To avoid problems in other platforms you must explicitly specify the path for the fonts.
args.setResourceFileUri(getArtifactsDir() + File.separatorChar + args.getResourceFileName());
break;
}
}
mText.append("\t" + args.getResourceFileUri());
// If we have specified a folder in the "ResourcesFolderAlias" property,
// we will also need to redirect each stream to put its resource in that folder.
args.setResourceStream(new FileOutputStream(args.getResourceFileUri()));
args.setKeepResourceStreamOpen(false);
}
public String getText() {
return mText.toString();
}
private int mSavedResourceCount;
private final StringBuilder mText = new StringBuilder();
}
IResourceSavingCallback value.public void setResourceSavingCallback(IResourceSavingCallback value)
Examples:
Shows how to use a callback to print the URIs of external resources created while converting a document to HTML.
public void htmlFixedResourceFolder() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
ResourceUriPrinter callback = new ResourceUriPrinter();
HtmlFixedSaveOptions options = new HtmlFixedSaveOptions();
{
options.setSaveFormat(SaveFormat.HTML_FIXED);
options.setExportEmbeddedImages(false);
options.setResourcesFolder(getArtifactsDir() + "HtmlFixedResourceFolder");
options.setResourcesFolderAlias(getArtifactsDir() + "HtmlFixedResourceFolderAlias");
options.setShowPageBorder(false);
options.setResourceSavingCallback(callback);
}
// A folder specified by ResourcesFolderAlias will contain the resources instead of ResourcesFolder.
// We must ensure the folder exists before the streams can put their resources into it.
new File(options.getResourcesFolderAlias()).mkdir();
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.HtmlFixedResourceFolder.html", options);
System.out.println(callback.getText());
String[] resourceFiles = new File(getArtifactsDir() + "HtmlFixedResourceFolderAlias").list();
Assert.assertFalse(new File(getArtifactsDir() + "HtmlFixedResourceFolder").exists());
Assert.assertEquals(6, IterableUtils.countMatches(Arrays.asList(resourceFiles),
f -> f.endsWith(".jpeg") || f.endsWith(".png") || f.endsWith(".css")));
}
/// <summary>
/// Counts and prints URIs of resources contained by as they are converted to fixed HTML.
/// </summary>
private static class ResourceUriPrinter implements IResourceSavingCallback {
public void resourceSaving(ResourceSavingArgs args) throws Exception {
// If we set a folder alias in the SaveOptions object, we will be able to print it from here.
mText.append(MessageFormat.format("Resource #{0} \"{1}\"", ++mSavedResourceCount, args.getResourceFileName()));
String extension = FilenameUtils.getExtension(args.getResourceFileName());
switch (extension) {
case "ttf":
case "woff": {
// By default, 'ResourceFileUri' uses system folder for fonts.
// To avoid problems in other platforms you must explicitly specify the path for the fonts.
args.setResourceFileUri(getArtifactsDir() + File.separatorChar + args.getResourceFileName());
break;
}
}
mText.append("\t" + args.getResourceFileUri());
// If we have specified a folder in the "ResourcesFolderAlias" property,
// we will also need to redirect each stream to put its resource in that folder.
args.setResourceStream(new FileOutputStream(args.getResourceFileUri()));
args.setKeepResourceStreamOpen(false);
}
public String getText() {
return mText.toString();
}
private int mSavedResourceCount;
private final StringBuilder mText = new StringBuilder();
}
value - The corresponding IResourceSavingCallback value.public java.nio.charset.Charset getEncoding()
public void setEncoding(java.nio.charset.Charset value)
public boolean getExportFormFields()
Examples:
Shows how to export form fields to Html.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertCheckBox("CheckBox", false, 15);
// When we export a document with form fields to .html,
// there are two ways in which Aspose.Words can export form fields.
// Setting the "ExportFormFields" flag to "true" will export them as interactive objects.
// Setting this flag to "false" will display form fields as plain text.
// This will freeze them at their current value, and prevent the reader of our HTML document
// from being able to interact with them.
HtmlFixedSaveOptions htmlFixedSaveOptions = new HtmlFixedSaveOptions();
{
htmlFixedSaveOptions.setExportFormFields(exportFormFields);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.ExportFormFields.html", htmlFixedSaveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportFormFields.html"), StandardCharsets.UTF_8);
if (exportFormFields)
{
Assert.assertTrue(Pattern.compile(
"<a name=\"CheckBox\" style=\"left:0pt; top:0pt;\"></a>" +
"<input style=\"position:absolute; left:0pt; top:0pt;\" type=\"checkbox\" name=\"CheckBox\" />").matcher(outDocContents).find());
}
else
{
Assert.assertTrue(Pattern.compile(
"<a name=\"CheckBox\" style=\"left:0pt; top:0pt;\"></a>" +
"<div class=\"awdiv\" style=\"left:0.8pt; top:0.8pt; width:14.25pt; height:14.25pt; border:solid 0.75pt #000000;\"").matcher(outDocContents).find());
}
public void setExportFormFields(boolean value)
Examples:
Shows how to export form fields to Html.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertCheckBox("CheckBox", false, 15);
// When we export a document with form fields to .html,
// there are two ways in which Aspose.Words can export form fields.
// Setting the "ExportFormFields" flag to "true" will export them as interactive objects.
// Setting this flag to "false" will display form fields as plain text.
// This will freeze them at their current value, and prevent the reader of our HTML document
// from being able to interact with them.
HtmlFixedSaveOptions htmlFixedSaveOptions = new HtmlFixedSaveOptions();
{
htmlFixedSaveOptions.setExportFormFields(exportFormFields);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.ExportFormFields.html", htmlFixedSaveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.ExportFormFields.html"), StandardCharsets.UTF_8);
if (exportFormFields)
{
Assert.assertTrue(Pattern.compile(
"<a name=\"CheckBox\" style=\"left:0pt; top:0pt;\"></a>" +
"<input style=\"position:absolute; left:0pt; top:0pt;\" type=\"checkbox\" name=\"CheckBox\" />").matcher(outDocContents).find());
}
else
{
Assert.assertTrue(Pattern.compile(
"<a name=\"CheckBox\" style=\"left:0pt; top:0pt;\"></a>" +
"<div class=\"awdiv\" style=\"left:0.8pt; top:0.8pt; width:14.25pt; height:14.25pt; border:solid 0.75pt #000000;\"").matcher(outDocContents).find());
}
value - Indication of whether form fields are exported as interactive items (as 'input' tag) rather than converted to text or graphics.public boolean getOptimizeOutput()
true. Default is true.
Examples:
Shows how to simplify a document when saving it to HTML by removing various redundant objects.
Document doc = new Document(getMyDir() + "Rendering.docx");
HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions();
{
saveOptions.setOptimizeOutput(optimizeOutput);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.OptimizeGraphicsOutput.html", saveOptions);
// The size of the optimized version of the document is almost a third of the size of the unoptimized document.
if (optimizeOutput)
Assert.assertEquals(60385.0,
new File(getArtifactsDir() + "HtmlFixedSaveOptions.OptimizeGraphicsOutput.html").length(), 200.0);
else
Assert.assertEquals(191000.0,
new File(getArtifactsDir() + "HtmlFixedSaveOptions.OptimizeGraphicsOutput.html").length(), 200.0);
getOptimizeOutput in class FixedPageSaveOptionsboolean value.public void setOptimizeOutput(boolean value)
true. Default is true.
Examples:
Shows how to simplify a document when saving it to HTML by removing various redundant objects.
Document doc = new Document(getMyDir() + "Rendering.docx");
HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions();
{
saveOptions.setOptimizeOutput(optimizeOutput);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.OptimizeGraphicsOutput.html", saveOptions);
// The size of the optimized version of the document is almost a third of the size of the unoptimized document.
if (optimizeOutput)
Assert.assertEquals(60385.0,
new File(getArtifactsDir() + "HtmlFixedSaveOptions.OptimizeGraphicsOutput.html").length(), 200.0);
else
Assert.assertEquals(191000.0,
new File(getArtifactsDir() + "HtmlFixedSaveOptions.OptimizeGraphicsOutput.html").length(), 200.0);
setOptimizeOutput in class FixedPageSaveOptionsvalue - The corresponding boolean value.public boolean getUseTargetMachineFonts()
true, getFontFormat() / setFontFormat(int) and getExportEmbeddedFonts() / setExportEmbeddedFonts(boolean) properties do not have effect, also getResourceSavingCallback() / setResourceSavingCallback(com.aspose.words.IResourceSavingCallback) is not fired for fonts. Default is false.
Examples:
Shows how use fonts only from the target machine when saving a document to HTML.
Document doc = new Document(getMyDir() + "Bullet points with alternative font.docx");
HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions();
{
saveOptions.setExportEmbeddedCss(true);
saveOptions.setUseTargetMachineFonts(useTargetMachineFonts);
saveOptions.setFontFormat(ExportFontFormat.TTF);
saveOptions.setExportEmbeddedFonts(false);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.UsingMachineFonts.html", saveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.UsingMachineFonts.html"), StandardCharsets.UTF_8);
if (useTargetMachineFonts)
Assert.assertFalse(Pattern.compile("@font-face").matcher(outDocContents).find());
else
Assert.assertTrue(Pattern.compile(
"@font-face [{] font-family:'Arial'; font-style:normal; font-weight:normal; src:local[(]'☺'[)], " +
"url[(]'HtmlFixedSaveOptions.UsingMachineFonts/font001.ttf'[)] format[(]'truetype'[)]; [}]").matcher(outDocContents).find());
boolean value.public void setUseTargetMachineFonts(boolean value)
true, getFontFormat() / setFontFormat(int) and getExportEmbeddedFonts() / setExportEmbeddedFonts(boolean) properties do not have effect, also getResourceSavingCallback() / setResourceSavingCallback(com.aspose.words.IResourceSavingCallback) is not fired for fonts. Default is false.
Examples:
Shows how use fonts only from the target machine when saving a document to HTML.
Document doc = new Document(getMyDir() + "Bullet points with alternative font.docx");
HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions();
{
saveOptions.setExportEmbeddedCss(true);
saveOptions.setUseTargetMachineFonts(useTargetMachineFonts);
saveOptions.setFontFormat(ExportFontFormat.TTF);
saveOptions.setExportEmbeddedFonts(false);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.UsingMachineFonts.html", saveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.UsingMachineFonts.html"), StandardCharsets.UTF_8);
if (useTargetMachineFonts)
Assert.assertFalse(Pattern.compile("@font-face").matcher(outDocContents).find());
else
Assert.assertTrue(Pattern.compile(
"@font-face [{] font-family:'Arial'; font-style:normal; font-weight:normal; src:local[(]'☺'[)], " +
"url[(]'HtmlFixedSaveOptions.UsingMachineFonts/font001.ttf'[)] format[(]'truetype'[)]; [}]").matcher(outDocContents).find());
value - The corresponding boolean value.public boolean getSaveFontFaceCssSeparately()
getExportEmbeddedCss() / setExportEmbeddedCss(boolean) is false). Default value is false, all CSS rules are written into single file "styles.css".
Remarks:
Setting this property to true restores the old behavior (separate files) for compatibility with legacy code.
Examples:
Shows how to place CSS into a separate file and add a prefix to all of its CSS class names.
Document doc = new Document(getMyDir() + "Bookmarks.docx");
HtmlFixedSaveOptions htmlFixedSaveOptions = new HtmlFixedSaveOptions();
{
htmlFixedSaveOptions.setCssClassNamesPrefix("myprefix");
htmlFixedSaveOptions.setSaveFontFaceCssSeparately(true);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.AddCssClassNamesPrefix.html", htmlFixedSaveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.AddCssClassNamesPrefix.html"), StandardCharsets.UTF_8);
Assert.assertTrue(Pattern.compile(
"<div class=\"myprefixdiv myprefixpage\" style=\"width:595[.]3pt; height:841[.]9pt;\">" +
"<div class=\"myprefixdiv\" style=\"left:85[.]05pt; top:36pt; clip:rect[(]0pt,510[.]25pt,74[.]95pt,-85.05pt[)];\">" +
"<span class=\"myprefixspan myprefixtext001\" style=\"font-size:11pt; left:294[.]73pt; top:0[.]36pt; line-height:12[.]29pt;\">").matcher(outDocContents).find());
outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.AddCssClassNamesPrefix/styles.css"), StandardCharsets.UTF_8);
Assert.assertTrue(Pattern.compile(
".myprefixdiv [{] position:absolute; [}] " +
".myprefixspan [{] position:absolute; white-space:pre; color:#000000; font-size:12pt; [}]").matcher(outDocContents).find());
boolean value.public void setSaveFontFaceCssSeparately(boolean value)
getExportEmbeddedCss() / setExportEmbeddedCss(boolean) is false). Default value is false, all CSS rules are written into single file "styles.css".
Remarks:
Setting this property to true restores the old behavior (separate files) for compatibility with legacy code.
Examples:
Shows how to place CSS into a separate file and add a prefix to all of its CSS class names.
Document doc = new Document(getMyDir() + "Bookmarks.docx");
HtmlFixedSaveOptions htmlFixedSaveOptions = new HtmlFixedSaveOptions();
{
htmlFixedSaveOptions.setCssClassNamesPrefix("myprefix");
htmlFixedSaveOptions.setSaveFontFaceCssSeparately(true);
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.AddCssClassNamesPrefix.html", htmlFixedSaveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.AddCssClassNamesPrefix.html"), StandardCharsets.UTF_8);
Assert.assertTrue(Pattern.compile(
"<div class=\"myprefixdiv myprefixpage\" style=\"width:595[.]3pt; height:841[.]9pt;\">" +
"<div class=\"myprefixdiv\" style=\"left:85[.]05pt; top:36pt; clip:rect[(]0pt,510[.]25pt,74[.]95pt,-85.05pt[)];\">" +
"<span class=\"myprefixspan myprefixtext001\" style=\"font-size:11pt; left:294[.]73pt; top:0[.]36pt; line-height:12[.]29pt;\">").matcher(outDocContents).find());
outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.AddCssClassNamesPrefix/styles.css"), StandardCharsets.UTF_8);
Assert.assertTrue(Pattern.compile(
".myprefixdiv [{] position:absolute; [}] " +
".myprefixspan [{] position:absolute; white-space:pre; color:#000000; font-size:12pt; [}]").matcher(outDocContents).find());
value - The corresponding boolean value.public java.lang.String getIdPrefix()
String value.java.lang.IllegalArgumentException - The value does not meet the requirements specified above.
Remarks:
If the prefix is specified, it can contain only letters, digits, underscores, and hyphens, and must start with a letter.
Examples:
Shows how to add a prefix that is prepended to all generated element IDs.
Document doc = new Document(getMyDir() + "Id prefix.docx");
HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions();
saveOptions.setIdPrefix("pfx1_");
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.IdPrefix.html", saveOptions);
public void setIdPrefix(java.lang.String value)
value - The corresponding String value.java.lang.IllegalArgumentException - The value does not meet the requirements specified above.
Remarks:
If the prefix is specified, it can contain only letters, digits, underscores, and hyphens, and must start with a letter.
Examples:
Shows how to add a prefix that is prepended to all generated element IDs.
Document doc = new Document(getMyDir() + "Id prefix.docx");
HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions();
saveOptions.setIdPrefix("pfx1_");
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.IdPrefix.html", saveOptions);
public boolean getRemoveJavaScriptFromLinks()
false.
Remarks:
If this option is enabled, all links containing JavaScript (e.g., links with "javascript:" in the href attribute) will be replaced with "javascript:void(0)". This can help prevent potential security risks, such as XSS attacks.
Examples:
Shows how to remove JavaScript from the links.
Document doc = new Document(getMyDir() + "JavaScript in HREF.docx");
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.setRemoveJavaScriptFromLinks(true);
doc.save(getArtifactsDir() + "HtmlSaveOptions.RemoveJavaScriptFromLinks.html", saveOptions);
Shows how to remove JavaScript from the links for html fixed documents.
Document doc = new Document(getMyDir() + "JavaScript in HREF.docx");
HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions();
saveOptions.setRemoveJavaScriptFromLinks(true);
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.RemoveJavaScriptFromLinks.html", saveOptions);
boolean value.public void setRemoveJavaScriptFromLinks(boolean value)
false.
Remarks:
If this option is enabled, all links containing JavaScript (e.g., links with "javascript:" in the href attribute) will be replaced with "javascript:void(0)". This can help prevent potential security risks, such as XSS attacks.
Examples:
Shows how to remove JavaScript from the links.
Document doc = new Document(getMyDir() + "JavaScript in HREF.docx");
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.setRemoveJavaScriptFromLinks(true);
doc.save(getArtifactsDir() + "HtmlSaveOptions.RemoveJavaScriptFromLinks.html", saveOptions);
Shows how to remove JavaScript from the links for html fixed documents.
Document doc = new Document(getMyDir() + "JavaScript in HREF.docx");
HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions();
saveOptions.setRemoveJavaScriptFromLinks(true);
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.RemoveJavaScriptFromLinks.html", saveOptions);
value - The corresponding boolean value.