FontSavingArgs
Inheritance: java.lang.Object
public class FontSavingArgs
Provides data for the IFontSavingCallback.fontSaving(com.aspose.words.FontSavingArgs) event.
To learn more, visit the Save a Document documentation article.
Remarks:
When Aspose.Words saves a document to HTML or related formats and HtmlSaveOptions.getExportFontResources() / HtmlSaveOptions.setExportFontResources(boolean) is set to true , it saves each font subject for export into a separate file.
FontSavingArgs controls whether particular font resource should be exported and how.
FontSavingArgs also allows to redefine how font file names are generated or to completely circumvent saving of fonts into files by providing your own stream objects.
To decide whether to save a particular font resource, use the isExportNeeded() / isExportNeeded(boolean) property.
To save fonts into streams instead of files, use the P:Aspose.Words.Saving.FontSavingArgs.FontStream property.
Examples:
Shows how to define custom logic for exporting fonts when saving to HTML.
public void saveExportedFonts() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
// Configure a SaveOptions object to export fonts to separate files.
// Set a callback that will handle font saving in a custom manner.
HtmlSaveOptions options = new HtmlSaveOptions();
{
options.setExportFontResources(true);
options.setFontSavingCallback(new HandleFontSaving());
}
// The callback will export .ttf files and save them alongside the output document.
doc.save(getArtifactsDir() + "HtmlSaveOptions.SaveExportedFonts.html", options);
File[] fontFileNames = new File(getArtifactsDir()).listFiles((d, name) -> name.endsWith(".ttf"));
for (File fontFilename : fontFileNames) {
System.out.println(fontFilename.getName());
}
}
///
/// Prints information about exported fonts and saves them in the same local system folder as their output .html.
///
public static class HandleFontSaving implements IFontSavingCallback {
public void fontSaving(FontSavingArgs args) throws Exception {
System.out.println(MessageFormat.format("Font:\t{0}", args.getFontFamilyName()));
if (args.getBold()) System.out.print(", bold");
if (args.getItalic()) System.out.print(", italic");
System.out.println(MessageFormat.format("\nSource:\t{0}, {1} bytes\n", args.getOriginalFileName(), args.getOriginalFileSize()));
// We can also access the source document from here.
Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.docx"));
Assert.assertTrue(args.isExportNeeded());
Assert.assertTrue(args.isSubsettingNeeded());
String[] splittedFileName = args.getOriginalFileName().split("\\\\");
String fileName = splittedFileName[splittedFileName.length - 1];
// There are two ways of saving an exported font.
// 1 - Save it to a local file system location:
args.setFontFileName(fileName);
// 2 - Save it to a stream:
args.setFontStream(new FileOutputStream(fileName));
Assert.assertFalse(args.getKeepFontStreamOpen());
}
}
Methods
Method | Description |
---|---|
getBold() | Indicates whether the current font is bold. |
getDocument() | Gets the document object that is being saved. |
getFontFamilyName() | Indicates the current font family name. |
getFontFileName() | Gets the file name (without path) where the font will be saved to. |
getFontStream() | |
getItalic() | Indicates whether the current font is italic. |
getKeepFontStreamOpen() | Specifies whether Aspose.Words should keep the stream open or close it after saving a font. |
getOriginalFileName() | Gets the original font file name with an extension. |
getOriginalFileSize() | Gets the original font file size. |
isExportNeeded() | Allows to specify whether the current font will be exported as a font resource. |
isExportNeeded(boolean value) | Allows to specify whether the current font will be exported as a font resource. |
isSubsettingNeeded() | Allows to specify whether the current font will be subsetted before exporting as a font resource. |
isSubsettingNeeded(boolean value) | Allows to specify whether the current font will be subsetted before exporting as a font resource. |
setFontFileName(String value) | Sets the file name (without path) where the font will be saved to. |
setFontStream(OutputStream value) | |
setKeepFontStreamOpen(boolean value) | Specifies whether Aspose.Words should keep the stream open or close it after saving a font. |
getBold()
public boolean getBold()
Indicates whether the current font is bold.
Examples:
Shows how to define custom logic for exporting fonts when saving to HTML.
public void saveExportedFonts() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
// Configure a SaveOptions object to export fonts to separate files.
// Set a callback that will handle font saving in a custom manner.
HtmlSaveOptions options = new HtmlSaveOptions();
{
options.setExportFontResources(true);
options.setFontSavingCallback(new HandleFontSaving());
}
// The callback will export .ttf files and save them alongside the output document.
doc.save(getArtifactsDir() + "HtmlSaveOptions.SaveExportedFonts.html", options);
File[] fontFileNames = new File(getArtifactsDir()).listFiles((d, name) -> name.endsWith(".ttf"));
for (File fontFilename : fontFileNames) {
System.out.println(fontFilename.getName());
}
}
///
/// Prints information about exported fonts and saves them in the same local system folder as their output .html.
///
public static class HandleFontSaving implements IFontSavingCallback {
public void fontSaving(FontSavingArgs args) throws Exception {
System.out.println(MessageFormat.format("Font:\t{0}", args.getFontFamilyName()));
if (args.getBold()) System.out.print(", bold");
if (args.getItalic()) System.out.print(", italic");
System.out.println(MessageFormat.format("\nSource:\t{0}, {1} bytes\n", args.getOriginalFileName(), args.getOriginalFileSize()));
// We can also access the source document from here.
Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.docx"));
Assert.assertTrue(args.isExportNeeded());
Assert.assertTrue(args.isSubsettingNeeded());
String[] splittedFileName = args.getOriginalFileName().split("\\\\");
String fileName = splittedFileName[splittedFileName.length - 1];
// There are two ways of saving an exported font.
// 1 - Save it to a local file system location:
args.setFontFileName(fileName);
// 2 - Save it to a stream:
args.setFontStream(new FileOutputStream(fileName));
Assert.assertFalse(args.getKeepFontStreamOpen());
}
}
Returns: boolean - The corresponding boolean value.
getDocument()
public Document getDocument()
Gets the document object that is being saved.
Examples:
Shows how to define custom logic for exporting fonts when saving to HTML.
public void saveExportedFonts() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
// Configure a SaveOptions object to export fonts to separate files.
// Set a callback that will handle font saving in a custom manner.
HtmlSaveOptions options = new HtmlSaveOptions();
{
options.setExportFontResources(true);
options.setFontSavingCallback(new HandleFontSaving());
}
// The callback will export .ttf files and save them alongside the output document.
doc.save(getArtifactsDir() + "HtmlSaveOptions.SaveExportedFonts.html", options);
File[] fontFileNames = new File(getArtifactsDir()).listFiles((d, name) -> name.endsWith(".ttf"));
for (File fontFilename : fontFileNames) {
System.out.println(fontFilename.getName());
}
}
///
/// Prints information about exported fonts and saves them in the same local system folder as their output .html.
///
public static class HandleFontSaving implements IFontSavingCallback {
public void fontSaving(FontSavingArgs args) throws Exception {
System.out.println(MessageFormat.format("Font:\t{0}", args.getFontFamilyName()));
if (args.getBold()) System.out.print(", bold");
if (args.getItalic()) System.out.print(", italic");
System.out.println(MessageFormat.format("\nSource:\t{0}, {1} bytes\n", args.getOriginalFileName(), args.getOriginalFileSize()));
// We can also access the source document from here.
Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.docx"));
Assert.assertTrue(args.isExportNeeded());
Assert.assertTrue(args.isSubsettingNeeded());
String[] splittedFileName = args.getOriginalFileName().split("\\\\");
String fileName = splittedFileName[splittedFileName.length - 1];
// There are two ways of saving an exported font.
// 1 - Save it to a local file system location:
args.setFontFileName(fileName);
// 2 - Save it to a stream:
args.setFontStream(new FileOutputStream(fileName));
Assert.assertFalse(args.getKeepFontStreamOpen());
}
}
Returns: Document - The document object that is being saved.
getFontFamilyName()
public String getFontFamilyName()
Indicates the current font family name.
Examples:
Shows how to define custom logic for exporting fonts when saving to HTML.
public void saveExportedFonts() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
// Configure a SaveOptions object to export fonts to separate files.
// Set a callback that will handle font saving in a custom manner.
HtmlSaveOptions options = new HtmlSaveOptions();
{
options.setExportFontResources(true);
options.setFontSavingCallback(new HandleFontSaving());
}
// The callback will export .ttf files and save them alongside the output document.
doc.save(getArtifactsDir() + "HtmlSaveOptions.SaveExportedFonts.html", options);
File[] fontFileNames = new File(getArtifactsDir()).listFiles((d, name) -> name.endsWith(".ttf"));
for (File fontFilename : fontFileNames) {
System.out.println(fontFilename.getName());
}
}
///
/// Prints information about exported fonts and saves them in the same local system folder as their output .html.
///
public static class HandleFontSaving implements IFontSavingCallback {
public void fontSaving(FontSavingArgs args) throws Exception {
System.out.println(MessageFormat.format("Font:\t{0}", args.getFontFamilyName()));
if (args.getBold()) System.out.print(", bold");
if (args.getItalic()) System.out.print(", italic");
System.out.println(MessageFormat.format("\nSource:\t{0}, {1} bytes\n", args.getOriginalFileName(), args.getOriginalFileSize()));
// We can also access the source document from here.
Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.docx"));
Assert.assertTrue(args.isExportNeeded());
Assert.assertTrue(args.isSubsettingNeeded());
String[] splittedFileName = args.getOriginalFileName().split("\\\\");
String fileName = splittedFileName[splittedFileName.length - 1];
// There are two ways of saving an exported font.
// 1 - Save it to a local file system location:
args.setFontFileName(fileName);
// 2 - Save it to a stream:
args.setFontStream(new FileOutputStream(fileName));
Assert.assertFalse(args.getKeepFontStreamOpen());
}
}
Returns: java.lang.String - The corresponding java.lang.String value.
getFontFileName()
public String getFontFileName()
Gets the file name (without path) where the font will be saved to.
Remarks:
This property allows you to redefine how the font file names are generated during export to HTML.
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 font into a different file. Note that file names must be unique.
Aspose.Words automatically generates a unique file name for every embedded font when exporting to HTML format. How the font 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 font file name looks like ...
When saving a document to a stream, the generated font file name looks like Aspose.Words….
getFontFileName() / setFontFileName(java.lang.String) must contain only the file name without the path. Aspose.Words determines the path for saving using the document file name, the HtmlSaveOptions.getFontsFolder() / HtmlSaveOptions.setFontsFolder(java.lang.String) and HtmlSaveOptions.getFontsFolderAlias() / HtmlSaveOptions.setFontsFolderAlias(java.lang.String) properties.
Examples:
Shows how to define custom logic for exporting fonts when saving to HTML.
public void saveExportedFonts() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
// Configure a SaveOptions object to export fonts to separate files.
// Set a callback that will handle font saving in a custom manner.
HtmlSaveOptions options = new HtmlSaveOptions();
{
options.setExportFontResources(true);
options.setFontSavingCallback(new HandleFontSaving());
}
// The callback will export .ttf files and save them alongside the output document.
doc.save(getArtifactsDir() + "HtmlSaveOptions.SaveExportedFonts.html", options);
File[] fontFileNames = new File(getArtifactsDir()).listFiles((d, name) -> name.endsWith(".ttf"));
for (File fontFilename : fontFileNames) {
System.out.println(fontFilename.getName());
}
}
///
/// Prints information about exported fonts and saves them in the same local system folder as their output .html.
///
public static class HandleFontSaving implements IFontSavingCallback {
public void fontSaving(FontSavingArgs args) throws Exception {
System.out.println(MessageFormat.format("Font:\t{0}", args.getFontFamilyName()));
if (args.getBold()) System.out.print(", bold");
if (args.getItalic()) System.out.print(", italic");
System.out.println(MessageFormat.format("\nSource:\t{0}, {1} bytes\n", args.getOriginalFileName(), args.getOriginalFileSize()));
// We can also access the source document from here.
Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.docx"));
Assert.assertTrue(args.isExportNeeded());
Assert.assertTrue(args.isSubsettingNeeded());
String[] splittedFileName = args.getOriginalFileName().split("\\\\");
String fileName = splittedFileName[splittedFileName.length - 1];
// There are two ways of saving an exported font.
// 1 - Save it to a local file system location:
args.setFontFileName(fileName);
// 2 - Save it to a stream:
args.setFontStream(new FileOutputStream(fileName));
Assert.assertFalse(args.getKeepFontStreamOpen());
}
}
P:Aspose.Words.Saving.FontSavingArgs.FontStream
Returns: java.lang.String - The file name (without path) where the font will be saved to.
getFontStream()
public OutputStream getFontStream()
Returns: java.io.OutputStream
getItalic()
public boolean getItalic()
Indicates whether the current font is italic.
Examples:
Shows how to define custom logic for exporting fonts when saving to HTML.
public void saveExportedFonts() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
// Configure a SaveOptions object to export fonts to separate files.
// Set a callback that will handle font saving in a custom manner.
HtmlSaveOptions options = new HtmlSaveOptions();
{
options.setExportFontResources(true);
options.setFontSavingCallback(new HandleFontSaving());
}
// The callback will export .ttf files and save them alongside the output document.
doc.save(getArtifactsDir() + "HtmlSaveOptions.SaveExportedFonts.html", options);
File[] fontFileNames = new File(getArtifactsDir()).listFiles((d, name) -> name.endsWith(".ttf"));
for (File fontFilename : fontFileNames) {
System.out.println(fontFilename.getName());
}
}
///
/// Prints information about exported fonts and saves them in the same local system folder as their output .html.
///
public static class HandleFontSaving implements IFontSavingCallback {
public void fontSaving(FontSavingArgs args) throws Exception {
System.out.println(MessageFormat.format("Font:\t{0}", args.getFontFamilyName()));
if (args.getBold()) System.out.print(", bold");
if (args.getItalic()) System.out.print(", italic");
System.out.println(MessageFormat.format("\nSource:\t{0}, {1} bytes\n", args.getOriginalFileName(), args.getOriginalFileSize()));
// We can also access the source document from here.
Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.docx"));
Assert.assertTrue(args.isExportNeeded());
Assert.assertTrue(args.isSubsettingNeeded());
String[] splittedFileName = args.getOriginalFileName().split("\\\\");
String fileName = splittedFileName[splittedFileName.length - 1];
// There are two ways of saving an exported font.
// 1 - Save it to a local file system location:
args.setFontFileName(fileName);
// 2 - Save it to a stream:
args.setFontStream(new FileOutputStream(fileName));
Assert.assertFalse(args.getKeepFontStreamOpen());
}
}
Returns: boolean - The corresponding boolean value.
getKeepFontStreamOpen()
public boolean getKeepFontStreamOpen()
Specifies whether Aspose.Words should keep the stream open or close it after saving a font.
Remarks:
Default is false and Aspose.Words will close the stream you provided in the P:Aspose.Words.Saving.FontSavingArgs.FontStream property after writing a font into it. Specify true to keep the stream open.
Examples:
Shows how to define custom logic for exporting fonts when saving to HTML.
public void saveExportedFonts() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
// Configure a SaveOptions object to export fonts to separate files.
// Set a callback that will handle font saving in a custom manner.
HtmlSaveOptions options = new HtmlSaveOptions();
{
options.setExportFontResources(true);
options.setFontSavingCallback(new HandleFontSaving());
}
// The callback will export .ttf files and save them alongside the output document.
doc.save(getArtifactsDir() + "HtmlSaveOptions.SaveExportedFonts.html", options);
File[] fontFileNames = new File(getArtifactsDir()).listFiles((d, name) -> name.endsWith(".ttf"));
for (File fontFilename : fontFileNames) {
System.out.println(fontFilename.getName());
}
}
///
/// Prints information about exported fonts and saves them in the same local system folder as their output .html.
///
public static class HandleFontSaving implements IFontSavingCallback {
public void fontSaving(FontSavingArgs args) throws Exception {
System.out.println(MessageFormat.format("Font:\t{0}", args.getFontFamilyName()));
if (args.getBold()) System.out.print(", bold");
if (args.getItalic()) System.out.print(", italic");
System.out.println(MessageFormat.format("\nSource:\t{0}, {1} bytes\n", args.getOriginalFileName(), args.getOriginalFileSize()));
// We can also access the source document from here.
Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.docx"));
Assert.assertTrue(args.isExportNeeded());
Assert.assertTrue(args.isSubsettingNeeded());
String[] splittedFileName = args.getOriginalFileName().split("\\\\");
String fileName = splittedFileName[splittedFileName.length - 1];
// There are two ways of saving an exported font.
// 1 - Save it to a local file system location:
args.setFontFileName(fileName);
// 2 - Save it to a stream:
args.setFontStream(new FileOutputStream(fileName));
Assert.assertFalse(args.getKeepFontStreamOpen());
}
}
P:Aspose.Words.Saving.FontSavingArgs.FontStream
Returns: boolean - The corresponding boolean value.
getOriginalFileName()
public String getOriginalFileName()
Gets the original font file name with an extension.
Remarks:
This property contains the original file name of the current font if it is known. Otherwise it can be an empty string.
Examples:
Shows how to define custom logic for exporting fonts when saving to HTML.
public void saveExportedFonts() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
// Configure a SaveOptions object to export fonts to separate files.
// Set a callback that will handle font saving in a custom manner.
HtmlSaveOptions options = new HtmlSaveOptions();
{
options.setExportFontResources(true);
options.setFontSavingCallback(new HandleFontSaving());
}
// The callback will export .ttf files and save them alongside the output document.
doc.save(getArtifactsDir() + "HtmlSaveOptions.SaveExportedFonts.html", options);
File[] fontFileNames = new File(getArtifactsDir()).listFiles((d, name) -> name.endsWith(".ttf"));
for (File fontFilename : fontFileNames) {
System.out.println(fontFilename.getName());
}
}
///
/// Prints information about exported fonts and saves them in the same local system folder as their output .html.
///
public static class HandleFontSaving implements IFontSavingCallback {
public void fontSaving(FontSavingArgs args) throws Exception {
System.out.println(MessageFormat.format("Font:\t{0}", args.getFontFamilyName()));
if (args.getBold()) System.out.print(", bold");
if (args.getItalic()) System.out.print(", italic");
System.out.println(MessageFormat.format("\nSource:\t{0}, {1} bytes\n", args.getOriginalFileName(), args.getOriginalFileSize()));
// We can also access the source document from here.
Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.docx"));
Assert.assertTrue(args.isExportNeeded());
Assert.assertTrue(args.isSubsettingNeeded());
String[] splittedFileName = args.getOriginalFileName().split("\\\\");
String fileName = splittedFileName[splittedFileName.length - 1];
// There are two ways of saving an exported font.
// 1 - Save it to a local file system location:
args.setFontFileName(fileName);
// 2 - Save it to a stream:
args.setFontStream(new FileOutputStream(fileName));
Assert.assertFalse(args.getKeepFontStreamOpen());
}
}
Returns: java.lang.String - The original font file name with an extension.
getOriginalFileSize()
public int getOriginalFileSize()
Gets the original font file size.
Remarks:
This property contains the original file size of the current font if it is known. Otherwise it can be zero.
Examples:
Shows how to define custom logic for exporting fonts when saving to HTML.
public void saveExportedFonts() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
// Configure a SaveOptions object to export fonts to separate files.
// Set a callback that will handle font saving in a custom manner.
HtmlSaveOptions options = new HtmlSaveOptions();
{
options.setExportFontResources(true);
options.setFontSavingCallback(new HandleFontSaving());
}
// The callback will export .ttf files and save them alongside the output document.
doc.save(getArtifactsDir() + "HtmlSaveOptions.SaveExportedFonts.html", options);
File[] fontFileNames = new File(getArtifactsDir()).listFiles((d, name) -> name.endsWith(".ttf"));
for (File fontFilename : fontFileNames) {
System.out.println(fontFilename.getName());
}
}
///
/// Prints information about exported fonts and saves them in the same local system folder as their output .html.
///
public static class HandleFontSaving implements IFontSavingCallback {
public void fontSaving(FontSavingArgs args) throws Exception {
System.out.println(MessageFormat.format("Font:\t{0}", args.getFontFamilyName()));
if (args.getBold()) System.out.print(", bold");
if (args.getItalic()) System.out.print(", italic");
System.out.println(MessageFormat.format("\nSource:\t{0}, {1} bytes\n", args.getOriginalFileName(), args.getOriginalFileSize()));
// We can also access the source document from here.
Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.docx"));
Assert.assertTrue(args.isExportNeeded());
Assert.assertTrue(args.isSubsettingNeeded());
String[] splittedFileName = args.getOriginalFileName().split("\\\\");
String fileName = splittedFileName[splittedFileName.length - 1];
// There are two ways of saving an exported font.
// 1 - Save it to a local file system location:
args.setFontFileName(fileName);
// 2 - Save it to a stream:
args.setFontStream(new FileOutputStream(fileName));
Assert.assertFalse(args.getKeepFontStreamOpen());
}
}
Returns: int - The original font file size.
isExportNeeded()
public boolean isExportNeeded()
Allows to specify whether the current font will be exported as a font resource. Default is true .
Examples:
Shows how to define custom logic for exporting fonts when saving to HTML.
public void saveExportedFonts() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
// Configure a SaveOptions object to export fonts to separate files.
// Set a callback that will handle font saving in a custom manner.
HtmlSaveOptions options = new HtmlSaveOptions();
{
options.setExportFontResources(true);
options.setFontSavingCallback(new HandleFontSaving());
}
// The callback will export .ttf files and save them alongside the output document.
doc.save(getArtifactsDir() + "HtmlSaveOptions.SaveExportedFonts.html", options);
File[] fontFileNames = new File(getArtifactsDir()).listFiles((d, name) -> name.endsWith(".ttf"));
for (File fontFilename : fontFileNames) {
System.out.println(fontFilename.getName());
}
}
///
/// Prints information about exported fonts and saves them in the same local system folder as their output .html.
///
public static class HandleFontSaving implements IFontSavingCallback {
public void fontSaving(FontSavingArgs args) throws Exception {
System.out.println(MessageFormat.format("Font:\t{0}", args.getFontFamilyName()));
if (args.getBold()) System.out.print(", bold");
if (args.getItalic()) System.out.print(", italic");
System.out.println(MessageFormat.format("\nSource:\t{0}, {1} bytes\n", args.getOriginalFileName(), args.getOriginalFileSize()));
// We can also access the source document from here.
Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.docx"));
Assert.assertTrue(args.isExportNeeded());
Assert.assertTrue(args.isSubsettingNeeded());
String[] splittedFileName = args.getOriginalFileName().split("\\\\");
String fileName = splittedFileName[splittedFileName.length - 1];
// There are two ways of saving an exported font.
// 1 - Save it to a local file system location:
args.setFontFileName(fileName);
// 2 - Save it to a stream:
args.setFontStream(new FileOutputStream(fileName));
Assert.assertFalse(args.getKeepFontStreamOpen());
}
}
Returns: boolean - The corresponding boolean value.
isExportNeeded(boolean value)
public void isExportNeeded(boolean value)
Allows to specify whether the current font will be exported as a font resource. Default is true .
Examples:
Shows how to define custom logic for exporting fonts when saving to HTML.
public void saveExportedFonts() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
// Configure a SaveOptions object to export fonts to separate files.
// Set a callback that will handle font saving in a custom manner.
HtmlSaveOptions options = new HtmlSaveOptions();
{
options.setExportFontResources(true);
options.setFontSavingCallback(new HandleFontSaving());
}
// The callback will export .ttf files and save them alongside the output document.
doc.save(getArtifactsDir() + "HtmlSaveOptions.SaveExportedFonts.html", options);
File[] fontFileNames = new File(getArtifactsDir()).listFiles((d, name) -> name.endsWith(".ttf"));
for (File fontFilename : fontFileNames) {
System.out.println(fontFilename.getName());
}
}
///
/// Prints information about exported fonts and saves them in the same local system folder as their output .html.
///
public static class HandleFontSaving implements IFontSavingCallback {
public void fontSaving(FontSavingArgs args) throws Exception {
System.out.println(MessageFormat.format("Font:\t{0}", args.getFontFamilyName()));
if (args.getBold()) System.out.print(", bold");
if (args.getItalic()) System.out.print(", italic");
System.out.println(MessageFormat.format("\nSource:\t{0}, {1} bytes\n", args.getOriginalFileName(), args.getOriginalFileSize()));
// We can also access the source document from here.
Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.docx"));
Assert.assertTrue(args.isExportNeeded());
Assert.assertTrue(args.isSubsettingNeeded());
String[] splittedFileName = args.getOriginalFileName().split("\\\\");
String fileName = splittedFileName[splittedFileName.length - 1];
// There are two ways of saving an exported font.
// 1 - Save it to a local file system location:
args.setFontFileName(fileName);
// 2 - Save it to a stream:
args.setFontStream(new FileOutputStream(fileName));
Assert.assertFalse(args.getKeepFontStreamOpen());
}
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | boolean | The corresponding boolean value. |
isSubsettingNeeded()
public boolean isSubsettingNeeded()
Allows to specify whether the current font will be subsetted before exporting as a font resource.
Remarks:
Fonts can be exported as complete original font files or subsetted to include only the characters that are used in the document. Subsetting allows to reduce the resulting font resource size.
By default, Aspose.Words decides whether to perform subsetting or not by comparing the original font file size with the one specified in HtmlSaveOptions.getFontResourcesSubsettingSizeThreshold() / HtmlSaveOptions.setFontResourcesSubsettingSizeThreshold(int). You can override this behavior for individual fonts by setting the isSubsettingNeeded() / isSubsettingNeeded(boolean) property.
Examples:
Shows how to define custom logic for exporting fonts when saving to HTML.
public void saveExportedFonts() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
// Configure a SaveOptions object to export fonts to separate files.
// Set a callback that will handle font saving in a custom manner.
HtmlSaveOptions options = new HtmlSaveOptions();
{
options.setExportFontResources(true);
options.setFontSavingCallback(new HandleFontSaving());
}
// The callback will export .ttf files and save them alongside the output document.
doc.save(getArtifactsDir() + "HtmlSaveOptions.SaveExportedFonts.html", options);
File[] fontFileNames = new File(getArtifactsDir()).listFiles((d, name) -> name.endsWith(".ttf"));
for (File fontFilename : fontFileNames) {
System.out.println(fontFilename.getName());
}
}
///
/// Prints information about exported fonts and saves them in the same local system folder as their output .html.
///
public static class HandleFontSaving implements IFontSavingCallback {
public void fontSaving(FontSavingArgs args) throws Exception {
System.out.println(MessageFormat.format("Font:\t{0}", args.getFontFamilyName()));
if (args.getBold()) System.out.print(", bold");
if (args.getItalic()) System.out.print(", italic");
System.out.println(MessageFormat.format("\nSource:\t{0}, {1} bytes\n", args.getOriginalFileName(), args.getOriginalFileSize()));
// We can also access the source document from here.
Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.docx"));
Assert.assertTrue(args.isExportNeeded());
Assert.assertTrue(args.isSubsettingNeeded());
String[] splittedFileName = args.getOriginalFileName().split("\\\\");
String fileName = splittedFileName[splittedFileName.length - 1];
// There are two ways of saving an exported font.
// 1 - Save it to a local file system location:
args.setFontFileName(fileName);
// 2 - Save it to a stream:
args.setFontStream(new FileOutputStream(fileName));
Assert.assertFalse(args.getKeepFontStreamOpen());
}
}
Returns: boolean - The corresponding boolean value.
isSubsettingNeeded(boolean value)
public void isSubsettingNeeded(boolean value)
Allows to specify whether the current font will be subsetted before exporting as a font resource.
Remarks:
Fonts can be exported as complete original font files or subsetted to include only the characters that are used in the document. Subsetting allows to reduce the resulting font resource size.
By default, Aspose.Words decides whether to perform subsetting or not by comparing the original font file size with the one specified in HtmlSaveOptions.getFontResourcesSubsettingSizeThreshold() / HtmlSaveOptions.setFontResourcesSubsettingSizeThreshold(int). You can override this behavior for individual fonts by setting the isSubsettingNeeded() / isSubsettingNeeded(boolean) property.
Examples:
Shows how to define custom logic for exporting fonts when saving to HTML.
public void saveExportedFonts() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
// Configure a SaveOptions object to export fonts to separate files.
// Set a callback that will handle font saving in a custom manner.
HtmlSaveOptions options = new HtmlSaveOptions();
{
options.setExportFontResources(true);
options.setFontSavingCallback(new HandleFontSaving());
}
// The callback will export .ttf files and save them alongside the output document.
doc.save(getArtifactsDir() + "HtmlSaveOptions.SaveExportedFonts.html", options);
File[] fontFileNames = new File(getArtifactsDir()).listFiles((d, name) -> name.endsWith(".ttf"));
for (File fontFilename : fontFileNames) {
System.out.println(fontFilename.getName());
}
}
///
/// Prints information about exported fonts and saves them in the same local system folder as their output .html.
///
public static class HandleFontSaving implements IFontSavingCallback {
public void fontSaving(FontSavingArgs args) throws Exception {
System.out.println(MessageFormat.format("Font:\t{0}", args.getFontFamilyName()));
if (args.getBold()) System.out.print(", bold");
if (args.getItalic()) System.out.print(", italic");
System.out.println(MessageFormat.format("\nSource:\t{0}, {1} bytes\n", args.getOriginalFileName(), args.getOriginalFileSize()));
// We can also access the source document from here.
Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.docx"));
Assert.assertTrue(args.isExportNeeded());
Assert.assertTrue(args.isSubsettingNeeded());
String[] splittedFileName = args.getOriginalFileName().split("\\\\");
String fileName = splittedFileName[splittedFileName.length - 1];
// There are two ways of saving an exported font.
// 1 - Save it to a local file system location:
args.setFontFileName(fileName);
// 2 - Save it to a stream:
args.setFontStream(new FileOutputStream(fileName));
Assert.assertFalse(args.getKeepFontStreamOpen());
}
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | boolean | The corresponding boolean value. |
setFontFileName(String value)
public void setFontFileName(String value)
Sets the file name (without path) where the font will be saved to.
Remarks:
This property allows you to redefine how the font file names are generated during export to HTML.
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 font into a different file. Note that file names must be unique.
Aspose.Words automatically generates a unique file name for every embedded font when exporting to HTML format. How the font 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 font file name looks like ...
When saving a document to a stream, the generated font file name looks like Aspose.Words….
getFontFileName() / setFontFileName(java.lang.String) must contain only the file name without the path. Aspose.Words determines the path for saving using the document file name, the HtmlSaveOptions.getFontsFolder() / HtmlSaveOptions.setFontsFolder(java.lang.String) and HtmlSaveOptions.getFontsFolderAlias() / HtmlSaveOptions.setFontsFolderAlias(java.lang.String) properties.
Examples:
Shows how to define custom logic for exporting fonts when saving to HTML.
public void saveExportedFonts() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
// Configure a SaveOptions object to export fonts to separate files.
// Set a callback that will handle font saving in a custom manner.
HtmlSaveOptions options = new HtmlSaveOptions();
{
options.setExportFontResources(true);
options.setFontSavingCallback(new HandleFontSaving());
}
// The callback will export .ttf files and save them alongside the output document.
doc.save(getArtifactsDir() + "HtmlSaveOptions.SaveExportedFonts.html", options);
File[] fontFileNames = new File(getArtifactsDir()).listFiles((d, name) -> name.endsWith(".ttf"));
for (File fontFilename : fontFileNames) {
System.out.println(fontFilename.getName());
}
}
///
/// Prints information about exported fonts and saves them in the same local system folder as their output .html.
///
public static class HandleFontSaving implements IFontSavingCallback {
public void fontSaving(FontSavingArgs args) throws Exception {
System.out.println(MessageFormat.format("Font:\t{0}", args.getFontFamilyName()));
if (args.getBold()) System.out.print(", bold");
if (args.getItalic()) System.out.print(", italic");
System.out.println(MessageFormat.format("\nSource:\t{0}, {1} bytes\n", args.getOriginalFileName(), args.getOriginalFileSize()));
// We can also access the source document from here.
Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.docx"));
Assert.assertTrue(args.isExportNeeded());
Assert.assertTrue(args.isSubsettingNeeded());
String[] splittedFileName = args.getOriginalFileName().split("\\\\");
String fileName = splittedFileName[splittedFileName.length - 1];
// There are two ways of saving an exported font.
// 1 - Save it to a local file system location:
args.setFontFileName(fileName);
// 2 - Save it to a stream:
args.setFontStream(new FileOutputStream(fileName));
Assert.assertFalse(args.getKeepFontStreamOpen());
}
}
P:Aspose.Words.Saving.FontSavingArgs.FontStream
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The file name (without path) where the font will be saved to. |
setFontStream(OutputStream value)
public void setFontStream(OutputStream value)
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.io.OutputStream |
setKeepFontStreamOpen(boolean value)
public void setKeepFontStreamOpen(boolean value)
Specifies whether Aspose.Words should keep the stream open or close it after saving a font.
Remarks:
Default is false and Aspose.Words will close the stream you provided in the P:Aspose.Words.Saving.FontSavingArgs.FontStream property after writing a font into it. Specify true to keep the stream open.
Examples:
Shows how to define custom logic for exporting fonts when saving to HTML.
public void saveExportedFonts() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
// Configure a SaveOptions object to export fonts to separate files.
// Set a callback that will handle font saving in a custom manner.
HtmlSaveOptions options = new HtmlSaveOptions();
{
options.setExportFontResources(true);
options.setFontSavingCallback(new HandleFontSaving());
}
// The callback will export .ttf files and save them alongside the output document.
doc.save(getArtifactsDir() + "HtmlSaveOptions.SaveExportedFonts.html", options);
File[] fontFileNames = new File(getArtifactsDir()).listFiles((d, name) -> name.endsWith(".ttf"));
for (File fontFilename : fontFileNames) {
System.out.println(fontFilename.getName());
}
}
///
/// Prints information about exported fonts and saves them in the same local system folder as their output .html.
///
public static class HandleFontSaving implements IFontSavingCallback {
public void fontSaving(FontSavingArgs args) throws Exception {
System.out.println(MessageFormat.format("Font:\t{0}", args.getFontFamilyName()));
if (args.getBold()) System.out.print(", bold");
if (args.getItalic()) System.out.print(", italic");
System.out.println(MessageFormat.format("\nSource:\t{0}, {1} bytes\n", args.getOriginalFileName(), args.getOriginalFileSize()));
// We can also access the source document from here.
Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.docx"));
Assert.assertTrue(args.isExportNeeded());
Assert.assertTrue(args.isSubsettingNeeded());
String[] splittedFileName = args.getOriginalFileName().split("\\\\");
String fileName = splittedFileName[splittedFileName.length - 1];
// There are two ways of saving an exported font.
// 1 - Save it to a local file system location:
args.setFontFileName(fileName);
// 2 - Save it to a stream:
args.setFontStream(new FileOutputStream(fileName));
Assert.assertFalse(args.getKeepFontStreamOpen());
}
}
P:Aspose.Words.Saving.FontSavingArgs.FontStream
Parameters:
Parameter | Type | Description |
---|---|---|
value | boolean | The corresponding boolean value. |