Aspose::Words::Saving::ResourceSavingArgs Class Reference

Detailed Description

Provides data for the ResourceSaving() event.

By default, when Aspose.Words saves a document to fixed page HTML or SVG, 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.

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

To save resources into streams instead of files, use the ResourceStream property.

Examples

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

void ResourceSavingCallback()
{
auto doc = MakeObject<Document>(MyDir + u"Bullet points with alternative font.docx");
auto callback = MakeObject<ExHtmlFixedSaveOptions::FontSavingCallback>();
auto saveOptions = MakeObject<HtmlFixedSaveOptions>();
saveOptions->set_ResourceSavingCallback(callback);
doc->Save(ArtifactsDir + u"HtmlFixedSaveOptions.UsingMachineFonts.html", saveOptions);
std::cout << callback->GetText() << std::endl;
}
class FontSavingCallback : public IResourceSavingCallback
{
public:
void ResourceSaving(SharedPtr<ResourceSavingArgs> args) override
{
mText->AppendLine(String::Format(u"Original document URI:\t{0}", args->get_Document()->get_OriginalFileName()));
mText->AppendLine(String::Format(u"Resource being saved:\t{0}", args->get_ResourceFileName()));
mText->AppendLine(String::Format(u"Full uri after saving:\t{0}\n", args->get_ResourceFileUri()));
}
String GetText()
{
return mText->ToString();
}
FontSavingCallback() : mText(MakeObject<System::Text::StringBuilder>())
{
}
private:
SharedPtr<System::Text::StringBuilder> mText;
};

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

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

Public Member Functions

SharedPtr< Documentget_Document () const
 Gets the document object that is currently being saved. More...
 
bool get_KeepResourceStreamOpen () const
 Specifies whether Aspose.Words should keep the stream open or close it after saving a resource. More...
 
String get_ResourceFileName () const
 Gets or sets the file name (without path) where the resource will be saved to. More...
 
String get_ResourceFileUri () const
 Gets or sets the uniform resource identifier (URI) used to reference the resource file from the document. More...
 
SharedPtr< Streamget_ResourceStream () const
 Allows to specify the stream where the resource will be saved to. More...
 
virtual const TypeInfoGetType () const override
 
virtual bool Is (const TypeInfo &target) const override
 
void set_KeepResourceStreamOpen (bool value)
 Setter for get_KeepResourceStreamOpen. More...
 
void set_ResourceFileName (const String &value)
 Setter for get_ResourceFileName. More...
 
void set_ResourceFileUri (const String &value)
 Setter for get_ResourceFileUri. More...
 
void set_ResourceStream (const SharedPtr< Stream > &value)
 Setter for get_ResourceStream. More...
 
template<typename CharType , typename Traits >
void set_ResourceStream (std::basic_ostream< CharType, Traits > &value)
 

Static Public Member Functions

static const TypeInfoType ()
 

Member Function Documentation

◆ get_Document()

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

Gets the document object that is currently being saved.

Examples

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

void ResourceSavingCallback()
{
auto doc = MakeObject<Document>(MyDir + u"Bullet points with alternative font.docx");
auto callback = MakeObject<ExHtmlFixedSaveOptions::FontSavingCallback>();
auto saveOptions = MakeObject<HtmlFixedSaveOptions>();
saveOptions->set_ResourceSavingCallback(callback);
doc->Save(ArtifactsDir + u"HtmlFixedSaveOptions.UsingMachineFonts.html", saveOptions);
std::cout << callback->GetText() << std::endl;
}
class FontSavingCallback : public IResourceSavingCallback
{
public:
void ResourceSaving(SharedPtr<ResourceSavingArgs> args) override
{
mText->AppendLine(String::Format(u"Original document URI:\t{0}", args->get_Document()->get_OriginalFileName()));
mText->AppendLine(String::Format(u"Resource being saved:\t{0}", args->get_ResourceFileName()));
mText->AppendLine(String::Format(u"Full uri after saving:\t{0}\n", args->get_ResourceFileUri()));
}
String GetText()
{
return mText->ToString();
}
FontSavingCallback() : mText(MakeObject<System::Text::StringBuilder>())
{
}
private:
SharedPtr<System::Text::StringBuilder> mText;
};

◆ get_KeepResourceStreamOpen()

bool Aspose::Words::Saving::ResourceSavingArgs::get_KeepResourceStreamOpen ( ) const

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

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

See also
Aspose::Words::Saving::ResourceSavingArgs::get_ResourceStream
Examples

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

