com.aspose.words

Interface IPageSavingCallback

  • public interface IPageSavingCallback 

Implement this interface if you want to control how Aspose.Words saves separate pages when saving a document to fixed page formats.

Example:

Shows how to use a callback to save a document to HTML page by page.
public void pageFileNames() throws Exception {
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    builder.writeln("Page 1.");
    builder.insertBreak(BreakType.PAGE_BREAK);
    builder.writeln("Page 2.");
    builder.insertImage(getImageDir() + "Logo.jpg");
    builder.insertBreak(BreakType.PAGE_BREAK);
    builder.writeln("Page 3.");

    // Create an "HtmlFixedSaveOptions" object, which we can pass to the document's "Save" method
    // to modify how we convert the document to HTML.
    HtmlFixedSaveOptions htmlFixedSaveOptions = new HtmlFixedSaveOptions();

    // We will save each page in this document to a separate HTML file in the local file system.
    // Set a callback that allows us to name each output HTML document.
    htmlFixedSaveOptions.setPageSavingCallback(new CustomFileNamePageSavingCallback());

    doc.save(getArtifactsDir() + "SavingCallback.PageFileNames.html", htmlFixedSaveOptions);

    String[] filePaths = DocumentHelper.directoryGetFiles(getArtifactsDir(), "SavingCallback.PageFileNames.Page_*").toArray(new String[0]);

    Assert.assertEquals(3, filePaths.length);
}

/// <summary>
/// Saves all pages to a file and directory specified within.
/// </summary>
private static class CustomFileNamePageSavingCallback implements IPageSavingCallback {
    public void pageSaving(PageSavingArgs args) throws Exception {
        String outFileName = MessageFormat.format("{0}SavingCallback.PageFileNames.Page_{1}.html", getArtifactsDir(), args.getPageIndex());

        // Below are two ways of specifying where Aspose.Words will save each page of the document.
        // 1 -  Set a filename for the output page file:
        args.setPageFileName(outFileName);

        // 2 -  Create a custom stream for the output page file:
        try (FileOutputStream outputStream = new FileOutputStream(outFileName)) {
            args.setPageStream(outputStream);
        }

        Assert.assertFalse(args.getKeepPageStreamOpen());
    }
}

Method Summary
abstract voidpageSaving(PageSavingArgs args)
Called when Aspose.Words saves a separate page to fixed page formats.
 

    • Method Detail

      • pageSaving

        public abstract void pageSaving(PageSavingArgs args)
                                     throws java.lang.Exception
        Called when Aspose.Words saves a separate page to fixed page formats.

        Example:

        Shows how to use a callback to save a document to HTML page by page.
        public void pageFileNames() throws Exception {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
        
            builder.writeln("Page 1.");
            builder.insertBreak(BreakType.PAGE_BREAK);
            builder.writeln("Page 2.");
            builder.insertImage(getImageDir() + "Logo.jpg");
            builder.insertBreak(BreakType.PAGE_BREAK);
            builder.writeln("Page 3.");
        
            // Create an "HtmlFixedSaveOptions" object, which we can pass to the document's "Save" method
            // to modify how we convert the document to HTML.
            HtmlFixedSaveOptions htmlFixedSaveOptions = new HtmlFixedSaveOptions();
        
            // We will save each page in this document to a separate HTML file in the local file system.
            // Set a callback that allows us to name each output HTML document.
            htmlFixedSaveOptions.setPageSavingCallback(new CustomFileNamePageSavingCallback());
        
            doc.save(getArtifactsDir() + "SavingCallback.PageFileNames.html", htmlFixedSaveOptions);
        
            String[] filePaths = DocumentHelper.directoryGetFiles(getArtifactsDir(), "SavingCallback.PageFileNames.Page_*").toArray(new String[0]);
        
            Assert.assertEquals(3, filePaths.length);
        }
        
        /// <summary>
        /// Saves all pages to a file and directory specified within.
        /// </summary>
        private static class CustomFileNamePageSavingCallback implements IPageSavingCallback {
            public void pageSaving(PageSavingArgs args) throws Exception {
                String outFileName = MessageFormat.format("{0}SavingCallback.PageFileNames.Page_{1}.html", getArtifactsDir(), args.getPageIndex());
        
                // Below are two ways of specifying where Aspose.Words will save each page of the document.
                // 1 -  Set a filename for the output page file:
                args.setPageFileName(outFileName);
        
                // 2 -  Create a custom stream for the output page file:
                try (FileOutputStream outputStream = new FileOutputStream(outFileName)) {
                    args.setPageStream(outputStream);
                }
        
                Assert.assertFalse(args.getKeepPageStreamOpen());
            }
        }