com.aspose.words

Interface ICssSavingCallback

  • public interface ICssSavingCallback 

Implement this interface if you want to control how Aspose.Words saves CSS (Cascading Style Sheet) when saving a document to HTML.

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 options = new HtmlSaveOptions();

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

    // We can designate a filename for our stylesheet like this
    options.setCssStyleSheetFileName(getArtifactsDir() + "Rendering.CssSavingCallback.css");

    // A custom ICssSavingCallback implementation can also control where that stylesheet will be saved and linked to by the Html document
    // This callback will override the filename we specified above in options.CssStyleSheetFileName,
    // but will give us more control over the saving process
    options.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", options);
}

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

Method Summary
abstract voidcssSaving(CssSavingArgs args)
Called when Aspose.Words saves an CSS (Cascading Style Sheet).
 

    • Method Detail

      • cssSaving

        public abstract void cssSaving(CssSavingArgs args)
                                    throws java.lang.Exception
        Called when Aspose.Words saves an CSS (Cascading Style Sheet).

        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 options = new HtmlSaveOptions();
        
            // By default, a CSS stylesheet is stored inside its HTML document, but we can have it saved to a separate file
            options.setCssStyleSheetType(CssStyleSheetType.EXTERNAL);
        
            // We can designate a filename for our stylesheet like this
            options.setCssStyleSheetFileName(getArtifactsDir() + "Rendering.CssSavingCallback.css");
        
            // A custom ICssSavingCallback implementation can also control where that stylesheet will be saved and linked to by the Html document
            // This callback will override the filename we specified above in options.CssStyleSheetFileName,
            // but will give us more control over the saving process
            options.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", options);
        }
        
        /// <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;
        }