void HtmlFixedResourceFolder()
{
auto doc = MakeObject<Document>(MyDir + u"Rendering.docx");
auto callback = MakeObject<ExHtmlFixedSaveOptions::ResourceUriPrinter>();
auto options = MakeObject<HtmlFixedSaveOptions>();
options->set_SaveFormat(SaveFormat::HtmlFixed);
options->set_ExportEmbeddedImages(false);
options->set_ResourcesFolder(ArtifactsDir + u"HtmlFixedResourceFolder");
options->set_ResourcesFolderAlias(ArtifactsDir + u"HtmlFixedResourceFolderAlias");
options->set_ShowPageBorder(false);
options->set_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.
System::IO::Directory::CreateDirectory_(options->get_ResourcesFolderAlias());
doc->Save(ArtifactsDir + u"HtmlFixedSaveOptions.HtmlFixedResourceFolder.html", options);
std::cout << callback->GetText() << std::endl;
ArrayPtr<String> resourceFiles = System::IO::Directory::GetFiles(ArtifactsDir + u"HtmlFixedResourceFolderAlias");
ASSERT_FALSE(System::IO::Directory::Exists(ArtifactsDir + u"HtmlFixedResourceFolder"));
std::function<bool(String f)> isImageOrCss = [](String f)
{
return f.EndsWith(u".jpeg") || f.EndsWith(u".png") || f.EndsWith(u".css");
};
ASSERT_EQ(6, resourceFiles->LINQ_Count(isImageOrCss));
}
class ResourceUriPrinter : public IResourceSavingCallback
{
public:
String GetText()
{
return mText->ToString();
}
ResourceUriPrinter() : mSavedResourceCount(0), mText(MakeObject<System::Text::StringBuilder>())
{
}
private:
int mSavedResourceCount;
SharedPtr<System::Text::StringBuilder> mText;
void ResourceSaving(SharedPtr<ResourceSavingArgs> args) override
{
// If we set a folder alias in the SaveOptions object, we will be able to print it from here.
mText->AppendLine(String::Format(u"Resource #{0} \"{1}\"", ++mSavedResourceCount, args->get_ResourceFileName()));
String extension = System::IO::Path::GetExtension(args->get_ResourceFileName());
if (extension == u".ttf" || extension == u".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->set_ResourceFileUri(ArtifactsDir + System::IO::Path::DirectorySeparatorChar + args->get_ResourceFileName());
}
mText->AppendLine(String(u"\t") + args->get_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->set_ResourceStream(MakeObject<System::IO::FileStream>(args->get_ResourceFileUri(), System::IO::FileMode::Create));
args->set_KeepResourceStreamOpen(false);
}
};

◆ get_ResourceFileName()

System::String Aspose::Words::Saving::ResourceSavingArgs::get_ResourceFileName ( ) const

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

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

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

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 or SVG using the document file name, the ResourcesFolder or ResourcesFolder and ResourcesFolderAlias or ResourcesFolderAlias properties.

See also
Aspose::Words::Saving::ResourceSavingArgs::get_ResourceStream
Examples

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

void ResourceSavingCallback()
{
auto doc = MakeObject<Document>(MyDir + u"Bullet points with alternative font.docx");
auto callback = MakeObject<ExHtmlFixedSaveOptions::FontSavingCallback>();
auto saveOptions = MakeObject<HtmlFixedSaveOptions>();
saveOptions->set_ResourceSavingCallback(callback);
doc->Save(ArtifactsDir + u"HtmlFixedSaveOptions.UsingMachineFonts.html", saveOptions);
std::cout << callback->GetText() << std::endl;
}
class FontSavingCallback : public IResourceSavingCallback
{
public:
void ResourceSaving(SharedPtr<ResourceSavingArgs> args) override
{
mText->AppendLine(String::Format(u"Original document URI:\t{0}", args->get_Document()->get_OriginalFileName()));
mText->AppendLine(String::Format(u"Resource being saved:\t{0}", args->get_ResourceFileName()));
mText->AppendLine(String::Format(u"Full uri after saving:\t{0}\n", args->get_ResourceFileUri()));
}
String GetText()
{
return mText->ToString();
}
FontSavingCallback() : mText(MakeObject<System::Text::StringBuilder>())
{
}
private:
SharedPtr<System::Text::StringBuilder> mText;
};

◆ get_ResourceFileUri()

System::String Aspose::Words::Saving::ResourceSavingArgs::get_ResourceFileUri ( ) const

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

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

Aspose.Words automatically generates an URI for every resource file during export to fixed page HTML or SVG 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.

Examples

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

void ResourceSavingCallback()
{
auto doc = MakeObject<Document>(MyDir + u"Bullet points with alternative font.docx");
auto callback = MakeObject<ExHtmlFixedSaveOptions::FontSavingCallback>();
auto saveOptions = MakeObject<HtmlFixedSaveOptions>();
saveOptions->set_ResourceSavingCallback(callback);
doc->Save(ArtifactsDir + u"HtmlFixedSaveOptions.UsingMachineFonts.html", saveOptions);
std::cout << callback->GetText() << std::endl;
}
class FontSavingCallback : public IResourceSavingCallback
{
public:
void ResourceSaving(SharedPtr<ResourceSavingArgs> args) override
{
mText->AppendLine(String::Format(u"Original document URI:\t{0}", args->get_Document()->get_OriginalFileName()));
mText->AppendLine(String::Format(u"Resource being saved:\t{0}", args->get_ResourceFileName()));
mText->AppendLine(String::Format(u"Full uri after saving:\t{0}\n", args->get_ResourceFileUri()));
}
String GetText()
{
return mText->ToString();
}
FontSavingCallback() : mText(MakeObject<System::Text::StringBuilder>())
{
}
private:
SharedPtr<System::Text::StringBuilder> mText;
};

◆ get_ResourceStream()

System::SharedPtr<System::IO::Stream> Aspose::Words::Saving::ResourceSavingArgs::get_ResourceStream ( ) const

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

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

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

See also
Aspose::Words::Saving::ResourceSavingArgs::get_ResourceFileName
Aspose::Words::Saving::ResourceSavingArgs::get_KeepResourceStreamOpen
Examples

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

void HtmlFixedResourceFolder()
{
auto doc = MakeObject<Document>(MyDir + u"Rendering.docx");
auto callback = MakeObject<ExHtmlFixedSaveOptions::ResourceUriPrinter>();
auto options = MakeObject<HtmlFixedSaveOptions>();
options->set_SaveFormat(SaveFormat::HtmlFixed);
options->set_ExportEmbeddedImages(false);
options->set_ResourcesFolder(ArtifactsDir + u"HtmlFixedResourceFolder");
options->set_ResourcesFolderAlias(ArtifactsDir + u"HtmlFixedResourceFolderAlias");
options->set_ShowPageBorder(false);
options->set_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.
System::IO::Directory::CreateDirectory_(options->get_ResourcesFolderAlias());
doc->Save(ArtifactsDir + u"HtmlFixedSaveOptions.HtmlFixedResourceFolder.html", options);
std::cout << callback->GetText() << std::endl;
ArrayPtr<String> resourceFiles = System::IO::Directory::GetFiles(ArtifactsDir + u"HtmlFixedResourceFolderAlias");
ASSERT_FALSE(System::IO::Directory::Exists(ArtifactsDir + u"HtmlFixedResourceFolder"));
std::function<bool(String f)> isImageOrCss = [](String f)
{
return f.EndsWith(u".jpeg") || f.EndsWith(u".png") || f.EndsWith(u".css");
};
ASSERT_EQ(6, resourceFiles->LINQ_Count(isImageOrCss));
}
class ResourceUriPrinter : public IResourceSavingCallback
{
public:
String GetText()
{
return mText->ToString();
}
ResourceUriPrinter() : mSavedResourceCount(0), mText(MakeObject<System::Text::StringBuilder>())
{
}
private:
int mSavedResourceCount;
SharedPtr<System::Text::StringBuilder> mText;
void ResourceSaving(SharedPtr<ResourceSavingArgs> args) override
{
// If we set a folder alias in the SaveOptions object, we will be able to print it from here.
mText->AppendLine(String::Format(u"Resource #{0} \"{1}\"", ++mSavedResourceCount, args->get_ResourceFileName()));
String extension = System::IO::Path::GetExtension(args->get_ResourceFileName());
if (extension == u".ttf" || extension == u".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->set_ResourceFileUri(ArtifactsDir + System::IO::Path::DirectorySeparatorChar + args->get_ResourceFileName());
}
mText->AppendLine(String(u"\t") + args->get_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->set_ResourceStream(MakeObject<System::IO::FileStream>(args->get_ResourceFileUri(), System::IO::FileMode::Create));
args->set_KeepResourceStreamOpen(false);
}
};

◆ GetType()

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

Reimplemented from System::Object.

◆ Is()

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

Reimplemented from System::Object.

◆ set_KeepResourceStreamOpen()

void Aspose::Words::Saving::ResourceSavingArgs::set_KeepResourceStreamOpen ( bool  value)

◆ set_ResourceFileName()

void Aspose::Words::Saving::ResourceSavingArgs::set_ResourceFileName ( const System::String value)

◆ set_ResourceFileUri()

void Aspose::Words::Saving::ResourceSavingArgs::set_ResourceFileUri ( const System::String value)

◆ set_ResourceStream() [1/2]

void Aspose::Words::Saving::ResourceSavingArgs::set_ResourceStream ( const System::SharedPtr< System::IO::Stream > &  value)

◆ set_ResourceStream() [2/2]

template<typename CharType , typename Traits >
void Aspose::Words::Saving::ResourceSavingArgs::set_ResourceStream ( std::basic_ostream< CharType, Traits > &  value)
inline

◆ Type()

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