ICssSavingCallback Interface

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

Namespace:  Aspose.Words.Saving
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public interface ICssSavingCallback

The ICssSavingCallback type exposes the following members.

Methods
  NameDescription
Public methodCode exampleCssSaving
Called when Aspose.Words saves an CSS (Cascading Style Sheet).
Examples
Shows how to work with CSS stylesheets that may be created along with Html documents.
public void CssSavingCallback()
{
    // Open a document to be converted to html
    Document doc = new Document(MyDir + "Rendering.docx");

    // 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.CssStyleSheetType = CssStyleSheetType.External;

    // We can designate a filename for our stylesheet like this
    options.CssStyleSheetFileName = ArtifactsDir + "SavingCallback.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.CssSavingCallback =
        new CustomCssSavingCallback(ArtifactsDir + "SavingCallback.CssSavingCallback.css", true, false);

    // The CssSaving() method of our callback will be called at this stage
    doc.Save(ArtifactsDir + "SavingCallback.CssSavingCallback.html", options);
}

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

    public void CssSaving(CssSavingArgs args)
    {
        // Set up the stream that will create the CSS document         
        args.CssStream = new FileStream(mCssTextFileName, FileMode.Create);
        Assert.True(args.CssStream.CanWrite);
        args.IsExportNeeded = mIsExportNeeded;
        args.KeepCssStreamOpen = mKeepCssStreamOpen;

        // We can also access the original document here like this
        Assert.True(args.Document.OriginalFileName.EndsWith("Rendering.docx"));
    }

    private readonly string mCssTextFileName;
    private readonly bool mIsExportNeeded;
    private readonly bool mKeepCssStreamOpen;
}
See Also