Class ResourceSavingArgs

Class ResourceSavingArgs

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

Provides data for the Aspose.Words.Saving.IResourceSavingCallback.ResourceSaving(Aspose.Words.Saving.ResourceSavingArgs) event.

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

public class ResourceSavingArgs

Inheritance

object ResourceSavingArgs

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 use a callback to track external resources created while converting a document to HTML.

public void ResourceSavingCallback()
                                                                                                               {
                                                                                                                   Document doc = new Document(MyDir + "Bullet points with alternative font.docx");

                                                                                                                   FontSavingCallback callback = new FontSavingCallback();

                                                                                                                   HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions
                                                                                                                   {
                                                                                                                       ResourceSavingCallback = callback
                                                                                                                   };

                                                                                                                   doc.Save(ArtifactsDir + "HtmlFixedSaveOptions.UsingMachineFonts.html", saveOptions);

                                                                                                                   Console.WriteLine(callback.GetText());
                                                                                                               }

                                                                                                               private class FontSavingCallback : IResourceSavingCallback
                                                                                                               {
                                                                                                                   /// <summary>
                                                                                                                   /// Called when Aspose.Words saves an external resource to fixed page HTML or SVG.
                                                                                                                   /// </summary>
                                                                                                                   public void ResourceSaving(ResourceSavingArgs args)
                                                                                                                   {
                                                                                                                       mText.AppendLine($"Original document URI:\t{args.Document.OriginalFileName}");
                                                                                                                       mText.AppendLine($"Resource being saved:\t{args.ResourceFileName}");
                                                                                                                       mText.AppendLine($"Full uri after saving:\t{args.ResourceFileUri}\n");
                                                                                                                   }

                                                                                                                   public string GetText()
                                                                                                                   {
                                                                                                                       return mText.ToString();
                                                                                                                   }

                                                                                                                   private readonly StringBuilder mText = new StringBuilder();
                                                                                                               }

Remarks

By default, when Aspose.Words saves a document to fixed page HTML, SVG or Markdown, it saves each resource into a separate file. Aspose.Words uses the document file name and a unique number to generate unique file name for each resource found in the document.

Aspose.Words.Saving.ResourceSavingArgs allows to redefine how resource file names are generated or to completely circumvent saving of resources into files by providing your own stream objects.

To apply your own logic for generating resource file names use the Aspose.Words.Saving.ResourceSavingArgs.ResourceFileName property.

To save resources into streams instead of files, use the Aspose.Words.Saving.ResourceSavingArgs.ResourceStream property.

Properties

Document

Gets the document object that is currently being saved.

public Document Document { get; }

Property Value

Document

Examples

Shows how to use a callback to track external resources created while converting a document to HTML.

public void ResourceSavingCallback()
                                                                                                               {
                                                                                                                   Document doc = new Document(MyDir + "Bullet points with alternative font.docx");

                                                                                                                   FontSavingCallback callback = new FontSavingCallback();

                                                                                                                   HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions
                                                                                                                   {
                                                                                                                       ResourceSavingCallback = callback
                                                                                                                   };

                                                                                                                   doc.Save(ArtifactsDir + "HtmlFixedSaveOptions.UsingMachineFonts.html", saveOptions);

                                                                                                                   Console.WriteLine(callback.GetText());
                                                                                                               }

                                                                                                               private class FontSavingCallback : IResourceSavingCallback
                                                                                                               {
                                                                                                                   /// <summary>
                                                                                                                   /// Called when Aspose.Words saves an external resource to fixed page HTML or SVG.
                                                                                                                   /// </summary>
                                                                                                                   public void ResourceSaving(ResourceSavingArgs args)
                                                                                                                   {
                                                                                                                       mText.AppendLine($"Original document URI:\t{args.Document.OriginalFileName}");
                                                                                                                       mText.AppendLine($"Resource being saved:\t{args.ResourceFileName}");
                                                                                                                       mText.AppendLine($"Full uri after saving:\t{args.ResourceFileUri}\n");
                                                                                                                   }

                                                                                                                   public string GetText()
                                                                                                                   {
                                                                                                                       return mText.ToString();
                                                                                                                   }

                                                                                                                   private readonly StringBuilder mText = new StringBuilder();
                                                                                                               }

