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 an HTML conversion creates.
public void externalCssFilenames() throws Exception
{
    Document doc = new Document(getMyDir() + "Rendering.docx");

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

    // Set the "CssStylesheetType" property to "CssStyleSheetType.External" to
    // accompany a saved HTML document with an external CSS stylesheet file.
    options.setCssStyleSheetType(CssStyleSheetType.EXTERNAL);

    // Below are two ways of specifying directories and filenames for output CSS stylesheets.
    // 1 -  Use the "CssStyleSheetFileName" property to assign a filename to our stylesheet:
    options.setCssStyleSheetFileName(getArtifactsDir() + "SavingCallback.ExternalCssFilenames.css");

    // 2 -  Use a custom callback to name our stylesheet:
    options.setCssSavingCallback(new CustomCssSavingCallback(getArtifactsDir() + "SavingCallback.ExternalCssFilenames.css", true, false));

    doc.save(getArtifactsDir() + "SavingCallback.ExternalCssFilenames.html", options);
}

/// <summary>
/// Sets a custom filename, along with other parameters for an external 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
    {
        // We can access the entire source document via the "Document" property.
        Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.docx"));

        args.setCssStream(new FileOutputStream(mCssTextFileName));
        args.isExportNeeded(mIsExportNeeded);
        args.setKeepCssStreamOpen(mKeepCssStreamOpen);
    }

    private final String mCssTextFileName;
    private final boolean mIsExportNeeded;
    private final 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 an HTML conversion creates.
        public void externalCssFilenames() throws Exception
        {
            Document doc = new Document(getMyDir() + "Rendering.docx");
        
            // Create an "HtmlFixedSaveOptions" object, which we can pass to the document's "Save" method
            // to modify how we convert the document to HTML.
            HtmlSaveOptions options = new HtmlSaveOptions();
        
            // Set the "CssStylesheetType" property to "CssStyleSheetType.External" to
            // accompany a saved HTML document with an external CSS stylesheet file.
            options.setCssStyleSheetType(CssStyleSheetType.EXTERNAL);
        
            // Below are two ways of specifying directories and filenames for output CSS stylesheets.
            // 1 -  Use the "CssStyleSheetFileName" property to assign a filename to our stylesheet:
            options.setCssStyleSheetFileName(getArtifactsDir() + "SavingCallback.ExternalCssFilenames.css");
        
            // 2 -  Use a custom callback to name our stylesheet:
            options.setCssSavingCallback(new CustomCssSavingCallback(getArtifactsDir() + "SavingCallback.ExternalCssFilenames.css", true, false));
        
            doc.save(getArtifactsDir() + "SavingCallback.ExternalCssFilenames.html", options);
        }
        
        /// <summary>
        /// Sets a custom filename, along with other parameters for an external 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
            {
                // We can access the entire source document via the "Document" property.
                Assert.assertTrue(args.getDocument().getOriginalFileName().endsWith("Rendering.docx"));
        
                args.setCssStream(new FileOutputStream(mCssTextFileName));
                args.isExportNeeded(mIsExportNeeded);
                args.setKeepCssStreamOpen(mKeepCssStreamOpen);
            }
        
            private final String mCssTextFileName;
            private final boolean mIsExportNeeded;
            private final boolean mKeepCssStreamOpen;
        }