com.aspose.words

Class CssSavingArgs

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

By default, when Aspose.Words saves a document to HTML, it saves CSS information inline (as a value of the style attribute on every element).

CssSavingArgs allows to save CSS information into file by providing your own stream object.

To save CSS into stream, use the CssStream property.

To suppress saving CSS into a file and embedding to HTML document use the IsExportNeeded property.

Example:

Shows how to work with CSS stylesheets that may be created along with Html documents.
public void cssSavingCallback() throws Exception {
    // Open a document to be converted to html
    Document doc = new Document(getMyDir() + "Rendering.doc");

    // If our output document will produce a CSS stylesheet, we can use an HtmlSaveOptions to control where it is saved
    HtmlSaveOptions htmlFixedSaveOptions = new HtmlSaveOptions();

    // By default, a CSS stylesheet is stored inside its HTML document, but we can have it saved to a separate file
    htmlFixedSaveOptions.setCssStyleSheetType(CssStyleSheetType.EXTERNAL);

    // A custom ICssSavingCallback implementation can control where that stylesheet will be saved and linked to by the Html document
    htmlFixedSaveOptions.setCssSavingCallback(new CustomCssSavingCallback(getArtifactsDir() + "Rendering.CssSavingCallback.css", true, false));

    // The CssSaving() method of our callback will be called at this stage
    doc.save(getArtifactsDir() + "Rendering.CssSavingCallback.html", htmlFixedSaveOptions);
}

/// <summary>
/// Designates a filename and other parameters for the saving of a CSS stylesheet
/// </summary>
private static class CustomCssSavingCallback implements ICssSavingCallback {
    public CustomCssSavingCallback(String cssDocFilename, boolean isExportNeeded, boolean keepCssStreamOpen) {
        mCssTextFileName = cssDocFilename;
        mIsExportNeeded = isExportNeeded;
        mKeepCssStreamOpen = keepCssStreamOpen;
    }

    public void cssSaving(CssSavingArgs args) throws Exception {
        Assert.assertNull(args.getCssStream());
        // Set up the stream that will create the CSS document
        args.setCssStream(new FileOutputStream(mCssTextFileName));
        Assert.assertNotNull(args.getCssStream());
        args.isExportNeeded(mIsExportNeeded);
        args.setKeepCssStreamOpen(mKeepCssStreamOpen);

        // We can also access the original document here like this
        Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.doc"));
    }

    private String mCssTextFileName;
    private boolean mIsExportNeeded;
    private boolean mKeepCssStreamOpen;
}

Property Getters/Setters Summary
java.io.OutputStreamgetCssStream()
void
setCssStream(java.io.OutputStreamvalue)
           Allows to specify the stream where the CSS information will be saved to.
DocumentgetDocument()
Gets the document object that is currently being saved.
booleanisExportNeeded()
void
isExportNeeded(booleanvalue)
           Allows to specify whether the CSS will be exported to file and embedded to HTML document. Default is true. When this property is false, the CSS information will not be saved to a CSS file and will not be embedded to HTML document.
