com.aspose.words

Interface IFontSavingCallback

  • public interface IFontSavingCallback 

Implement this interface if you want to receive notifications and control how Aspose.Words saves fonts when exporting a document to HTML format.

Example:

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());
    }

}

/// <summary>
/// Prints information about exported fonts, and saves them in the same local system folder as their output .html.
/// </summary>
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());
    }
}

Method Summary
abstract voidfontSaving(FontSavingArgs args)
Called when Aspose.Words is about to save a font resource.
 

    • Method Detail

      • fontSaving

        public abstract void fontSaving(FontSavingArgs args)
                                     throws java.lang.Exception
        Called when Aspose.Words is about to save a font resource.

        Example:

        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());
            }
        
        }
        
        /// <summary>
        /// Prints information about exported fonts, and saves them in the same local system folder as their output .html.
        /// </summary>
        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());
            }
        }