KeepResourceStreamOpen

Specifies whether Aspose.Words should keep the stream open or close it after saving a resource.

public bool KeepResourceStreamOpen { get; set; }

Property Value

bool

Examples

Shows how to use a callback to print the URIs of external resources created while converting a document to HTML.

public void HtmlFixedResourceFolder()
                                                                                                                           {
                                                                                                                               Document doc = new Document(MyDir + "Rendering.docx");

                                                                                                                               ResourceUriPrinter callback = new ResourceUriPrinter();

                                                                                                                               HtmlFixedSaveOptions options = new HtmlFixedSaveOptions
                                                                                                                               {
                                                                                                                                   SaveFormat = SaveFormat.HtmlFixed,
                                                                                                                                   ExportEmbeddedImages = false,
                                                                                                                                   ResourcesFolder = ArtifactsDir + "HtmlFixedResourceFolder",
                                                                                                                                   ResourcesFolderAlias = ArtifactsDir + "HtmlFixedResourceFolderAlias",
                                                                                                                                   ShowPageBorder = false,
                                                                                                                                   ResourceSavingCallback = callback
                                                                                                                               };

                                                                                                                               // A folder specified by ResourcesFolderAlias will contain the resources instead of ResourcesFolder.
                                                                                                                               // We must ensure the folder exists before the streams can put their resources into it.
                                                                                                                               Directory.CreateDirectory(options.ResourcesFolderAlias);

                                                                                                                               doc.Save(ArtifactsDir + "HtmlFixedSaveOptions.HtmlFixedResourceFolder.html", options);

                                                                                                                               Console.WriteLine(callback.GetText());

                                                                                                                               string[] resourceFiles = Directory.GetFiles(ArtifactsDir + "HtmlFixedResourceFolderAlias");

                                                                                                                               Assert.That(Directory.Exists(ArtifactsDir + "HtmlFixedResourceFolder"), Is.False);
                                                                                                                               Assert.That(resourceFiles.Count(f => f.EndsWith(".jpeg") || f.EndsWith(".png") || f.EndsWith(".css")), Is.EqualTo(6));
                                                                                                                           }

                                                                                                                           /// <summary>
                                                                                                                           /// Counts and prints URIs of resources contained by as they are converted to fixed HTML.
                                                                                                                           /// </summary>
                                                                                                                           private class ResourceUriPrinter : IResourceSavingCallback
                                                                                                                           {
                                                                                                                               void IResourceSavingCallback.ResourceSaving(ResourceSavingArgs args)
                                                                                                                               {
                                                                                                                                   // If we set a folder alias in the SaveOptions object, we will be able to print it from here.
                                                                                                                                   mText.AppendLine($"Resource #{++mSavedResourceCount} \"{args.ResourceFileName}\"");

                                                                                                                                   string extension = Path.GetExtension(args.ResourceFileName);
                                                                                                                                   switch (extension)
                                                                                                                                   {
                                                                                                                                       case ".ttf":
                                                                                                                                       case ".woff":
                                                                                                                                       {
                                                                                                                                           // By default, 'ResourceFileUri' uses system folder for fonts.
                                                                                                                                           // To avoid problems in other platforms you must explicitly specify the path for the fonts.
                                                                                                                                           args.ResourceFileUri = ArtifactsDir + Path.DirectorySeparatorChar + args.ResourceFileName;
                                                                                                                                           break;
                                                                                                                                       }
                                                                                                                                   }

                                                                                                                                   mText.AppendLine("\t" + args.ResourceFileUri);

                                                                                                                                   // If we have specified a folder in the "ResourcesFolderAlias" property,
                                                                                                                                   // we will also need to redirect each stream to put its resource in that folder.
                                                                                                                                   args.ResourceStream = new FileStream(args.ResourceFileUri, FileMode.Create);
                                                                                                                                   args.KeepResourceStreamOpen = false;
                                                                                                                               }

                                                                                                                               public string GetText()
                                                                                                                               {
                                                                                                                                   return mText.ToString();
                                                                                                                               }

                                                                                                                               private int mSavedResourceCount;
                                                                                                                               private readonly StringBuilder mText = new StringBuilder();
                                                                                                                           }

