com.aspose.words

Class DocumentPartSavingArgs

  • java.lang.Object
    • com.aspose.words.DocumentPartSavingArgs
public class DocumentPartSavingArgs 
extends java.lang.Object

When Aspose.Words saves a document to HTML or related formats and HtmlSaveOptions.DocumentSplitCriteria is specified, the document is split into parts and by default, each document part is saved into a separate file.

Class DocumentPartSavingArgs allows you to control how each document part will be saved. It allows to redefine how file names are generated or to completely circumvent saving of document parts into files by providing your own stream objects.

To save document parts into streams instead of files, use the DocumentPartStream property.

Example:

Shows how split a document into parts and save them.
public void documentParts() throws Exception {
    // Open a document to be converted to html
    Document doc = new Document(getMyDir() + "Rendering.doc");
    String outFileName = "SavingCallback.DocumentParts.Rendering.html";

    // We can use an appropriate SaveOptions subclass to customize the conversion process
    HtmlSaveOptions options = new HtmlSaveOptions();

    // We can use it to split a document into smaller parts, in this instance split by section breaks
    // Each part will be saved into a separate file, creating many files during the conversion process instead of just one
    options.setDocumentSplitCriteria(DocumentSplitCriteria.SECTION_BREAK);

    // We can set a callback to name each document part file ourselves
    options.setDocumentPartSavingCallback(new SavedDocumentPartRename(outFileName, options.getDocumentSplitCriteria()));

    // If we convert a document that contains images into html, we will end up with one html file which links to several images
    // Each image will be in the form of a file in the local file system
    // There is also a callback that can customize the name and file system location of each image
    options.setImageSavingCallback(new SavedImageRename(outFileName));

    // The DocumentPartSaving() and ImageSaving() methods of our callbacks will be run at this time
    doc.save(getArtifactsDir() + outFileName, options);
}

/// <summary>
/// Renames saved document parts that are produced when an HTML document is saved while being split according to a criteria
/// </summary>
private static class SavedDocumentPartRename implements IDocumentPartSavingCallback {
    public SavedDocumentPartRename(String outFileName, int documentSplitCriteria) {
        mOutFileName = outFileName;
        mDocumentSplitCriteria = documentSplitCriteria;
    }

