Class ImageSavingArgs
Namespace: Aspose.Words.Saving
Assembly: Aspose.Words.dll (25.12.0)
Provides data for the Aspose.Words.Saving.IImageSavingCallback.ImageSaving(Aspose.Words.Saving.ImageSavingArgs) event.
To learn more, visit the Save a Document documentation article.
public class ImageSavingArgsInheritance
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 split a document into parts and save them.
public void DocumentPartsFileNames()
{
Document doc = new Document(MyDir + "Rendering.docx");
string outFileName = "SavingCallback.DocumentPartsFileNames.html";
// 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();
// If we save the document normally, there will be one output HTML
// document with all the source document's contents.
// Set the "DocumentSplitCriteria" property to "DocumentSplitCriteria.SectionBreak" to
// save our document to multiple HTML files: one for each section.
options.DocumentSplitCriteria = DocumentSplitCriteria.SectionBreak;
// Assign a custom callback to the "DocumentPartSavingCallback" property to alter the document part saving logic.
options.DocumentPartSavingCallback = new SavedDocumentPartRename(outFileName, options.DocumentSplitCriteria);
// If we convert a document that contains images into html, we will end up with one html file which links to several images.
// Each image will be in the form of a file in the local file system.
// There is also a callback that can customize the name and file system location of each image.
options.ImageSavingCallback = new SavedImageRename(outFileName);
doc.Save(ArtifactsDir + outFileName, options);
}
/// <summary>
/// Sets custom filenames for output documents that the saving operation splits a document into.
/// </summary>
private class SavedDocumentPartRename : IDocumentPartSavingCallback
{
public SavedDocumentPartRename(string outFileName, DocumentSplitCriteria documentSplitCriteria)
{
mOutFileName = outFileName;
mDocumentSplitCriteria = documentSplitCriteria;
}
void IDocumentPartSavingCallback.DocumentPartSaving(DocumentPartSavingArgs args)
{
// We can access the entire source document via the "Document" property.
Assert.That(args.Document.OriginalFileName.EndsWith("Rendering.docx"), Is.True);
string partType = string.Empty;
switch (mDocumentSplitCriteria)
{
case DocumentSplitCriteria.PageBreak:
partType = "Page";
break;
case DocumentSplitCriteria.ColumnBreak:
partType = "Column";
break;
case DocumentSplitCriteria.SectionBreak:
partType = "Section";
break;
case DocumentSplitCriteria.HeadingParagraph:
partType = "Paragraph from heading";
break;
}
string partFileName = $"{mOutFileName} part {++mCount}, of type {partType}{Path.GetExtension(args.DocumentPartFileName)}";
// Below are two ways of specifying where Aspose.Words will save each part of the document.
// 1 - Set a filename for the output part file:
args.DocumentPartFileName = partFileName;
// 2 - Create a custom stream for the output part file:
args.DocumentPartStream = new FileStream(ArtifactsDir + partFileName, FileMode.Create);
Assert.That(args.DocumentPartStream.CanWrite, Is.True);
Assert.That(args.KeepDocumentPartStreamOpen, Is.False);
}
private int mCount;
private readonly string mOutFileName;
private readonly DocumentSplitCriteria mDocumentSplitCriteria;
}
/// <summary>
/// Sets custom filenames for image files that an HTML conversion creates.
/// </summary>
public class SavedImageRename : IImageSavingCallback
{
public SavedImageRename(string outFileName)
{
mOutFileName = outFileName;
}
void IImageSavingCallback.ImageSaving(ImageSavingArgs args)
{
string imageFileName = $"{mOutFileName} shape {++mCount}, of type {args.CurrentShape.ShapeType}{Path.GetExtension(args.ImageFileName)}";
// Below are two ways of specifying where Aspose.Words will save each part of the document.
// 1 - Set a filename for the output image file:
args.ImageFileName = imageFileName;
// 2 - Create a custom stream for the output image file:
args.ImageStream = new FileStream(ArtifactsDir + imageFileName, FileMode.Create);
Assert.That(args.ImageStream.CanWrite, Is.True);
Assert.That(args.IsImageAvailable, Is.True);
Assert.That(args.KeepImageStreamOpen, Is.False);
}
private int mCount;
private readonly string mOutFileName;
}Remarks
By default, when Aspose.Words saves a document to HTML, it saves each image into a separate file. Aspose.Words uses the document file name and a unique number to generate unique file name for each image found in the document.
Aspose.Words.Saving.ImageSavingArgs allows to redefine how image file names are generated or to completely circumvent saving of images into files by providing your own stream objects.
To apply your own logic for generating image file names use the Aspose.Words.Saving.ImageSavingArgs.ImageFileName, Aspose.Words.Saving.ImageSavingArgs.CurrentShape and Aspose.Words.Saving.ImageSavingArgs.IsImageAvailable properties.
To save images into streams instead of files, use the Aspose.Words.Saving.ImageSavingArgs.ImageStream property.
Properties
CurrentShape
Gets the Aspose.Words.Drawing.ShapeBase object corresponding to the shape or group shape that is about to be saved.
public ShapeBase CurrentShape { get; }Property Value
Examples
Shows how to involve an image saving callback in an HTML conversion process.
public void ImageSavingCallback()
{
Document doc = new Document(MyDir + "Rendering.docx");
// When we save the document to HTML, we can pass a SaveOptions object to designate a callback
// to customize the image saving process.
HtmlSaveOptions options = new HtmlSaveOptions();
options.ImageSavingCallback = new ImageShapePrinter();
doc.Save(ArtifactsDir + "HtmlSaveOptions.ImageSavingCallback.html", options);
}
/// <summary>
/// Prints the properties of each image as the saving process saves it to an image file in the local file system
/// during the exporting of a document to HTML.
/// </summary>
private class ImageShapePrinter : IImageSavingCallback
{
void IImageSavingCallback.ImageSaving(ImageSavingArgs args)
{
args.KeepImageStreamOpen = false;
Assert.That(args.IsImageAvailable, Is.True);
Console.WriteLine($"{args.Document.OriginalFileName.Split('\\').Last()} Image #{++mImageCount}");
LayoutCollector layoutCollector = new LayoutCollector(args.Document);
Console.WriteLine($"\tOn page:\t{layoutCollector.GetStartPageIndex(args.CurrentShape)}");
Console.WriteLine($"\tDimensions:\t{args.CurrentShape.Bounds}");
Console.WriteLine($"\tAlignment:\t{args.CurrentShape.VerticalAlignment}");
Console.WriteLine($"\tWrap type:\t{args.CurrentShape.WrapType}");
Console.WriteLine($"Output filename:\t{args.ImageFileName}\n");
}
private int mImageCount;
}Remarks
Aspose.Words.Saving.IImageSavingCallback can be fired while saving either a shape or a group shape. That's why the property has Aspose.Words.Drawing.ShapeBase type. You can check whether it's a group shape comparing Aspose.Words.Drawing.ShapeBase.ShapeType with Aspose.Words.Drawing.ShapeType.Group or by casting it to one of derived classes: Aspose.Words.Drawing.Shape or Aspose.Words.Drawing.GroupShape.
Aspose.Words uses the document file name and a unique number to generate unique file name for each image found in the document. You can use the Aspose.Words.Saving.ImageSavingArgs.CurrentShape property to generate a "better" file name by examining shape properties such as Aspose.Words.Drawing.ImageData.Title (Shape only), Aspose.Words.Drawing.ImageData.SourceFullName (Shape only) and Aspose.Words.Drawing.ShapeBase.Name. Of course you can build file names using any other properties or criteria but note that subsidiary file names must be unique within the export operation.
Some images in the document can be unavailable. To check image availability use the Aspose.Words.Saving.ImageSavingArgs.IsImageAvailable property.
Document
Gets the document object that is currently being saved.
public Document Document { get; }Property Value
Examples
Shows how to involve an image saving callback in an HTML conversion process.
public void ImageSavingCallback()
{
Document doc = new Document(MyDir + "Rendering.docx");
// When we save the document to HTML, we can pass a SaveOptions object to designate a callback
// to customize the image saving process.
HtmlSaveOptions options = new HtmlSaveOptions();
options.ImageSavingCallback = new ImageShapePrinter();
doc.Save(ArtifactsDir + "HtmlSaveOptions.ImageSavingCallback.html", options);
}
/// <summary>
/// Prints the properties of each image as the saving process saves it to an image file in the local file system
/// during the exporting of a document to HTML.
/// </summary>
private class ImageShapePrinter : IImageSavingCallback
{
void IImageSavingCallback.ImageSaving(ImageSavingArgs args)
{
args.KeepImageStreamOpen = false;
Assert.That(args.IsImageAvailable, Is.True);
Console.WriteLine($"{args.Document.OriginalFileName.Split('\\').Last()} Image #{++mImageCount}");
LayoutCollector layoutCollector = new LayoutCollector(args.Document);
Console.WriteLine($"\tOn page:\t{layoutCollector.GetStartPageIndex(args.CurrentShape)}");
Console.WriteLine($"\tDimensions:\t{args.CurrentShape.Bounds}");
Console.WriteLine($"\tAlignment:\t{args.CurrentShape.VerticalAlignment}");
Console.WriteLine($"\tWrap type:\t{args.CurrentShape.WrapType}");
Console.WriteLine($"Output filename:\t{args.ImageFileName}\n");
}
private int mImageCount;
}ImageFileName
Gets or sets the file name (without path) where the image will be saved to.
public string ImageFileName { get; set; }Property Value
Examples
Shows how to split a document into parts and save them.
public void DocumentPartsFileNames()
{
Document doc = new Document(MyDir + "Rendering.docx");
string outFileName = "SavingCallback.DocumentPartsFileNames.html";
// 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();
// If we save the document normally, there will be one output HTML
// document with all the source document's contents.
// Set the "DocumentSplitCriteria" property to "DocumentSplitCriteria.SectionBreak" to
// save our document to multiple HTML files: one for each section.
options.DocumentSplitCriteria = DocumentSplitCriteria.SectionBreak;
// Assign a custom callback to the "DocumentPartSavingCallback" property to alter the document part saving logic.
options.DocumentPartSavingCallback = new SavedDocumentPartRename(outFileName, options.DocumentSplitCriteria);
// If we convert a document that contains images into html, we will end up with one html file which links to several images.
// Each image will be in the form of a file in the local file system.
// There is also a callback that can customize the name and file system location of each image.
options.ImageSavingCallback = new SavedImageRename(outFileName);
doc.Save(ArtifactsDir + outFileName, options);
}
/// <summary>
/// Sets custom filenames for output documents that the saving operation splits a document into.
/// </summary>
private class SavedDocumentPartRename : IDocumentPartSavingCallback
{
public SavedDocumentPartRename(string outFileName, DocumentSplitCriteria documentSplitCriteria)
{
mOutFileName = outFileName;
mDocumentSplitCriteria = documentSplitCriteria;
}
void IDocumentPartSavingCallback.DocumentPartSaving(DocumentPartSavingArgs args)
{
// We can access the entire source document via the "Document" property.
Assert.That(args.Document.OriginalFileName.EndsWith("Rendering.docx"), Is.True);
string partType = string.Empty;
switch (mDocumentSplitCriteria)
{
case DocumentSplitCriteria.PageBreak:
partType = "Page";
break;
case DocumentSplitCriteria.ColumnBreak:
partType = "Column";
break;
case DocumentSplitCriteria.SectionBreak:
partType = "Section";
break;
case DocumentSplitCriteria.HeadingParagraph:
partType = "Paragraph from heading";
break;
}
string partFileName = $"{mOutFileName} part {++mCount}, of type {partType}{Path.GetExtension(args.DocumentPartFileName)}";
// Below are two ways of specifying where Aspose.Words will save each part of the document.
// 1 - Set a filename for the output part file:
args.DocumentPartFileName = partFileName;
// 2 - Create a custom stream for the output part file:
args.DocumentPartStream = new FileStream(ArtifactsDir + partFileName, FileMode.Create);
Assert.That(args.DocumentPartStream.CanWrite, Is.True);
Assert.That(args.KeepDocumentPartStreamOpen, Is.False);
}
private int mCount;
private readonly string mOutFileName;
private readonly DocumentSplitCriteria mDocumentSplitCriteria;
}
/// <summary>
/// Sets custom filenames for image files that an HTML conversion creates.
/// </summary>
public class SavedImageRename : IImageSavingCallback
{
public SavedImageRename(string outFileName)
{
mOutFileName = outFileName;
}
void IImageSavingCallback.ImageSaving(ImageSavingArgs args)
{
string imageFileName = $"{mOutFileName} shape {++mCount}, of type {args.CurrentShape.ShapeType}{Path.GetExtension(args.ImageFileName)}";
// Below are two ways of specifying where Aspose.Words will save each part of the document.
// 1 - Set a filename for the output image file:
args.ImageFileName = imageFileName;
// 2 - Create a custom stream for the output image file:
args.ImageStream = new FileStream(ArtifactsDir + imageFileName, FileMode.Create);
Assert.That(args.ImageStream.CanWrite, Is.True);
Assert.That(args.IsImageAvailable, Is.True);
Assert.That(args.KeepImageStreamOpen, Is.False);
}
private int mCount;
private readonly string mOutFileName;
}Remarks
This property allows you to redefine how the image file names are generated during export to HTML.
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 image into a different file. Note that file names must be unique.
Aspose.Words automatically generates a unique file name for every embedded image when exporting to HTML format. How the image 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 image file name looks like <document base file name>.<image number>.<extension>.
When saving a document to a stream, the generated image file name looks like Aspose.Words.<document guid>.<image number>.<extension>.
Aspose.Words.Saving.ImageSavingArgs.ImageFileName 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 HTML using the document file name, the Aspose.Words.Saving.HtmlSaveOptions.ImagesFolder and
Aspose.Words.Saving.HtmlSaveOptions.ImagesFolderAlias properties.
ImageStream
Allows to specify the stream where the image will be saved to.
public Stream ImageStream { get; set; }Property Value
Examples
Shows how to involve an image saving callback in an HTML conversion process.
public void ImageSavingCallback()
{
Document doc = new Document(MyDir + "Rendering.docx");
// When we save the document to HTML, we can pass a SaveOptions object to designate a callback
// to customize the image saving process.
HtmlSaveOptions options = new HtmlSaveOptions();
options.ImageSavingCallback = new ImageShapePrinter();
doc.Save(ArtifactsDir + "HtmlSaveOptions.ImageSavingCallback.html", options);
}
/// <summary>
/// Prints the properties of each image as the saving process saves it to an image file in the local file system
/// during the exporting of a document to HTML.
/// </summary>
private class ImageShapePrinter : IImageSavingCallback
{
void IImageSavingCallback.ImageSaving(ImageSavingArgs args)
{
args.KeepImageStreamOpen = false;
Assert.That(args.IsImageAvailable, Is.True);
Console.WriteLine($"{args.Document.OriginalFileName.Split('\\').Last()} Image #{++mImageCount}");
LayoutCollector layoutCollector = new LayoutCollector(args.Document);
Console.WriteLine($"\tOn page:\t{layoutCollector.GetStartPageIndex(args.CurrentShape)}");
Console.WriteLine($"\tDimensions:\t{args.CurrentShape.Bounds}");
Console.WriteLine($"\tAlignment:\t{args.CurrentShape.VerticalAlignment}");
Console.WriteLine($"\tWrap type:\t{args.CurrentShape.WrapType}");
Console.WriteLine($"Output filename:\t{args.ImageFileName}\n");
}
private int mImageCount;
}Remarks
This property allows you to save images to streams instead of files during HTML.
The default value is null. When this property is null, the image
will be saved to a file specified in the Aspose.Words.Saving.ImageSavingArgs.ImageFileName property.
Using Aspose.Words.Saving.IImageSavingCallback you cannot substitute one image with another. It is intended only for control over location where to save images.
Aspose.Words.Saving.ImageSavingArgs.ImageFileName Aspose.Words.Saving.ImageSavingArgs.KeepImageStreamOpenIsImageAvailable
Returns true if the current image is available for export.
public bool IsImageAvailable { get; }Property Value
Examples
Shows how to involve an image saving callback in an HTML conversion process.
public void ImageSavingCallback()
{
Document doc = new Document(MyDir + "Rendering.docx");
// When we save the document to HTML, we can pass a SaveOptions object to designate a callback
// to customize the image saving process.
HtmlSaveOptions options = new HtmlSaveOptions();
options.ImageSavingCallback = new ImageShapePrinter();
doc.Save(ArtifactsDir + "HtmlSaveOptions.ImageSavingCallback.html", options);
}
/// <summary>
/// Prints the properties of each image as the saving process saves it to an image file in the local file system
/// during the exporting of a document to HTML.
/// </summary>
private class ImageShapePrinter : IImageSavingCallback
{
void IImageSavingCallback.ImageSaving(ImageSavingArgs args)
{
args.KeepImageStreamOpen = false;
Assert.That(args.IsImageAvailable, Is.True);
Console.WriteLine($"{args.Document.OriginalFileName.Split('\\').Last()} Image #{++mImageCount}");
LayoutCollector layoutCollector = new LayoutCollector(args.Document);
Console.WriteLine($"\tOn page:\t{layoutCollector.GetStartPageIndex(args.CurrentShape)}");
Console.WriteLine($"\tDimensions:\t{args.CurrentShape.Bounds}");
Console.WriteLine($"\tAlignment:\t{args.CurrentShape.VerticalAlignment}");
Console.WriteLine($"\tWrap type:\t{args.CurrentShape.WrapType}");
Console.WriteLine($"Output filename:\t{args.ImageFileName}\n");
}
private int mImageCount;
}Remarks
Some images in the document can be unavailable, for example, because the image
is linked and the link is inaccessible or does not point to a valid image.
In this case Aspose.Words exports an icon with a red cross. This property returns
true if the original image is available; returns false if the original
image is not available and a "no image" icon will be offered for save.
When saving a group shape or a shape that doesn't require any image this property
is always true.
See Also
ImageSavingArgs . CurrentShape
KeepImageStreamOpen
Specifies whether Aspose.Words should keep the stream open or close it after saving an image.
public bool KeepImageStreamOpen { get; set; }Property Value
Examples
Shows how to involve an image saving callback in an HTML conversion process.
public void ImageSavingCallback()
{
Document doc = new Document(MyDir + "Rendering.docx");
// When we save the document to HTML, we can pass a SaveOptions object to designate a callback
// to customize the image saving process.
HtmlSaveOptions options = new HtmlSaveOptions();
options.ImageSavingCallback = new ImageShapePrinter();
doc.Save(ArtifactsDir + "HtmlSaveOptions.ImageSavingCallback.html", options);
}
/// <summary>
/// Prints the properties of each image as the saving process saves it to an image file in the local file system
/// during the exporting of a document to HTML.
/// </summary>
private class ImageShapePrinter : IImageSavingCallback
{
void IImageSavingCallback.ImageSaving(ImageSavingArgs args)
{
args.KeepImageStreamOpen = false;
Assert.That(args.IsImageAvailable, Is.True);
Console.WriteLine($"{args.Document.OriginalFileName.Split('\\').Last()} Image #{++mImageCount}");
LayoutCollector layoutCollector = new LayoutCollector(args.Document);
Console.WriteLine($"\tOn page:\t{layoutCollector.GetStartPageIndex(args.CurrentShape)}");
Console.WriteLine($"\tDimensions:\t{args.CurrentShape.Bounds}");
Console.WriteLine($"\tAlignment:\t{args.CurrentShape.VerticalAlignment}");
Console.WriteLine($"\tWrap type:\t{args.CurrentShape.WrapType}");
Console.WriteLine($"Output filename:\t{args.ImageFileName}\n");
}
private int mImageCount;
}Remarks
Default is false and Aspose.Words will close the stream you provided
in the Aspose.Words.Saving.ImageSavingArgs.ImageStream property after writing an image into it.
Specify true to keep the stream open.