Class CssSavingArgs

Class CssSavingArgs

Namespace: Aspose.Words.Saving
Assembly: Aspose.Words.dll (25.12.0)

Provides data for the Aspose.Words.Saving.ICssSavingCallback.CssSaving(Aspose.Words.Saving.CssSavingArgs) event.

To learn more, visit the Save a Document documentation article.

public class CssSavingArgs

Inheritance

object CssSavingArgs

Inherited Members

object.GetType() , object.MemberwiseClone() , object.ToString() , object.Equals(object?) , object.Equals(object?, object?) , object.ReferenceEquals(object?, object?) , object.GetHashCode()

Examples

Shows how to work with CSS stylesheets that an HTML conversion creates.

public void ExternalCssFilenames()
                                                                                  {
                                                                                      Document doc = new Document(MyDir + "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.CssStyleSheetType = 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.CssStyleSheetFileName = ArtifactsDir + "SavingCallback.ExternalCssFilenames.css";

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

                                                                                      doc.Save(ArtifactsDir + "SavingCallback.ExternalCssFilenames.html", options);
                                                                                  }

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

                                                                                          args.CssStream = new FileStream(mCssTextFileName, FileMode.Create);
                                                                                          args.IsExportNeeded = mIsExportNeeded;
                                                                                          args.KeepCssStreamOpen = mKeepCssStreamOpen;

                                                                                          Assert.That(args.CssStream.CanWrite, Is.True);
                                                                                      }

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

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).

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

To save CSS into stream, use the Aspose.Words.Saving.CssSavingArgs.CssStream property.

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

Properties

CssStream

Allows to specify the stream where the CSS information will be saved to.

public Stream CssStream { get; set; }

Property Value

Stream

Examples

Shows how to work with CSS stylesheets that an HTML conversion creates.

public void ExternalCssFilenames()
                                                                                  {
                                                                                      Document doc = new Document(MyDir + "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.CssStyleSheetType = 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.CssStyleSheetFileName = ArtifactsDir + "SavingCallback.ExternalCssFilenames.css";

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

                                                                                      doc.Save(ArtifactsDir + "SavingCallback.ExternalCssFilenames.html", options);
                                                                                  }

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

                                                                                          args.CssStream = new FileStream(mCssTextFileName, FileMode.Create);
                                                                                          args.IsExportNeeded = mIsExportNeeded;
                                                                                          args.KeepCssStreamOpen = mKeepCssStreamOpen;

                                                                                          Assert.That(args.CssStream.CanWrite, Is.True);
                                                                                      }

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

Remarks

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 Aspose.Words.Saving.CssSavingArgs.IsExportNeeded property.

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

Aspose.Words.Saving.CssSavingArgs.KeepCssStreamOpen

Document

Gets the document object that is currently being saved.

public Document Document { get; }

Property Value

Document

Examples

Shows how to work with CSS stylesheets that an HTML conversion creates.

public void ExternalCssFilenames()
                                                                                  {
                                                                                      Document doc = new Document(MyDir + "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.CssStyleSheetType = 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.CssStyleSheetFileName = ArtifactsDir + "SavingCallback.ExternalCssFilenames.css";

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

                                                                                      doc.Save(ArtifactsDir + "SavingCallback.ExternalCssFilenames.html", options);
                                                                                  }

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

                                                                                          args.CssStream = new FileStream(mCssTextFileName, FileMode.Create);
                                                                                          args.IsExportNeeded = mIsExportNeeded;
                                                                                          args.KeepCssStreamOpen = mKeepCssStreamOpen;

                                                                                          Assert.That(args.CssStream.CanWrite, Is.True);
                                                                                      }

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

IsExportNeeded

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 bool IsExportNeeded { get; set; }

Property Value

bool

Examples

Shows how to work with CSS stylesheets that an HTML conversion creates.

public void ExternalCssFilenames()
                                                                                  {
                                                                                      Document doc = new Document(MyDir + "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.CssStyleSheetType = 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.CssStyleSheetFileName = ArtifactsDir + "SavingCallback.ExternalCssFilenames.css";

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

                                                                                      doc.Save(ArtifactsDir + "SavingCallback.ExternalCssFilenames.html", options);
                                                                                  }

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

                                                                                          args.CssStream = new FileStream(mCssTextFileName, FileMode.Create);
                                                                                          args.IsExportNeeded = mIsExportNeeded;
                                                                                          args.KeepCssStreamOpen = mKeepCssStreamOpen;

                                                                                          Assert.That(args.CssStream.CanWrite, Is.True);
                                                                                      }

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

KeepCssStreamOpen

Specifies whether Aspose.Words should keep the stream open or close it after saving an CSS information.

public bool KeepCssStreamOpen { get; set; }

Property Value

bool

Examples

Shows how to work with CSS stylesheets that an HTML conversion creates.

public void ExternalCssFilenames()
                                                                                  {
                                                                                      Document doc = new Document(MyDir + "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.CssStyleSheetType = 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.CssStyleSheetFileName = ArtifactsDir + "SavingCallback.ExternalCssFilenames.css";

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

                                                                                      doc.Save(ArtifactsDir + "SavingCallback.ExternalCssFilenames.html", options);
                                                                                  }

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

                                                                                          args.CssStream = new FileStream(mCssTextFileName, FileMode.Create);
                                                                                          args.IsExportNeeded = mIsExportNeeded;
                                                                                          args.KeepCssStreamOpen = mKeepCssStreamOpen;

                                                                                          Assert.That(args.CssStream.CanWrite, Is.True);
                                                                                      }

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

Remarks

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

Aspose.Words.Saving.CssSavingArgs.CssStream
 English