booleangetKeepCssStreamOpen()
void
setKeepCssStreamOpen(booleanvalue)
           Specifies whether Aspose.Words should keep the stream open or close it after saving an CSS information.
 

    • Property Getters/Setters Detail

      • getCssStream/setCssStream

        public java.io.OutputStream getCssStream() / public void setCssStream(java.io.OutputStream value)
        
        Allows to specify the stream where the CSS information will be saved to.

        This property allows you to save CSS information to a stream.

        The default value is null. This property doesn't suppress saving CSS information to a file or embedding to HTML document. To suppress exporting CSS use the IsExportNeeded property.

        Using ICssSavingCallback you cannot substitute CSS with another. It is intended only for saving CSS to a stream.

        Example:

        Shows how to work with CSS stylesheets that may be created along with Html documents.
        public void cssSavingCallback() throws Exception {
            // Open a document to be converted to html
            Document doc = new Document(getMyDir() + "Rendering.doc");
        
            // If our output document will produce a CSS stylesheet, we can use an HtmlSaveOptions to control where it is saved
            HtmlSaveOptions htmlFixedSaveOptions = new HtmlSaveOptions();
        
            // By default, a CSS stylesheet is stored inside its HTML document, but we can have it saved to a separate file
            htmlFixedSaveOptions.setCssStyleSheetType(CssStyleSheetType.EXTERNAL);
        
            // A custom ICssSavingCallback implementation can control where that stylesheet will be saved and linked to by the Html document
            htmlFixedSaveOptions.setCssSavingCallback(new CustomCssSavingCallback(getArtifactsDir() + "Rendering.CssSavingCallback.css", true, false));
        
            // The CssSaving() method of our callback will be called at this stage
            doc.save(getArtifactsDir() + "Rendering.CssSavingCallback.html", htmlFixedSaveOptions);
        }
        
        /// <summary>
        /// Designates a filename and other parameters for the saving of a CSS stylesheet
        /// </summary>
        private static class CustomCssSavingCallback implements ICssSavingCallback {
            public CustomCssSavingCallback(String cssDocFilename, boolean isExportNeeded, boolean keepCssStreamOpen) {
                mCssTextFileName = cssDocFilename;
                mIsExportNeeded = isExportNeeded;
                mKeepCssStreamOpen = keepCssStreamOpen;
            }
        
            public void cssSaving(CssSavingArgs args) throws Exception {
                Assert.assertNull(args.getCssStream());
                // Set up the stream that will create the CSS document
                args.setCssStream(new FileOutputStream(mCssTextFileName));
                Assert.assertNotNull(args.getCssStream());
                args.isExportNeeded(mIsExportNeeded);
                args.setKeepCssStreamOpen(mKeepCssStreamOpen);
        
                // We can also access the original document here like this
                Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.doc"));
            }
        
            private String mCssTextFileName;
            private boolean mIsExportNeeded;
            private boolean mKeepCssStreamOpen;
        }
        See Also:
        KeepCssStreamOpen
      • getDocument

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

        Example:

        Shows how to work with CSS stylesheets that may be created along with Html documents.
        public void cssSavingCallback() throws Exception {
            // Open a document to be converted to html
            Document doc = new Document(getMyDir() + "Rendering.doc");
        
            // If our output document will produce a CSS stylesheet, we can use an HtmlSaveOptions to control where it is saved
            HtmlSaveOptions htmlFixedSaveOptions = new HtmlSaveOptions();
        
            // By default, a CSS stylesheet is stored inside its HTML document, but we can have it saved to a separate file
            htmlFixedSaveOptions.setCssStyleSheetType(CssStyleSheetType.EXTERNAL);
        
            // A custom ICssSavingCallback implementation can control where that stylesheet will be saved and linked to by the Html document
            htmlFixedSaveOptions.setCssSavingCallback(new CustomCssSavingCallback(getArtifactsDir() + "Rendering.CssSavingCallback.css", true, false));
        
            // The CssSaving() method of our callback will be called at this stage
            doc.save(getArtifactsDir() + "Rendering.CssSavingCallback.html", htmlFixedSaveOptions);
        }
        
        /// <summary>
        /// Designates a filename and other parameters for the saving of a CSS stylesheet
        /// </summary>
        private static class CustomCssSavingCallback implements ICssSavingCallback {
            public CustomCssSavingCallback(String cssDocFilename, boolean isExportNeeded, boolean keepCssStreamOpen) {
                mCssTextFileName = cssDocFilename;
                mIsExportNeeded = isExportNeeded;
                mKeepCssStreamOpen = keepCssStreamOpen;
            }
        
            public void cssSaving(CssSavingArgs args) throws Exception {
                Assert.assertNull(args.getCssStream());
                // Set up the stream that will create the CSS document
                args.setCssStream(new FileOutputStream(mCssTextFileName));
                Assert.assertNotNull(args.getCssStream());
                args.isExportNeeded(mIsExportNeeded);
                args.setKeepCssStreamOpen(mKeepCssStreamOpen);
        
                // We can also access the original document here like this
                Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.doc"));
            }
        
            private String mCssTextFileName;
            private boolean mIsExportNeeded;
            private boolean mKeepCssStreamOpen;
        }
      • isExportNeeded/isExportNeeded

        public boolean isExportNeeded() / public void isExportNeeded(boolean value)
        
        Allows to specify whether the CSS will be exported to file and embedded to HTML document. Default is true. When this property is false, the CSS information will not be saved to a CSS file and will not be embedded to HTML document.

        Example:

        Shows how to work with CSS stylesheets that may be created along with Html documents.
        public void cssSavingCallback() throws Exception {
            // Open a document to be converted to html
            Document doc = new Document(getMyDir() + "Rendering.doc");
        
            // If our output document will produce a CSS stylesheet, we can use an HtmlSaveOptions to control where it is saved
            HtmlSaveOptions htmlFixedSaveOptions = new HtmlSaveOptions();
        
            // By default, a CSS stylesheet is stored inside its HTML document, but we can have it saved to a separate file
            htmlFixedSaveOptions.setCssStyleSheetType(CssStyleSheetType.EXTERNAL);
        
            // A custom ICssSavingCallback implementation can control where that stylesheet will be saved and linked to by the Html document
            htmlFixedSaveOptions.setCssSavingCallback(new CustomCssSavingCallback(getArtifactsDir() + "Rendering.CssSavingCallback.css", true, false));
        
            // The CssSaving() method of our callback will be called at this stage
            doc.save(getArtifactsDir() + "Rendering.CssSavingCallback.html", htmlFixedSaveOptions);
        }
        
        /// <summary>
        /// Designates a filename and other parameters for the saving of a CSS stylesheet
        /// </summary>
        private static class CustomCssSavingCallback implements ICssSavingCallback {
            public CustomCssSavingCallback(String cssDocFilename, boolean isExportNeeded, boolean keepCssStreamOpen) {
                mCssTextFileName = cssDocFilename;
                mIsExportNeeded = isExportNeeded;
                mKeepCssStreamOpen = keepCssStreamOpen;
            }
        
            public void cssSaving(CssSavingArgs args) throws Exception {
                Assert.assertNull(args.getCssStream());
                // Set up the stream that will create the CSS document
                args.setCssStream(new FileOutputStream(mCssTextFileName));
                Assert.assertNotNull(args.getCssStream());
                args.isExportNeeded(mIsExportNeeded);
                args.setKeepCssStreamOpen(mKeepCssStreamOpen);
        
                // We can also access the original document here like this
                Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.doc"));
            }
        
            private String mCssTextFileName;
            private boolean mIsExportNeeded;
            private boolean mKeepCssStreamOpen;
        }
      • getKeepCssStreamOpen/setKeepCssStreamOpen

        public boolean getKeepCssStreamOpen() / public void setKeepCssStreamOpen(boolean value)
        
        Specifies whether Aspose.Words should keep the stream open or close it after saving an CSS information.

        Default is false and Aspose.Words will close the stream you provided in the CssStream property after writing an CSS information into it. Specify true to keep the stream open.

        Example:

        Shows how to work with CSS stylesheets that may be created along with Html documents.
        public void cssSavingCallback() throws Exception {
            // Open a document to be converted to html
            Document doc = new Document(getMyDir() + "Rendering.doc");
        
            // If our output document will produce a CSS stylesheet, we can use an HtmlSaveOptions to control where it is saved
            HtmlSaveOptions htmlFixedSaveOptions = new HtmlSaveOptions();
        
            // By default, a CSS stylesheet is stored inside its HTML document, but we can have it saved to a separate file
            htmlFixedSaveOptions.setCssStyleSheetType(CssStyleSheetType.EXTERNAL);
        
            // A custom ICssSavingCallback implementation can control where that stylesheet will be saved and linked to by the Html document
            htmlFixedSaveOptions.setCssSavingCallback(new CustomCssSavingCallback(getArtifactsDir() + "Rendering.CssSavingCallback.css", true, false));
        
            // The CssSaving() method of our callback will be called at this stage
            doc.save(getArtifactsDir() + "Rendering.CssSavingCallback.html", htmlFixedSaveOptions);
        }
        
        /// <summary>
        /// Designates a filename and other parameters for the saving of a CSS stylesheet
        /// </summary>
        private static class CustomCssSavingCallback implements ICssSavingCallback {
            public CustomCssSavingCallback(String cssDocFilename, boolean isExportNeeded, boolean keepCssStreamOpen) {
                mCssTextFileName = cssDocFilename;
                mIsExportNeeded = isExportNeeded;
                mKeepCssStreamOpen = keepCssStreamOpen;
            }
        
            public void cssSaving(CssSavingArgs args) throws Exception {
                Assert.assertNull(args.getCssStream());
                // Set up the stream that will create the CSS document
                args.setCssStream(new FileOutputStream(mCssTextFileName));
                Assert.assertNotNull(args.getCssStream());
                args.isExportNeeded(mIsExportNeeded);
                args.setKeepCssStreamOpen(mKeepCssStreamOpen);
        
                // We can also access the original document here like this
                Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.doc"));
            }
        
            private String mCssTextFileName;
            private boolean mIsExportNeeded;
            private boolean mKeepCssStreamOpen;
        }
        See Also:
        CssStream