search/mag_sel search/close
Aspose::Words::Saving::ImageSavingArgs Class Reference

Provides data for the ImageSaving() event.

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.

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 ImageFileName, CurrentShape and IsImageAvailable properties.

To save images into streams instead of files, use the ImageStream property.

Examples

Shows how to split a document into parts and save them.

void DocumentPartsFileNames()
{
auto doc = MakeObject<Document>(MyDir + u"Rendering.docx");
String outFileName = u"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.
auto options = MakeObject<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->set_DocumentSplitCriteria(DocumentSplitCriteria::SectionBreak);
// Assign a custom callback to the "DocumentPartSavingCallback" property to alter the document part saving logic.
options->set_DocumentPartSavingCallback(MakeObject<ExSavingCallback::SavedDocumentPartRename>(outFileName, options->get_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->set_ImageSavingCallback(MakeObject<ExSavingCallback::SavedImageRename>(outFileName));
doc->Save(ArtifactsDir + outFileName, options);
}
class SavedDocumentPartRename : public IDocumentPartSavingCallback
{
public:
SavedDocumentPartRename(String outFileName, DocumentSplitCriteria documentSplitCriteria)
: mCount(0), mDocumentSplitCriteria(((Aspose::Words::Saving::DocumentSplitCriteria)0))
{
mOutFileName = outFileName;
mDocumentSplitCriteria = documentSplitCriteria;
}
void DocumentPartSaving(SharedPtr<DocumentPartSavingArgs> args) override
{
// We can access the entire source document via the "Document" property.
ASSERT_TRUE(args->get_Document()->get_OriginalFileName().EndsWith(u"Rendering.docx"));
String partType = String::Empty;
switch (mDocumentSplitCriteria)
{
partType = u"Page";
break;
partType = u"Column";
break;
partType = u"Section";
break;
partType = u"Paragraph from heading";
break;
default:
break;
}
String partFileName = String::Format(u"{0} part {1}, of type {2}{3}", mOutFileName, ++mCount, partType,
System::IO::Path::GetExtension(args->get_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->set_DocumentPartFileName(partFileName);
// 2 - Create a custom stream for the output part file:
args->set_DocumentPartStream(MakeObject<System::IO::FileStream>(ArtifactsDir + partFileName, System::IO::FileMode::Create));
ASSERT_TRUE(args->get_DocumentPartStream()->get_CanWrite());
ASSERT_FALSE(args->get_KeepDocumentPartStreamOpen());
}
private:
int mCount;
String mOutFileName;
DocumentSplitCriteria mDocumentSplitCriteria;
};
class SavedImageRename : public IImageSavingCallback
{
public:
SavedImageRename(String outFileName) : mCount(0)
{
mOutFileName = outFileName;
}
void ImageSaving(SharedPtr<ImageSavingArgs> args) override
{
String imageFileName = String::Format(u"{0} shape {1}, of type {2}{3}", mOutFileName, ++mCount, args->get_CurrentShape()->get_ShapeType(),
System::IO::Path::GetExtension(args->get_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->set_ImageFileName(imageFileName);
// 2 - Create a custom stream for the output image file:
args->set_ImageStream(MakeObject<System::IO::FileStream>(ArtifactsDir + imageFileName, System::IO::FileMode::Create));
ASSERT_TRUE(args->get_ImageStream()->get_CanWrite());
ASSERT_TRUE(args->get_IsImageAvailable());
ASSERT_FALSE(args->get_KeepImageStreamOpen());
}
private:
int mCount;
String mOutFileName;
};

#include <Aspose.Words.Cpp/Saving/ImageSavingArgs.h>

+ Inheritance diagram for Aspose::Words::Saving::ImageSavingArgs:

Public Member Functions

SharedPtr< ShapeBaseget_CurrentShape () const
 Gets the ShapeBase object corresponding to the shape or group shape that is about to be saved. More...
 
SharedPtr< Documentget_Document ()
 Gets the document object that is currently being saved. More...
 
String get_ImageFileName () const
 Gets or sets the file name (without path) where the image will be saved to. More...
 
SharedPtr< Streamget_ImageStream () const
 Allows to specify the stream where the image will be saved to. More...
 
bool get_IsImageAvailable () const
 Returns true if the current image is available for export. More...
 
bool get_KeepImageStreamOpen () const
 Specifies whether Aspose.Words should keep the stream open or close it after saving an image. More...
 
virtual const TypeInfoGetType () const override
 
virtual bool Is (const TypeInfo &target) const override
 
void set_ImageFileName (String value)
 Setter for get_ImageFileName. More...
 
void set_ImageStream (SharedPtr< Stream > value)
 Setter for get_ImageStream. More...
 
void set_KeepImageStreamOpen (bool value)
 Setter for get_KeepImageStreamOpen. More...
 

Static Public Member Functions

static const TypeInfoType ()
 

Member Function Documentation

◆ get_CurrentShape()

System::SharedPtr<Aspose::Words::Drawing::ShapeBase> Aspose::Words::Saving::ImageSavingArgs::get_CurrentShape ( ) const

Gets the ShapeBase object corresponding to the shape or group shape that is about to be saved.

IImageSavingCallback can be fired while saving either a shape or a group shape. That's why the property has ShapeBase type. You can check whether it's a group shape comparing ShapeType with Group or by casting it to one of derived classes: Shape or 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 CurrentShape property to generate a "better" file name by examining shape properties such as Title (Shape only), SourceFullName (Shape only) and 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 IsImageAvailable property.

Examples

Shows how to involve an image saving callback in an HTML conversion process.

void ImageSavingCallback()
{
auto doc = MakeObject<Document>(MyDir + u"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.
auto options = MakeObject<HtmlSaveOptions>();
options->set_ImageSavingCallback(MakeObject<ExHtmlSaveOptions::ImageShapePrinter>());
doc->Save(ArtifactsDir + u"HtmlSaveOptions.ImageSavingCallback.html", options);
}
class ImageShapePrinter : public IImageSavingCallback
{
public:
void ImageSaving(SharedPtr<ImageSavingArgs> args) override
{
args->set_KeepImageStreamOpen(false);
ASSERT_TRUE(args->get_IsImageAvailable());
std::cout << args->get_Document()->get_OriginalFileName().Split(MakeArray<char16_t>({u'\\'}))->LINQ_Last() << " Image #" << ++mImageCount
<< std::endl;
auto layoutCollector = MakeObject<LayoutCollector>(args->get_Document());
std::cout << "\tOn page:\t" << layoutCollector->GetStartPageIndex(args->get_CurrentShape()) << std::endl;
std::cout << "\tDimensions:\t" << args->get_CurrentShape()->get_Bounds() << std::endl;
std::cout << String::Format(u"\tAlignment:\t{0}", args->get_CurrentShape()->get_VerticalAlignment()) << std::endl;
std::cout << String::Format(u"\tWrap type:\t{0}", args->get_CurrentShape()->get_WrapType()) << std::endl;
std::cout << "Output filename:\t" << args->get_ImageFileName() << "\n" << std::endl;
}
ImageShapePrinter() : mImageCount(0)
{
}
private:
int mImageCount;
};

◆ get_Document()

System::SharedPtr<Aspose::Words::Document> Aspose::Words::Saving::ImageSavingArgs::get_Document ( )

Gets the document object that is currently being saved.

Examples

Shows how to involve an image saving callback in an HTML conversion process.

void ImageSavingCallback()
{
auto doc = MakeObject<Document>(MyDir + u"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.
auto options = MakeObject<HtmlSaveOptions>();
options->set_ImageSavingCallback(MakeObject<ExHtmlSaveOptions::ImageShapePrinter>());
doc->Save(ArtifactsDir + u"HtmlSaveOptions.ImageSavingCallback.html", options);
}
class ImageShapePrinter : public IImageSavingCallback
{
public:
void ImageSaving(SharedPtr<ImageSavingArgs> args) override
{
args->set_KeepImageStreamOpen(false);
ASSERT_TRUE(args->get_IsImageAvailable());
std::cout << args->get_Document()->get_OriginalFileName().Split(MakeArray<char16_t>({u'\\'}))->LINQ_Last() << " Image #" << ++mImageCount
<< std::endl;
auto layoutCollector = MakeObject<LayoutCollector>(args->get_Document());
std::cout << "\tOn page:\t" << layoutCollector->GetStartPageIndex(args->get_CurrentShape()) << std::endl;
std::cout << "\tDimensions:\t" << args->get_CurrentShape()->get_Bounds() << std::endl;
std::cout << String::Format(u"\tAlignment:\t{0}", args->get_CurrentShape()->get_VerticalAlignment()) << std::endl;
std::cout << String::Format(u"\tWrap type:\t{0}", args->get_CurrentShape()->get_WrapType()) << std::endl;
std::cout << "Output filename:\t" << args->get_ImageFileName() << "\n" << std::endl;
}
ImageShapePrinter() : mImageCount(0)
{
}
private:
int mImageCount;
};

◆ get_ImageFileName()

System::String Aspose::Words::Saving::ImageSavingArgs::get_ImageFileName ( ) const

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

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

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 ImagesFolder and ImagesFolderAlias properties.

See also
Aspose::Words::Saving::ImageSavingArgs::get_CurrentShape
Aspose::Words::Saving::ImageSavingArgs::get_IsImageAvailable
Aspose::Words::Saving::ImageSavingArgs::get_ImageStream
Aspose::Words::Saving::HtmlSaveOptions::get_ImagesFolder
Aspose::Words::Saving::HtmlSaveOptions::get_ImagesFolderAlias
Examples

Shows how to split a document into parts and save them.

void DocumentPartsFileNames()
{
auto doc = MakeObject<Document>(MyDir + u"Rendering.docx");
String outFileName = u"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.
auto options = MakeObject<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->set_DocumentSplitCriteria(DocumentSplitCriteria::SectionBreak);
// Assign a custom callback to the "DocumentPartSavingCallback" property to alter the document part saving logic.
options->set_DocumentPartSavingCallback(MakeObject<ExSavingCallback::SavedDocumentPartRename>(outFileName, options->get_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->set_ImageSavingCallback(MakeObject<ExSavingCallback::SavedImageRename>(outFileName));
doc->Save(ArtifactsDir + outFileName, options);
}
class SavedDocumentPartRename : public IDocumentPartSavingCallback
{
public:
SavedDocumentPartRename(String outFileName, DocumentSplitCriteria documentSplitCriteria)
: mCount(0), mDocumentSplitCriteria(((Aspose::Words::Saving::DocumentSplitCriteria)0))
{
mOutFileName = outFileName;
mDocumentSplitCriteria = documentSplitCriteria;
}
void DocumentPartSaving(SharedPtr<DocumentPartSavingArgs> args) override
{
// We can access the entire source document via the "Document" property.
ASSERT_TRUE(args->get_Document()->get_OriginalFileName().EndsWith(u"Rendering.docx"));
String partType = String::Empty;
switch (mDocumentSplitCriteria)
{
partType = u"Page";
break;
partType = u"Column";
break;
partType = u"Section";
break;
partType = u"Paragraph from heading";
break;
default:
break;
}
String partFileName = String::Format(u"{0} part {1}, of type {2}{3}", mOutFileName, ++mCount, partType,
System::IO::Path::GetExtension(args->get_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->set_DocumentPartFileName(partFileName);
// 2 - Create a custom stream for the output part file:
args->set_DocumentPartStream(MakeObject<System::IO::FileStream>(ArtifactsDir + partFileName, System::IO::FileMode::Create));
ASSERT_TRUE(args->get_DocumentPartStream()->get_CanWrite());
ASSERT_FALSE(args->get_KeepDocumentPartStreamOpen());
}
private:
int mCount;
String mOutFileName;
DocumentSplitCriteria mDocumentSplitCriteria;
};
class SavedImageRename : public IImageSavingCallback
{
public:
SavedImageRename(String outFileName) : mCount(0)
{
mOutFileName = outFileName;
}
void ImageSaving(SharedPtr<ImageSavingArgs> args) override
{
String imageFileName = String::Format(u"{0} shape {1}, of type {2}{3}", mOutFileName, ++mCount, args->get_CurrentShape()->get_ShapeType(),
System::IO::Path::GetExtension(args->get_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->set_ImageFileName(imageFileName);
// 2 - Create a custom stream for the output image file:
args->set_ImageStream(MakeObject<System::IO::FileStream>(ArtifactsDir + imageFileName, System::IO::FileMode::Create));
ASSERT_TRUE(args->get_ImageStream()->get_CanWrite());
ASSERT_TRUE(args->get_IsImageAvailable());
ASSERT_FALSE(args->get_KeepImageStreamOpen());
}
private:
int mCount;
String mOutFileName;
};

◆ get_ImageStream()

System::SharedPtr<System::IO::Stream> Aspose::Words::Saving::ImageSavingArgs::get_ImageStream ( ) const

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

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 ImageFileName property.

Using IImageSavingCallback you cannot substitute one image with another. It is intended only for control over location where to save images.

See also
Aspose::Words::Saving::ImageSavingArgs::get_ImageFileName
Aspose::Words::Saving::ImageSavingArgs::get_KeepImageStreamOpen
Examples

Shows how to involve an image saving callback in an HTML conversion process.

void ImageSavingCallback()
{
auto doc = MakeObject<Document>(MyDir + u"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.
auto options = MakeObject<HtmlSaveOptions>();
options->set_ImageSavingCallback(MakeObject<ExHtmlSaveOptions::ImageShapePrinter>());
doc->Save(ArtifactsDir + u"HtmlSaveOptions.ImageSavingCallback.html", options);
}
class ImageShapePrinter : public IImageSavingCallback
{
public:
void ImageSaving(SharedPtr<ImageSavingArgs> args) override
{
args->set_KeepImageStreamOpen(false);
ASSERT_TRUE(args->get_IsImageAvailable());
std::cout << args->get_Document()->get_OriginalFileName().Split(MakeArray<char16_t>({u'\\'}))->LINQ_Last() << " Image #" << ++mImageCount
<< std::endl;
auto layoutCollector = MakeObject<LayoutCollector>(args->get_Document());
std::cout << "\tOn page:\t" << layoutCollector->GetStartPageIndex(args->get_CurrentShape()) << std::endl;
std::cout << "\tDimensions:\t" << args->get_CurrentShape()->get_Bounds() << std::endl;
std::cout << String::Format(u"\tAlignment:\t{0}", args->get_CurrentShape()->get_VerticalAlignment()) << std::endl;
std::cout << String::Format(u"\tWrap type:\t{0}", args->get_CurrentShape()->get_WrapType()) << std::endl;
std::cout << "Output filename:\t" << args->get_ImageFileName() << "\n" << std::endl;
}
ImageShapePrinter() : mImageCount(0)
{
}
private:
int mImageCount;
};

◆ get_IsImageAvailable()

bool Aspose::Words::Saving::ImageSavingArgs::get_IsImageAvailable ( ) const

Returns true if the current image is available for export.

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
Aspose::Words::Saving::ImageSavingArgs::get_CurrentShape
Examples

Shows how to involve an image saving callback in an HTML conversion process.

void ImageSavingCallback()
{
auto doc = MakeObject<Document>(MyDir + u"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.
auto options = MakeObject<HtmlSaveOptions>();
options->set_ImageSavingCallback(MakeObject<ExHtmlSaveOptions::ImageShapePrinter>());
doc->Save(ArtifactsDir + u"HtmlSaveOptions.ImageSavingCallback.html", options);
}
class ImageShapePrinter : public IImageSavingCallback
{
public:
void ImageSaving(SharedPtr<ImageSavingArgs> args) override
{
args->set_KeepImageStreamOpen(false);
ASSERT_TRUE(args->get_IsImageAvailable());
std::cout << args->get_Document()->get_OriginalFileName().Split(MakeArray<char16_t>({u'\\'}))->LINQ_Last() << " Image #" << ++mImageCount
<< std::endl;
auto layoutCollector = MakeObject<LayoutCollector>(args->get_Document());
std::cout << "\tOn page:\t" << layoutCollector->GetStartPageIndex(args->get_CurrentShape()) << std::endl;
std::cout << "\tDimensions:\t" << args->get_CurrentShape()->get_Bounds() << std::endl;
std::cout << String::Format(u"\tAlignment:\t{0}", args->get_CurrentShape()->get_VerticalAlignment()) << std::endl;
std::cout << String::Format(u"\tWrap type:\t{0}", args->get_CurrentShape()->get_WrapType()) << std::endl;
std::cout << "Output filename:\t" << args->get_ImageFileName() << "\n" << std::endl;
}
ImageShapePrinter() : mImageCount(0)
{
}
private:
int mImageCount;
};

◆ get_KeepImageStreamOpen()

bool Aspose::Words::Saving::ImageSavingArgs::get_KeepImageStreamOpen ( ) const

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

Default is false and Aspose.Words will close the stream you provided in the ImageStream property after writing an image into it. Specify true to keep the stream open.

See also
Aspose::Words::Saving::ImageSavingArgs::get_ImageStream
Examples

Shows how to involve an image saving callback in an HTML conversion process.

void ImageSavingCallback()
{
auto doc = MakeObject<Document>(MyDir + u"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.
auto options = MakeObject<HtmlSaveOptions>();
options->set_ImageSavingCallback(MakeObject<ExHtmlSaveOptions::ImageShapePrinter>());
doc->Save(ArtifactsDir + u"HtmlSaveOptions.ImageSavingCallback.html", options);
}
class ImageShapePrinter : public IImageSavingCallback
{
public:
void ImageSaving(SharedPtr<ImageSavingArgs> args) override
{
args->set_KeepImageStreamOpen(false);
ASSERT_TRUE(args->get_IsImageAvailable());
std::cout << args->get_Document()->get_OriginalFileName().Split(MakeArray<char16_t>({u'\\'}))->LINQ_Last() << " Image #" << ++mImageCount
<< std::endl;
auto layoutCollector = MakeObject<LayoutCollector>(args->get_Document());
std::cout << "\tOn page:\t" << layoutCollector->GetStartPageIndex(args->get_CurrentShape()) << std::endl;
std::cout << "\tDimensions:\t" << args->get_CurrentShape()->get_Bounds() << std::endl;
std::cout << String::Format(u"\tAlignment:\t{0}", args->get_CurrentShape()->get_VerticalAlignment()) << std::endl;
std::cout << String::Format(u"\tWrap type:\t{0}", args->get_CurrentShape()->get_WrapType()) << std::endl;
std::cout << "Output filename:\t" << args->get_ImageFileName() << "\n" << std::endl;
}
ImageShapePrinter() : mImageCount(0)
{
}
private:
int mImageCount;
};

◆ GetType()

virtual const System::TypeInfo& Aspose::Words::Saving::ImageSavingArgs::GetType ( ) const
overridevirtual

Reimplemented from System::Object.

◆ Is()

virtual bool Aspose::Words::Saving::ImageSavingArgs::Is ( const System::TypeInfo target) const
overridevirtual

Reimplemented from System::Object.

◆ set_ImageFileName()

void Aspose::Words::Saving::ImageSavingArgs::set_ImageFileName ( System::String  value)

◆ set_ImageStream()

void Aspose::Words::Saving::ImageSavingArgs::set_ImageStream ( System::SharedPtr< System::IO::Stream value)

◆ set_KeepImageStreamOpen()

void Aspose::Words::Saving::ImageSavingArgs::set_KeepImageStreamOpen ( bool  value)

◆ Type()

static const System::TypeInfo& Aspose::Words::Saving::ImageSavingArgs::Type ( )
static