Remarks

Default is false and Aspose.Words will close the stream you provided in the Aspose.Words.Saving.ResourceSavingArgs.ResourceStream property after writing a resource into it. Specify true to keep the stream open.

Aspose.Words.Saving.ResourceSavingArgs.ResourceStream

ResourceFileName

Gets or sets the file name (without path) where the resource will be saved to.

public string ResourceFileName { get; set; }

Property Value

string

Examples

Shows how to use a callback to track external resources created while converting a document to HTML.

public void ResourceSavingCallback()
                                                                                                               {
                                                                                                                   Document doc = new Document(MyDir + "Bullet points with alternative font.docx");

                                                                                                                   FontSavingCallback callback = new FontSavingCallback();

                                                                                                                   HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions
                                                                                                                   {
                                                                                                                       ResourceSavingCallback = callback
                                                                                                                   };

                                                                                                                   doc.Save(ArtifactsDir + "HtmlFixedSaveOptions.UsingMachineFonts.html", saveOptions);

                                                                                                                   Console.WriteLine(callback.GetText());
                                                                                                               }

                                                                                                               private class FontSavingCallback : IResourceSavingCallback
                                                                                                               {
                                                                                                                   /// <summary>
                                                                                                                   /// Called when Aspose.Words saves an external resource to fixed page HTML or SVG.
                                                                                                                   /// </summary>
                                                                                                                   public void ResourceSaving(ResourceSavingArgs args)
                                                                                                                   {
                                                                                                                       mText.AppendLine($"Original document URI:\t{args.Document.OriginalFileName}");
                                                                                                                       mText.AppendLine($"Resource being saved:\t{args.ResourceFileName}");
                                                                                                                       mText.AppendLine($"Full uri after saving:\t{args.ResourceFileUri}\n");
                                                                                                                   }

                                                                                                                   public string GetText()
                                                                                                                   {
                                                                                                                       return mText.ToString();
                                                                                                                   }

                                                                                                                   private readonly StringBuilder mText = new StringBuilder();
                                                                                                               }

Remarks

This property allows you to redefine how the resource file names are generated during export to fixed page HTML, SVG or Markdown.

When the event is fired, this property contains the file name that was generated by Aspose.Words. You can change the value of this property to save the resource into a different file. Note that file names must be unique.

Aspose.Words automatically generates a unique file name for every resource when exporting to fixed page HTML, SVG or Markdown format. How the resource file name is generated depends on whether you save the document to a file or to a stream.

When saving a document to a file, the generated resource file name looks like <document base file name>.<image number>.<extension>.

When saving a document to a stream, the generated resource file name looks like Aspose.Words.<document guid>.<image number>.<extension>.

Aspose.Words.Saving.ResourceSavingArgs.ResourceFileName must contain only the file name without the path. Aspose.Words determines the path for saving and the value of the src attribute for writing to fixed page HTML, SVG or Markdown using the document file name, the Aspose.Words.Saving.HtmlFixedSaveOptions.ResourcesFolder or Aspose.Words.Saving.SvgSaveOptions.ResourcesFolder and Aspose.Words.Saving.HtmlFixedSaveOptions.ResourcesFolderAlias or Aspose.Words.Saving.SvgSaveOptions.ResourcesFolderAlias or Aspose.Words.Saving.MarkdownSaveOptions.ImagesFolder or Aspose.Words.Saving.MarkdownSaveOptions.ImagesFolderAlias properties.

