CssSavingArgs Class

Provides data for the CssSaving(CssSavingArgs) event.
Inheritance Hierarchy
SystemObject
  Aspose.Words.SavingCssSavingArgs

Namespace:  Aspose.Words.Saving
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public class CssSavingArgs

The CssSavingArgs type exposes the following members.

Properties
  NameDescription
Public propertyCode exampleCssStream
Allows to specify the stream where the CSS information will be saved to.
Public propertyCode exampleDocument
Gets the document object that is currently being saved.
Public propertyCode exampleIsExportNeeded
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.
Public propertyCode exampleKeepCssStreamOpen
Specifies whether Aspose.Words should keep the stream open or close it after saving an CSS information.
Methods
  NameDescription
Public methodEquals (Inherited from Object.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Public methodToString (Inherited from Object.)
Remarks

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.

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