com.aspose.words

Interface IResourceSavingCallback

  • public interface IResourceSavingCallback 

Implement this interface if you want to control how Aspose.Words saves external resources (images, fonts and css) when saving a document to fixed page HTML or SVG.

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

Method Summary
abstract voidresourceSaving(ResourceSavingArgs args)
Called when Aspose.Words saves an external resource to fixed page HTML or SVG formats.
 

    • Method Detail

      • resourceSaving

        public abstract void resourceSaving(ResourceSavingArgs args)
                                         throws java.lang.Exception
        Called when Aspose.Words saves an external resource to fixed page HTML or SVG formats.

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