Aspose.Words.Saving.ResourceSavingArgs.ResourceStream Aspose.Words.Saving.HtmlFixedSaveOptions.ResourcesFolder Aspose.Words.Saving.SvgSaveOptions.ResourcesFolder Aspose.Words.Saving.HtmlFixedSaveOptions.ResourcesFolderAlias Aspose.Words.Saving.SvgSaveOptions.ResourcesFolderAlias

ResourceFileUri

Gets or sets the uniform resource identifier (URI) used to reference the resource file from the document.

public string ResourceFileUri { get; set; }

Property Value

string

Examples

Shows how to use a callback to track external resources created while converting a document to HTML.

public void ResourceSavingCallback()
                                                                                                               {
                                                                                                                   Document doc = new Document(MyDir + "Bullet points with alternative font.docx");

                                                                                                                   FontSavingCallback callback = new FontSavingCallback();

                                                                                                                   HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions
                                                                                                                   {
                                                                                                                       ResourceSavingCallback = callback
                                                                                                                   };

                                                                                                                   doc.Save(ArtifactsDir + "HtmlFixedSaveOptions.UsingMachineFonts.html", saveOptions);

                                                                                                                   Console.WriteLine(callback.GetText());
                                                                                                               }

                                                                                                               private class FontSavingCallback : IResourceSavingCallback
                                                                                                               {
                                                                                                                   /// <summary>
                                                                                                                   /// Called when Aspose.Words saves an external resource to fixed page HTML or SVG.
                                                                                                                   /// </summary>
                                                                                                                   public void ResourceSaving(ResourceSavingArgs args)
                                                                                                                   {
                                                                                                                       mText.AppendLine($"Original document URI:\t{args.Document.OriginalFileName}");
                                                                                                                       mText.AppendLine($"Resource being saved:\t{args.ResourceFileName}");
                                                                                                                       mText.AppendLine($"Full uri after saving:\t{args.ResourceFileUri}\n");
                                                                                                                   }

                                                                                                                   public string GetText()
                                                                                                                   {
                                                                                                                       return mText.ToString();
                                                                                                                   }

                                                                                                                   private readonly StringBuilder mText = new StringBuilder();
                                                                                                               }

Remarks

This property allows you to change URIs of resource files exported to fixed page HTML, SVG or Markdown documents.

Aspose.Words automatically generates an URI for every resource file during export to fixed page HTML, SVG or Markdown format. The generated URIs reference resource files saved by Aspose.Words. However, the URIs can be incorrect if resource files are to be moved to other location or if resource files are saved to streams. This property allows to correct URIs in these cases.

When the event is fired, this property contains the URI that was generated by Aspose.Words. You can change the value of this property to provide a custom URI for the resource file.

Aspose.Words.Saving.HtmlFixedSaveOptions.ResourcesFolder Aspose.Words.Saving.SvgSaveOptions.ResourcesFolder Aspose.Words.Saving.MarkdownSaveOptions.ImagesFolder Aspose.Words.Saving.HtmlFixedSaveOptions.ResourcesFolderAlias Aspose.Words.Saving.SvgSaveOptions.ResourcesFolderAlias Aspose.Words.Saving.MarkdownSaveOptions.ImagesFolderAlias

ResourceStream

Allows to specify the stream where the resource will be saved to.

public Stream ResourceStream { get; set; }

Property Value

Stream

Examples

Shows how to use a callback to print the URIs of external resources created while converting a document to HTML.

