public class ResourceSavingArgs
By default, when Aspose.Words saves a document to fixed page HTML or SVG, it saves each resource into
a separate file. Aspose.Words uses the document file name and a unique number to generate unique file name
for each resource found in the document. To apply your own logic for generating resource file names use the
To save resources into streams instead of files, use the Example:
public void usingMachineFonts() throws Exception {
Document doc = new Document(getMyDir() + "Bullet points with alternative font.docx");
HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions();
{
saveOptions.setExportEmbeddedCss(true);
saveOptions.setUseTargetMachineFonts(true);
saveOptions.setFontFormat(ExportFontFormat.TTF);
saveOptions.setExportEmbeddedFonts(false);
saveOptions.setResourceSavingCallback(new ResourceSavingCallback());
}
doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.UsingMachineFonts.html", saveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.UsingMachineFonts.html"), "utf-8");
if (saveOptions.getUseTargetMachineFonts())
Assert.assertFalse(outDocContents.matches("@font-face"));
else
Assert.assertTrue(outDocContents.matches("@font-face * font-family:'Arial'; font-style:normal; font-weight:normal; src:local[(]'☺'[)], " +
"url[(]'HtmlFixedSaveOptions.UsingMachineFonts/font001.ttf'[)] format[(]'truetype'[)]; }"));
}
private static class ResourceSavingCallback implements IResourceSavingCallback {
/**
* Called when Aspose.Words saves an external resource to fixed page HTML or SVG.
*/
public void resourceSaving(final ResourceSavingArgs args) {
System.out.println(MessageFormat.format("Original document URI:\t{0}", args.getDocument().getOriginalFileName()));
System.out.println(MessageFormat.format("Resource being saved:\t{0}", args.getResourceFileName()));
System.out.println(MessageFormat.format("Full uri after saving:\t{0}", args.getResourceFileUri()));
args.setResourceStream(new ByteArrayOutputStream());
args.setKeepResourceStreamOpen(true);
String extension = FilenameUtils.getExtension(args.getResourceFileName());
switch (extension) {
case "ttf":
case "woff":
Assert.fail("'ResourceSavingCallback' is not fired for fonts when 'UseTargetMachineFonts' is true");
break;
}
}
}
Property Getters/Setters Summary | ||
---|---|---|
Document | getDocument() | |
Gets the document object that is currently being saved.
|
||
boolean | getKeepResourceStreamOpen() | |
void | setKeepResourceStreamOpen(booleanvalue) | |
Specifies whether Aspose.Words should keep the stream open or close it after saving a resource. | ||
java.lang.String | getResourceFileName() | |
void | setResourceFileName(java.lang.Stringvalue) | |
Gets or sets the file name (without path) where the resource will be saved to. | ||
java.lang.String | getResourceFileUri() | |
void | setResourceFileUri(java.lang.Stringvalue) | |
Gets or sets the uniform resource identifier (URI) used to reference the resource file from the document. | ||
java.io.OutputStream | getResourceStream() | |
void | setResourceStream(java.io.OutputStreamvalue) | |
Allows to specify the stream where the resource will be saved to. |
public Document getDocument()
Example:
Shows how used target machine fonts to display the document.public void usingMachineFonts() throws Exception { Document doc = new Document(getMyDir() + "Bullet points with alternative font.docx"); HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions(); { saveOptions.setExportEmbeddedCss(true); saveOptions.setUseTargetMachineFonts(true); saveOptions.setFontFormat(ExportFontFormat.TTF); saveOptions.setExportEmbeddedFonts(false); saveOptions.setResourceSavingCallback(new ResourceSavingCallback()); } doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.UsingMachineFonts.html", saveOptions); String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.UsingMachineFonts.html"), "utf-8"); if (saveOptions.getUseTargetMachineFonts()) Assert.assertFalse(outDocContents.matches("@font-face")); else Assert.assertTrue(outDocContents.matches("@font-face * font-family:'Arial'; font-style:normal; font-weight:normal; src:local[(]'☺'[)], " + "url[(]'HtmlFixedSaveOptions.UsingMachineFonts/font001.ttf'[)] format[(]'truetype'[)]; }")); } private static class ResourceSavingCallback implements IResourceSavingCallback { /** * Called when Aspose.Words saves an external resource to fixed page HTML or SVG. */ public void resourceSaving(final ResourceSavingArgs args) { System.out.println(MessageFormat.format("Original document URI:\t{0}", args.getDocument().getOriginalFileName())); System.out.println(MessageFormat.format("Resource being saved:\t{0}", args.getResourceFileName())); System.out.println(MessageFormat.format("Full uri after saving:\t{0}", args.getResourceFileUri())); args.setResourceStream(new ByteArrayOutputStream()); args.setKeepResourceStreamOpen(true); String extension = FilenameUtils.getExtension(args.getResourceFileName()); switch (extension) { case "ttf": case "woff": Assert.fail("'ResourceSavingCallback' is not fired for fonts when 'UseTargetMachineFonts' is true"); break; } } }
public boolean getKeepResourceStreamOpen() / public void setKeepResourceStreamOpen(boolean value)
Default is false
and Aspose.Words will close the stream you provided
in the true
to keep the stream open.
Example:
Shows how used target machine fonts to display the document.public void usingMachineFonts() throws Exception { Document doc = new Document(getMyDir() + "Bullet points with alternative font.docx"); HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions(); { saveOptions.setExportEmbeddedCss(true); saveOptions.setUseTargetMachineFonts(true); saveOptions.setFontFormat(ExportFontFormat.TTF); saveOptions.setExportEmbeddedFonts(false); saveOptions.setResourceSavingCallback(new ResourceSavingCallback()); } doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.UsingMachineFonts.html", saveOptions); String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.UsingMachineFonts.html"), "utf-8"); if (saveOptions.getUseTargetMachineFonts()) Assert.assertFalse(outDocContents.matches("@font-face")); else Assert.assertTrue(outDocContents.matches("@font-face * font-family:'Arial'; font-style:normal; font-weight:normal; src:local[(]'☺'[)], " + "url[(]'HtmlFixedSaveOptions.UsingMachineFonts/font001.ttf'[)] format[(]'truetype'[)]; }")); } private static class ResourceSavingCallback implements IResourceSavingCallback { /** * Called when Aspose.Words saves an external resource to fixed page HTML or SVG. */ public void resourceSaving(final ResourceSavingArgs args) { System.out.println(MessageFormat.format("Original document URI:\t{0}", args.getDocument().getOriginalFileName())); System.out.println(MessageFormat.format("Resource being saved:\t{0}", args.getResourceFileName())); System.out.println(MessageFormat.format("Full uri after saving:\t{0}", args.getResourceFileUri())); args.setResourceStream(new ByteArrayOutputStream()); args.setKeepResourceStreamOpen(true); String extension = FilenameUtils.getExtension(args.getResourceFileName()); switch (extension) { case "ttf": case "woff": Assert.fail("'ResourceSavingCallback' is not fired for fonts when 'UseTargetMachineFonts' is true"); break; } } }
public java.lang.String getResourceFileName() / public void setResourceFileName(java.lang.String value)
This property allows you to redefine how the resource file names are generated during export to fixed page HTML or SVG.
When the event is fired, this property contains the file name that was generated by Aspose.Words. You can change the value of this property to save the resource into a different file. Note that file names must be unique.
Aspose.Words automatically generates a unique file name for every resource when exporting to fixed page HTML or SVG format. How the resource file name is generated depends on whether you save the document to a file or to a stream.
When saving a document to a file, the generated resource file name looks like <document base file name>.<image number>.<extension>.
When saving a document to a stream, the generated resource file name looks like Aspose.Words.<document guid>.<image number>.<extension>.
src
attribute for writing
to fixed page HTML or SVG using the document file name, the
Example:
Shows how used target machine fonts to display the document.public void usingMachineFonts() throws Exception { Document doc = new Document(getMyDir() + "Bullet points with alternative font.docx"); HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions(); { saveOptions.setExportEmbeddedCss(true); saveOptions.setUseTargetMachineFonts(true); saveOptions.setFontFormat(ExportFontFormat.TTF); saveOptions.setExportEmbeddedFonts(false); saveOptions.setResourceSavingCallback(new ResourceSavingCallback()); } doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.UsingMachineFonts.html", saveOptions); String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.UsingMachineFonts.html"), "utf-8"); if (saveOptions.getUseTargetMachineFonts()) Assert.assertFalse(outDocContents.matches("@font-face")); else Assert.assertTrue(outDocContents.matches("@font-face * font-family:'Arial'; font-style:normal; font-weight:normal; src:local[(]'☺'[)], " + "url[(]'HtmlFixedSaveOptions.UsingMachineFonts/font001.ttf'[)] format[(]'truetype'[)]; }")); } private static class ResourceSavingCallback implements IResourceSavingCallback { /** * Called when Aspose.Words saves an external resource to fixed page HTML or SVG. */ public void resourceSaving(final ResourceSavingArgs args) { System.out.println(MessageFormat.format("Original document URI:\t{0}", args.getDocument().getOriginalFileName())); System.out.println(MessageFormat.format("Resource being saved:\t{0}", args.getResourceFileName())); System.out.println(MessageFormat.format("Full uri after saving:\t{0}", args.getResourceFileUri())); args.setResourceStream(new ByteArrayOutputStream()); args.setKeepResourceStreamOpen(true); String extension = FilenameUtils.getExtension(args.getResourceFileName()); switch (extension) { case "ttf": case "woff": Assert.fail("'ResourceSavingCallback' is not fired for fonts when 'UseTargetMachineFonts' is true"); break; } } }
public java.lang.String getResourceFileUri() / public void setResourceFileUri(java.lang.String value)
This property allows you to change URIs of resource files exported to fixed page HTML or SVG documents.
Aspose.Words automatically generates an URI for every resource file during export to fixed page HTML or SVG format. The generated URIs reference resource files saved by Aspose.Words. However, the URIs can be incorrect if resource files are to be moved to other location or if resource files are saved to streams. This property allows to correct URIs in these cases.
When the event is fired, this property contains the URI that was generated by Aspose.Words. You can change the value of this property to provide a custom URI for the resource file.
Example:
Shows how used target machine fonts to display the document.public void usingMachineFonts() throws Exception { Document doc = new Document(getMyDir() + "Bullet points with alternative font.docx"); HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions(); { saveOptions.setExportEmbeddedCss(true); saveOptions.setUseTargetMachineFonts(true); saveOptions.setFontFormat(ExportFontFormat.TTF); saveOptions.setExportEmbeddedFonts(false); saveOptions.setResourceSavingCallback(new ResourceSavingCallback()); } doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.UsingMachineFonts.html", saveOptions); String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.UsingMachineFonts.html"), "utf-8"); if (saveOptions.getUseTargetMachineFonts()) Assert.assertFalse(outDocContents.matches("@font-face")); else Assert.assertTrue(outDocContents.matches("@font-face * font-family:'Arial'; font-style:normal; font-weight:normal; src:local[(]'☺'[)], " + "url[(]'HtmlFixedSaveOptions.UsingMachineFonts/font001.ttf'[)] format[(]'truetype'[)]; }")); } private static class ResourceSavingCallback implements IResourceSavingCallback { /** * Called when Aspose.Words saves an external resource to fixed page HTML or SVG. */ public void resourceSaving(final ResourceSavingArgs args) { System.out.println(MessageFormat.format("Original document URI:\t{0}", args.getDocument().getOriginalFileName())); System.out.println(MessageFormat.format("Resource being saved:\t{0}", args.getResourceFileName())); System.out.println(MessageFormat.format("Full uri after saving:\t{0}", args.getResourceFileUri())); args.setResourceStream(new ByteArrayOutputStream()); args.setKeepResourceStreamOpen(true); String extension = FilenameUtils.getExtension(args.getResourceFileName()); switch (extension) { case "ttf": case "woff": Assert.fail("'ResourceSavingCallback' is not fired for fonts when 'UseTargetMachineFonts' is true"); break; } } }
public java.io.OutputStream getResourceStream() / public void setResourceStream(java.io.OutputStream value)
This property allows you to save resources to streams instead of files.
The default value is null
. When this property is null
, the resource
will be saved to a file specified in the
Using
Example:
Shows how used target machine fonts to display the document.public void usingMachineFonts() throws Exception { Document doc = new Document(getMyDir() + "Bullet points with alternative font.docx"); HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions(); { saveOptions.setExportEmbeddedCss(true); saveOptions.setUseTargetMachineFonts(true); saveOptions.setFontFormat(ExportFontFormat.TTF); saveOptions.setExportEmbeddedFonts(false); saveOptions.setResourceSavingCallback(new ResourceSavingCallback()); } doc.save(getArtifactsDir() + "HtmlFixedSaveOptions.UsingMachineFonts.html", saveOptions); String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "HtmlFixedSaveOptions.UsingMachineFonts.html"), "utf-8"); if (saveOptions.getUseTargetMachineFonts()) Assert.assertFalse(outDocContents.matches("@font-face")); else Assert.assertTrue(outDocContents.matches("@font-face * font-family:'Arial'; font-style:normal; font-weight:normal; src:local[(]'☺'[)], " + "url[(]'HtmlFixedSaveOptions.UsingMachineFonts/font001.ttf'[)] format[(]'truetype'[)]; }")); } private static class ResourceSavingCallback implements IResourceSavingCallback { /** * Called when Aspose.Words saves an external resource to fixed page HTML or SVG. */ public void resourceSaving(final ResourceSavingArgs args) { System.out.println(MessageFormat.format("Original document URI:\t{0}", args.getDocument().getOriginalFileName())); System.out.println(MessageFormat.format("Resource being saved:\t{0}", args.getResourceFileName())); System.out.println(MessageFormat.format("Full uri after saving:\t{0}", args.getResourceFileUri())); args.setResourceStream(new ByteArrayOutputStream()); args.setKeepResourceStreamOpen(true); String extension = FilenameUtils.getExtension(args.getResourceFileName()); switch (extension) { case "ttf": case "woff": Assert.fail("'ResourceSavingCallback' is not fired for fonts when 'UseTargetMachineFonts' is true"); break; } } }