    public void documentPartSaving(DocumentPartSavingArgs args) throws Exception {
        Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.doc"));

        String partType = "";

        switch (mDocumentSplitCriteria) {
            case DocumentSplitCriteria.PAGE_BREAK:
                partType = "Page";
                break;
            case DocumentSplitCriteria.COLUMN_BREAK:
                partType = "Column";
                break;
            case DocumentSplitCriteria.SECTION_BREAK:
                partType = "Section";
                break;
            case DocumentSplitCriteria.HEADING_PARAGRAPH:
                partType = "Paragraph from heading";
                break;
        }

        String partFileName = MessageFormat.format("{0} part {1}, of type {2}.{3}", mOutFileName, ++mCount, partType, FilenameUtils.getExtension(args.getDocumentPartFileName()));

        // We can designate the filename and location of each output file either by filename
        args.setDocumentPartFileName(partFileName);

        // Or we can make a new stream and choose the location of the file at construction
        try {
            FileOutputStream outputStream = new FileOutputStream(getArtifactsDir() + partFileName);
            args.setDocumentPartStream(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Assert.assertNotNull(args.getDocumentPartStream());
        Assert.assertFalse(args.getKeepDocumentPartStreamOpen());
    }

    private int mCount;
    private String mOutFileName;
    private int mDocumentSplitCriteria;
}

/// <summary>
/// Renames saved images that are produced when an HTML document is saved
/// </summary>
public static class SavedImageRename implements IImageSavingCallback {
    public SavedImageRename(String outFileName) {
        mOutFileName = outFileName;
    }

    public void imageSaving(ImageSavingArgs args) throws Exception {
        // Same filename and stream functions as above in IDocumentPartSavingCallback apply here
        String imageFileName = MessageFormat.format("{0} shape {1}, of type {2}.{3}", mOutFileName, ++mCount, args.getCurrentShape().getShapeType(), FilenameUtils.getExtension(args.getImageFileName()));

        args.setImageFileName(imageFileName);

        try {
            FileOutputStream outputStream = new FileOutputStream(getArtifactsDir() + imageFileName);
            args.setImageStream(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Assert.assertNotNull(args.getImageStream());
        Assert.assertTrue(args.isImageAvailable());
        Assert.assertFalse(args.getKeepImageStreamOpen());
    }

    private int mCount;
    private String mOutFileName;
}

Property Getters/Setters Summary
DocumentgetDocument()
Gets the document object that is being saved.
java.lang.StringgetDocumentPartFileName()
void
setDocumentPartFileName(java.lang.Stringvalue)
           Gets or sets the file name (without path) where the document part will be saved to.
java.io.OutputStreamgetDocumentPartStream()
void
setDocumentPartStream(java.io.OutputStreamvalue)
           Allows to specify the stream where the document part will be saved to.
booleangetKeepDocumentPartStreamOpen()
void
           Specifies whether Aspose.Words should keep the stream open or close it after saving a document part.
 

    • Property Getters/Setters Detail

      • getDocument

        public Document getDocument()
        
        Gets the document object that is being saved.

        Example:

        Shows how split a document into parts and save them.
        public void documentParts() throws Exception {
            // Open a document to be converted to html
            Document doc = new Document(getMyDir() + "Rendering.doc");
            String outFileName = "SavingCallback.DocumentParts.Rendering.html";
        
            // We can use an appropriate SaveOptions subclass to customize the conversion process
            HtmlSaveOptions options = new HtmlSaveOptions();
        
            // We can use it to split a document into smaller parts, in this instance split by section breaks
            // Each part will be saved into a separate file, creating many files during the conversion process instead of just one
            options.setDocumentSplitCriteria(DocumentSplitCriteria.SECTION_BREAK);
        
            // We can set a callback to name each document part file ourselves
            options.setDocumentPartSavingCallback(new SavedDocumentPartRename(outFileName, options.getDocumentSplitCriteria()));
        
            // If we convert a document that contains images into html, we will end up with one html file which links to several images
            // Each image will be in the form of a file in the local file system
            // There is also a callback that can customize the name and file system location of each image
            options.setImageSavingCallback(new SavedImageRename(outFileName));
        
            // The DocumentPartSaving() and ImageSaving() methods of our callbacks will be run at this time
            doc.save(getArtifactsDir() + outFileName, options);
        }
        
        /// <summary>
        /// Renames saved document parts that are produced when an HTML document is saved while being split according to a criteria
        /// </summary>
        private static class SavedDocumentPartRename implements IDocumentPartSavingCallback {
            public SavedDocumentPartRename(String outFileName, int documentSplitCriteria) {
                mOutFileName = outFileName;
                mDocumentSplitCriteria = documentSplitCriteria;
            }
        
            public void documentPartSaving(DocumentPartSavingArgs args) throws Exception {
                Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.doc"));
        
                String partType = "";
        
                switch (mDocumentSplitCriteria) {
                    case DocumentSplitCriteria.PAGE_BREAK:
                        partType = "Page";
                        break;
                    case DocumentSplitCriteria.COLUMN_BREAK:
                        partType = "Column";
                        break;
                    case DocumentSplitCriteria.SECTION_BREAK:
                        partType = "Section";
                        break;
                    case DocumentSplitCriteria.HEADING_PARAGRAPH:
                        partType = "Paragraph from heading";
                        break;
                }
        
                String partFileName = MessageFormat.format("{0} part {1}, of type {2}.{3}", mOutFileName, ++mCount, partType, FilenameUtils.getExtension(args.getDocumentPartFileName()));
        
                // We can designate the filename and location of each output file either by filename
                args.setDocumentPartFileName(partFileName);
        
                // Or we can make a new stream and choose the location of the file at construction
                try {
                    FileOutputStream outputStream = new FileOutputStream(getArtifactsDir() + partFileName);
                    args.setDocumentPartStream(outputStream);
                } catch (IOException e) {
                    e.printStackTrace();
                }
        
                Assert.assertNotNull(args.getDocumentPartStream());
                Assert.assertFalse(args.getKeepDocumentPartStreamOpen());
            }
        
            private int mCount;
            private String mOutFileName;
            private int mDocumentSplitCriteria;
        }
        
        /// <summary>
        /// Renames saved images that are produced when an HTML document is saved
        /// </summary>
        public static class SavedImageRename implements IImageSavingCallback {
            public SavedImageRename(String outFileName) {
                mOutFileName = outFileName;
            }
        
            public void imageSaving(ImageSavingArgs args) throws Exception {
                // Same filename and stream functions as above in IDocumentPartSavingCallback apply here
                String imageFileName = MessageFormat.format("{0} shape {1}, of type {2}.{3}", mOutFileName, ++mCount, args.getCurrentShape().getShapeType(), FilenameUtils.getExtension(args.getImageFileName()));
        
                args.setImageFileName(imageFileName);
        
                try {
                    FileOutputStream outputStream = new FileOutputStream(getArtifactsDir() + imageFileName);
                    args.setImageStream(outputStream);
                } catch (IOException e) {
                    e.printStackTrace();
                }
        
                Assert.assertNotNull(args.getImageStream());
                Assert.assertTrue(args.isImageAvailable());
                Assert.assertFalse(args.getKeepImageStreamOpen());
            }
        
            private int mCount;
            private String mOutFileName;
        }
      • getDocumentPartFileName/setDocumentPartFileName

        public java.lang.String getDocumentPartFileName() / public void setDocumentPartFileName(java.lang.String value)
        
        Gets or sets the file name (without path) where the document part will be saved to.

        This property allows you to redefine how the document part file names are generated during export to HTML or EPUB.

        When the callback is invoked, this property contains the file name that was generated by Aspose.Words. You can change the value of this property to save the document part into a different file. Note that the file name for each part must be unique.

        DocumentPartFileName must contain only the file name without the path. Aspose.Words determines the path for saving using the document file name. If output document file name was not specified, for instance when saving to a stream, this file name is used only for referencing document parts. The same is true when saving to EPUB format.

        Example:

        Shows how split a document into parts and save them.
        public void documentParts() throws Exception {
            // Open a document to be converted to html
            Document doc = new Document(getMyDir() + "Rendering.doc");
            String outFileName = "SavingCallback.DocumentParts.Rendering.html";
        
            // We can use an appropriate SaveOptions subclass to customize the conversion process
            HtmlSaveOptions options = new HtmlSaveOptions();
        
            // We can use it to split a document into smaller parts, in this instance split by section breaks
            // Each part will be saved into a separate file, creating many files during the conversion process instead of just one
            options.setDocumentSplitCriteria(DocumentSplitCriteria.SECTION_BREAK);
        
            // We can set a callback to name each document part file ourselves
            options.setDocumentPartSavingCallback(new SavedDocumentPartRename(outFileName, options.getDocumentSplitCriteria()));
        
            // If we convert a document that contains images into html, we will end up with one html file which links to several images
            // Each image will be in the form of a file in the local file system
            // There is also a callback that can customize the name and file system location of each image
            options.setImageSavingCallback(new SavedImageRename(outFileName));
        
            // The DocumentPartSaving() and ImageSaving() methods of our callbacks will be run at this time
            doc.save(getArtifactsDir() + outFileName, options);
        }
        
        /// <summary>
        /// Renames saved document parts that are produced when an HTML document is saved while being split according to a criteria
        /// </summary>
        private static class SavedDocumentPartRename implements IDocumentPartSavingCallback {
            public SavedDocumentPartRename(String outFileName, int documentSplitCriteria) {
                mOutFileName = outFileName;
                mDocumentSplitCriteria = documentSplitCriteria;
            }
        
            public void documentPartSaving(DocumentPartSavingArgs args) throws Exception {
                Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.doc"));
        
                String partType = "";
        
                switch (mDocumentSplitCriteria) {
                    case DocumentSplitCriteria.PAGE_BREAK:
                        partType = "Page";
                        break;
                    case DocumentSplitCriteria.COLUMN_BREAK:
                        partType = "Column";
                        break;
                    case DocumentSplitCriteria.SECTION_BREAK:
                        partType = "Section";
                        break;
                    case DocumentSplitCriteria.HEADING_PARAGRAPH:
                        partType = "Paragraph from heading";
                        break;
                }
        
                String partFileName = MessageFormat.format("{0} part {1}, of type {2}.{3}", mOutFileName, ++mCount, partType, FilenameUtils.getExtension(args.getDocumentPartFileName()));
        
                // We can designate the filename and location of each output file either by filename
                args.setDocumentPartFileName(partFileName);
        
                // Or we can make a new stream and choose the location of the file at construction
                try {
                    FileOutputStream outputStream = new FileOutputStream(getArtifactsDir() + partFileName);
                    args.setDocumentPartStream(outputStream);
                } catch (IOException e) {
                    e.printStackTrace();
                }
        
                Assert.assertNotNull(args.getDocumentPartStream());
                Assert.assertFalse(args.getKeepDocumentPartStreamOpen());
            }
        
            private int mCount;
            private String mOutFileName;
            private int mDocumentSplitCriteria;
        }
        
        /// <summary>
        /// Renames saved images that are produced when an HTML document is saved
        /// </summary>
        public static class SavedImageRename implements IImageSavingCallback {
            public SavedImageRename(String outFileName) {
                mOutFileName = outFileName;
            }
        
            public void imageSaving(ImageSavingArgs args) throws Exception {
                // Same filename and stream functions as above in IDocumentPartSavingCallback apply here
                String imageFileName = MessageFormat.format("{0} shape {1}, of type {2}.{3}", mOutFileName, ++mCount, args.getCurrentShape().getShapeType(), FilenameUtils.getExtension(args.getImageFileName()));
        
                args.setImageFileName(imageFileName);
        
                try {
                    FileOutputStream outputStream = new FileOutputStream(getArtifactsDir() + imageFileName);
                    args.setImageStream(outputStream);
                } catch (IOException e) {
                    e.printStackTrace();
                }
        
                Assert.assertNotNull(args.getImageStream());
                Assert.assertTrue(args.isImageAvailable());
                Assert.assertFalse(args.getKeepImageStreamOpen());
            }
        
            private int mCount;
            private String mOutFileName;
        }
        See Also:
        DocumentPartStream
      • getDocumentPartStream/setDocumentPartStream

        public java.io.OutputStream getDocumentPartStream() / public void setDocumentPartStream(java.io.OutputStream value)
        
        Allows to specify the stream where the document part will be saved to.

        This property allows you to save document parts to streams instead of files during HTML export.

        The default value is null. When this property is null, the document part will be saved to a file specified in the DocumentPartFileName property.

        When saving to a stream in HTML format is requested by Document.save(java.io.OutputStream,int) or Document.save(java.io.OutputStream,com.aspose.words.SaveOptions) and first document part is about to be saved, Aspose.Words suggests here the main output stream initially passed by the caller.

        When saving to EPUB format that is a container format based on HTML, DocumentPartStream cannot be specified because all subsidiary parts will be encapsulated into a single output package.

        Example:

        Shows how split a document into parts and save them.
        public void documentParts() throws Exception {
            // Open a document to be converted to html
            Document doc = new Document(getMyDir() + "Rendering.doc");
            String outFileName = "SavingCallback.DocumentParts.Rendering.html";
        
            // We can use an appropriate SaveOptions subclass to customize the conversion process
            HtmlSaveOptions options = new HtmlSaveOptions();
        
            // We can use it to split a document into smaller parts, in this instance split by section breaks
            // Each part will be saved into a separate file, creating many files during the conversion process instead of just one
            options.setDocumentSplitCriteria(DocumentSplitCriteria.SECTION_BREAK);
        
            // We can set a callback to name each document part file ourselves
            options.setDocumentPartSavingCallback(new SavedDocumentPartRename(outFileName, options.getDocumentSplitCriteria()));
        
            // If we convert a document that contains images into html, we will end up with one html file which links to several images
            // Each image will be in the form of a file in the local file system
            // There is also a callback that can customize the name and file system location of each image
            options.setImageSavingCallback(new SavedImageRename(outFileName));
        
            // The DocumentPartSaving() and ImageSaving() methods of our callbacks will be run at this time
            doc.save(getArtifactsDir() + outFileName, options);
        }
        
        /// <summary>
        /// Renames saved document parts that are produced when an HTML document is saved while being split according to a criteria
        /// </summary>
        private static class SavedDocumentPartRename implements IDocumentPartSavingCallback {
            public SavedDocumentPartRename(String outFileName, int documentSplitCriteria) {
                mOutFileName = outFileName;
                mDocumentSplitCriteria = documentSplitCriteria;
            }
        
            public void documentPartSaving(DocumentPartSavingArgs args) throws Exception {
                Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.doc"));
        
                String partType = "";
        
                switch (mDocumentSplitCriteria) {
                    case DocumentSplitCriteria.PAGE_BREAK:
                        partType = "Page";
                        break;
                    case DocumentSplitCriteria.COLUMN_BREAK:
                        partType = "Column";
                        break;
                    case DocumentSplitCriteria.SECTION_BREAK:
                        partType = "Section";
                        break;
                    case DocumentSplitCriteria.HEADING_PARAGRAPH:
                        partType = "Paragraph from heading";
                        break;
                }
        
                String partFileName = MessageFormat.format("{0} part {1}, of type {2}.{3}", mOutFileName, ++mCount, partType, FilenameUtils.getExtension(args.getDocumentPartFileName()));
        
                // We can designate the filename and location of each output file either by filename
                args.setDocumentPartFileName(partFileName);
        
                // Or we can make a new stream and choose the location of the file at construction
                try {
                    FileOutputStream outputStream = new FileOutputStream(getArtifactsDir() + partFileName);
                    args.setDocumentPartStream(outputStream);
                } catch (IOException e) {
                    e.printStackTrace();
                }
        
                Assert.assertNotNull(args.getDocumentPartStream());
                Assert.assertFalse(args.getKeepDocumentPartStreamOpen());
            }
        
            private int mCount;
            private String mOutFileName;
            private int mDocumentSplitCriteria;
        }
        
        /// <summary>
        /// Renames saved images that are produced when an HTML document is saved
        /// </summary>
        public static class SavedImageRename implements IImageSavingCallback {
            public SavedImageRename(String outFileName) {
                mOutFileName = outFileName;
            }
        
            public void imageSaving(ImageSavingArgs args) throws Exception {
                // Same filename and stream functions as above in IDocumentPartSavingCallback apply here
                String imageFileName = MessageFormat.format("{0} shape {1}, of type {2}.{3}", mOutFileName, ++mCount, args.getCurrentShape().getShapeType(), FilenameUtils.getExtension(args.getImageFileName()));
        
                args.setImageFileName(imageFileName);
        
                try {
                    FileOutputStream outputStream = new FileOutputStream(getArtifactsDir() + imageFileName);
                    args.setImageStream(outputStream);
                } catch (IOException e) {
                    e.printStackTrace();
                }
        
                Assert.assertNotNull(args.getImageStream());
                Assert.assertTrue(args.isImageAvailable());
                Assert.assertFalse(args.getKeepImageStreamOpen());
            }
        
            private int mCount;
            private String mOutFileName;
        }
        See Also:
        KeepDocumentPartStreamOpen
      • getKeepDocumentPartStreamOpen/setKeepDocumentPartStreamOpen

        public boolean getKeepDocumentPartStreamOpen() / public void setKeepDocumentPartStreamOpen(boolean value)
        
        Specifies whether Aspose.Words should keep the stream open or close it after saving a document part.

        Default is false and Aspose.Words will close the stream you provided in the DocumentPartStream property after writing a document part into it. Specify true to keep the stream open. Please note that the main output stream provided in the call to Document.save(java.io.OutputStream,int) or Document.save(java.io.OutputStream,com.aspose.words.SaveOptions) will never be closed by Aspose.Words even if KeepDocumentPartStreamOpen is set to false.

        Example:

        Shows how split a document into parts and save them.
        public void documentParts() throws Exception {
            // Open a document to be converted to html
            Document doc = new Document(getMyDir() + "Rendering.doc");
            String outFileName = "SavingCallback.DocumentParts.Rendering.html";
        
            // We can use an appropriate SaveOptions subclass to customize the conversion process
            HtmlSaveOptions options = new HtmlSaveOptions();
        
            // We can use it to split a document into smaller parts, in this instance split by section breaks
            // Each part will be saved into a separate file, creating many files during the conversion process instead of just one
            options.setDocumentSplitCriteria(DocumentSplitCriteria.SECTION_BREAK);
        
            // We can set a callback to name each document part file ourselves
            options.setDocumentPartSavingCallback(new SavedDocumentPartRename(outFileName, options.getDocumentSplitCriteria()));
        
            // If we convert a document that contains images into html, we will end up with one html file which links to several images
            // Each image will be in the form of a file in the local file system
            // There is also a callback that can customize the name and file system location of each image
            options.setImageSavingCallback(new SavedImageRename(outFileName));
        
            // The DocumentPartSaving() and ImageSaving() methods of our callbacks will be run at this time
            doc.save(getArtifactsDir() + outFileName, options);
        }
        
        /// <summary>
        /// Renames saved document parts that are produced when an HTML document is saved while being split according to a criteria
        /// </summary>
        private static class SavedDocumentPartRename implements IDocumentPartSavingCallback {
            public SavedDocumentPartRename(String outFileName, int documentSplitCriteria) {
                mOutFileName = outFileName;
                mDocumentSplitCriteria = documentSplitCriteria;
            }
        
            public void documentPartSaving(DocumentPartSavingArgs args) throws Exception {
                Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.doc"));
        
                String partType = "";
        
                switch (mDocumentSplitCriteria) {
                    case DocumentSplitCriteria.PAGE_BREAK:
                        partType = "Page";
                        break;
                    case DocumentSplitCriteria.COLUMN_BREAK:
                        partType = "Column";
                        break;
                    case DocumentSplitCriteria.SECTION_BREAK:
                        partType = "Section";
                        break;
                    case DocumentSplitCriteria.HEADING_PARAGRAPH:
                        partType = "Paragraph from heading";
                        break;
                }
        
                String partFileName = MessageFormat.format("{0} part {1}, of type {2}.{3}", mOutFileName, ++mCount, partType, FilenameUtils.getExtension(args.getDocumentPartFileName()));
        
                // We can designate the filename and location of each output file either by filename
                args.setDocumentPartFileName(partFileName);
        
                // Or we can make a new stream and choose the location of the file at construction
                try {
                    FileOutputStream outputStream = new FileOutputStream(getArtifactsDir() + partFileName);
                    args.setDocumentPartStream(outputStream);
                } catch (IOException e) {
                    e.printStackTrace();
                }
        
                Assert.assertNotNull(args.getDocumentPartStream());
                Assert.assertFalse(args.getKeepDocumentPartStreamOpen());
            }
        
            private int mCount;
            private String mOutFileName;
            private int mDocumentSplitCriteria;
        }
        
        /// <summary>
        /// Renames saved images that are produced when an HTML document is saved
        /// </summary>
        public static class SavedImageRename implements IImageSavingCallback {
            public SavedImageRename(String outFileName) {
                mOutFileName = outFileName;
            }
        
            public void imageSaving(ImageSavingArgs args) throws Exception {
                // Same filename and stream functions as above in IDocumentPartSavingCallback apply here
                String imageFileName = MessageFormat.format("{0} shape {1}, of type {2}.{3}", mOutFileName, ++mCount, args.getCurrentShape().getShapeType(), FilenameUtils.getExtension(args.getImageFileName()));
        
                args.setImageFileName(imageFileName);
        
                try {
                    FileOutputStream outputStream = new FileOutputStream(getArtifactsDir() + imageFileName);
                    args.setImageStream(outputStream);
                } catch (IOException e) {
                    e.printStackTrace();
                }
        
                Assert.assertNotNull(args.getImageStream());
                Assert.assertTrue(args.isImageAvailable());
                Assert.assertFalse(args.getKeepImageStreamOpen());
            }
        
            private int mCount;
            private String mOutFileName;
        }
        See Also:
        DocumentPartStream