public void HtmlFixedResourceFolder()
                                                                                                                           {
                                                                                                                               Document doc = new Document(MyDir + "Rendering.docx");

                                                                                                                               ResourceUriPrinter callback = new ResourceUriPrinter();

                                                                                                                               HtmlFixedSaveOptions options = new HtmlFixedSaveOptions
                                                                                                                               {
                                                                                                                                   SaveFormat = SaveFormat.HtmlFixed,
                                                                                                                                   ExportEmbeddedImages = false,
                                                                                                                                   ResourcesFolder = ArtifactsDir + "HtmlFixedResourceFolder",
                                                                                                                                   ResourcesFolderAlias = ArtifactsDir + "HtmlFixedResourceFolderAlias",
                                                                                                                                   ShowPageBorder = false,
                                                                                                                                   ResourceSavingCallback = callback
                                                                                                                               };

                                                                                                                               // A folder specified by ResourcesFolderAlias will contain the resources instead of ResourcesFolder.
                                                                                                                               // We must ensure the folder exists before the streams can put their resources into it.
                                                                                                                               Directory.CreateDirectory(options.ResourcesFolderAlias);

                                                                                                                               doc.Save(ArtifactsDir + "HtmlFixedSaveOptions.HtmlFixedResourceFolder.html", options);

                                                                                                                               Console.WriteLine(callback.GetText());

                                                                                                                               string[] resourceFiles = Directory.GetFiles(ArtifactsDir + "HtmlFixedResourceFolderAlias");

                                                                                                                               Assert.That(Directory.Exists(ArtifactsDir + "HtmlFixedResourceFolder"), Is.False);
                                                                                                                               Assert.That(resourceFiles.Count(f => f.EndsWith(".jpeg") || f.EndsWith(".png") || f.EndsWith(".css")), Is.EqualTo(6));
                                                                                                                           }

                                                                                                                           /// <summary>
                                                                                                                           /// Counts and prints URIs of resources contained by as they are converted to fixed HTML.
                                                                                                                           /// </summary>
                                                                                                                           private class ResourceUriPrinter : IResourceSavingCallback
                                                                                                                           {
                                                                                                                               void IResourceSavingCallback.ResourceSaving(ResourceSavingArgs args)
                                                                                                                               {
                                                                                                                                   // If we set a folder alias in the SaveOptions object, we will be able to print it from here.
                                                                                                                                   mText.AppendLine($"Resource #{++mSavedResourceCount} \"{args.ResourceFileName}\"");

                                                                                                                                   string extension = Path.GetExtension(args.ResourceFileName);
                                                                                                                                   switch (extension)
                                                                                                                                   {
                                                                                                                                       case ".ttf":
                                                                                                                                       case ".woff":
                                                                                                                                       {
                                                                                                                                           // By default, 'ResourceFileUri' uses system folder for fonts.
                                                                                                                                           // To avoid problems in other platforms you must explicitly specify the path for the fonts.
                                                                                                                                           args.ResourceFileUri = ArtifactsDir + Path.DirectorySeparatorChar + args.ResourceFileName;
                                                                                                                                           break;
                                                                                                                                       }
                                                                                                                                   }

                                                                                                                                   mText.AppendLine("\t" + args.ResourceFileUri);

                                                                                                                                   // If we have specified a folder in the "ResourcesFolderAlias" property,
                                                                                                                                   // we will also need to redirect each stream to put its resource in that folder.
                                                                                                                                   args.ResourceStream = new FileStream(args.ResourceFileUri, FileMode.Create);
                                                                                                                                   args.KeepResourceStreamOpen = false;
                                                                                                                               }

                                                                                                                               public string GetText()
                                                                                                                               {
                                                                                                                                   return mText.ToString();
                                                                                                                               }

                                                                                                                               private int mSavedResourceCount;
                                                                                                                               private readonly StringBuilder mText = new StringBuilder();
                                                                                                                           }

Remarks

This property allows you to save resources to streams instead of files.

The default value is null. When this property is null, the resource will be saved to a file specified in the Aspose.Words.Saving.ResourceSavingArgs.ResourceFileName property.

Using Aspose.Words.Saving.IResourceSavingCallback you cannot substitute one resource with another. It is intended only for control over location where to save resources.

Aspose.Words.Saving.ResourceSavingArgs.ResourceFileName Aspose.Words.Saving.ResourceSavingArgs.KeepResourceStreamOpen
 English