Aspose::Words::Loading::LoadOptions Class Reference

Detailed Description

Allows to specify additional options (such as password or base URI) when loading a document into a Document object.

Examples

Shows how to load an encrypted Microsoft Word document.

SharedPtr<Document> doc;
// Aspose.Words throw an exception if we try to open an encrypted document without its password.
ASSERT_THROW(doc = MakeObject<Document>(MyDir + u"Encrypted.docx"), IncorrectPasswordException);
// When loading such a document, the password is passed to the document's constructor using a LoadOptions object.
auto options = MakeObject<LoadOptions>(u"docPassword");
// There are two ways of loading an encrypted document with a LoadOptions object.
// 1 - Load the document from the local file system by filename:
doc = MakeObject<Document>(MyDir + u"Encrypted.docx", options);
// 2 - Load the document from a stream:
{
SharedPtr<System::IO::Stream> stream = System::IO::File::OpenRead(MyDir + u"Encrypted.docx");
doc = MakeObject<Document>(stream, options);
}

#include <Aspose.Words.Cpp/Loading/LoadOptions.h>

+ Inheritance diagram for Aspose::Words::Loading::LoadOptions:

Public Member Functions

 LoadOptions ()
 Initializes a new instance of this class with default values. More...
 
 LoadOptions (LoadFormat loadFormat, String password, String baseUri)
 A shortcut to initialize a new instance of this class with properties set to the specified values. More...
 
 LoadOptions (String password)
 A shortcut to initialize a new instance of this class with the specified password to load an encrypted document. More...
 
String get_BaseUri () const
 Gets or sets the string that will be used to resolve relative URIs found in the document into absolute URIs when required. Can be null or empty string. Default is null. More...
 
bool get_ConvertMetafilesToPng () const
 Gets or sets whether to convert metafile (Wmf or Emf) images to Png image format. More...
 
bool get_ConvertShapeToOfficeMath () const
 Gets or sets whether to convert shapes with EquationXML to Office Math objects. More...
 
SharedPtr< Encodingget_Encoding () const
 Gets or sets the encoding that will be used to load an HTML, TXT, or CHM document if the encoding is not specified inside the document. Can be null. Default is null. More...
 
SharedPtr< FontSettingsget_FontSettings () const
 Allows to specify document font settings. More...
 
SharedPtr< LanguagePreferencesget_LanguagePreferences () const
 Gets language preferences that will be used when document is loading. More...
 
LoadFormat get_LoadFormat () const
 Specifies the format of the document to be loaded. Default is Auto. More...
 
MsWordVersion get_MswVersion () const
 Allows to specify that the document loading process should match a specific MS Word version. Default value is Word2007 More...
 
String get_Password () const
 Gets or sets the password for opening an encrypted document. Can be null or empty string. Default is null. More...
 
bool get_PreserveIncludePictureField () const
 Gets or sets whether to preserve the INCLUDEPICTURE field when reading Microsoft Word formats. The default value is false. More...
 
SharedPtr< IResourceLoadingCallbackget_ResourceLoadingCallback () const
 Allows to control how external resources (images, style sheets) are loaded when a document is imported from HTML, MHTML. More...
 
String get_TempFolder () const
 Allows to use temporary files when reading document. By default this property is null and no temporary files are used. More...
 
bool get_UpdateDirtyFields () const
 Specifies whether to update the fields with the dirty attribute. More...
 
SharedPtr< IWarningCallbackget_WarningCallback () const
 Called during a load operation, when an issue is detected that might result in data or formatting fidelity loss. More...
 
virtual const TypeInfoGetType () const override
 
virtual bool Is (const TypeInfo &target) const override
 
void set_BaseUri (String value)
 Setter for get_BaseUri. More...
 
void set_ConvertMetafilesToPng (bool value)
 Setter for get_ConvertMetafilesToPng. More...
 
void set_ConvertShapeToOfficeMath (bool value)
 Setter for get_ConvertShapeToOfficeMath. More...
 
void set_Encoding (SharedPtr< Encoding > value)
 Setter for get_Encoding. More...
 
void set_FontSettings (SharedPtr< FontSettings > value)
 Setter for get_FontSettings. More...
 
void set_LoadFormat (LoadFormat value)
 Setter for get_LoadFormat. More...
 
void set_MswVersion (MsWordVersion value)
 Setter for get_MswVersion. More...
 
void set_Password (String value)
 Setter for get_Password. More...
 
void set_PreserveIncludePictureField (bool value)
 Setter for get_PreserveIncludePictureField. More...
 
void set_ResourceLoadingCallback (SharedPtr< IResourceLoadingCallback > value)
 Setter for get_ResourceLoadingCallback. More...
 
void set_TempFolder (String value)
 Setter for get_TempFolder. More...
 
void set_UpdateDirtyFields (bool value)
 Setter for get_UpdateDirtyFields. More...
 
void set_WarningCallback (SharedPtr< IWarningCallback > value)
 Setter for get_WarningCallback. More...
 

Static Public Member Functions

static const TypeInfoType ()
 

Constructor & Destructor Documentation

◆ LoadOptions() [1/3]

Aspose::Words::Loading::LoadOptions::LoadOptions ( )

Initializes a new instance of this class with default values.

Examples

Shows how to open an HTML document with images from a stream using a base URI.

{
SharedPtr<System::IO::Stream> stream = System::IO::File::OpenRead(MyDir + u"Document.html");
// Pass the URI of the base folder while loading it
// so that any images with relative URIs in the HTML document can be found.
auto loadOptions = MakeObject<LoadOptions>();
loadOptions->set_BaseUri(ImageDir);
auto doc = MakeObject<Document>(stream, loadOptions);
// Verify that the first shape of the document contains a valid image.
auto shape = System::DynamicCast<Shape>(doc->GetChild(NodeType::Shape, 0, true));
ASSERT_TRUE(shape->get_IsImage());
ASSERT_FALSE(shape->get_ImageData()->get_ImageBytes() == nullptr);
ASSERT_NEAR(32.0, ConvertUtil::PointToPixel(shape->get_Width()), 0.01);
ASSERT_NEAR(32.0, ConvertUtil::PointToPixel(shape->get_Height()), 0.01);
}

◆ LoadOptions() [2/3]

Aspose::Words::Loading::LoadOptions::LoadOptions ( System::String  password)

A shortcut to initialize a new instance of this class with the specified password to load an encrypted document.

Parameters
passwordThe password to open an encrypted document. Can be null or empty string.
Examples

Shows how to load an encrypted Microsoft Word document.

SharedPtr<Document> doc;
// Aspose.Words throw an exception if we try to open an encrypted document without its password.
ASSERT_THROW(doc = MakeObject<Document>(MyDir + u"Encrypted.docx"), IncorrectPasswordException);
// When loading such a document, the password is passed to the document's constructor using a LoadOptions object.
auto options = MakeObject<LoadOptions>(u"docPassword");
// There are two ways of loading an encrypted document with a LoadOptions object.
// 1 - Load the document from the local file system by filename:
doc = MakeObject<Document>(MyDir + u"Encrypted.docx", options);
// 2 - Load the document from a stream:
{
SharedPtr<System::IO::Stream> stream = System::IO::File::OpenRead(MyDir + u"Encrypted.docx");
doc = MakeObject<Document>(stream, options);
}

◆ LoadOptions() [3/3]

Aspose::Words::Loading::LoadOptions::LoadOptions ( Aspose::Words::LoadFormat  loadFormat,
System::String  password,
System::String  baseUri 
)

A shortcut to initialize a new instance of this class with properties set to the specified values.

Parameters
loadFormatThe format of the document to be loaded.
passwordThe password to open an encrypted document. Can be null or empty string.
baseUriThe string that will be used to resolve relative URIs to absolute. Can be null or empty string.
Examples

Shows how save a web page as a .docx file.

const String url = u"https://www.aspose.com/";
{
auto client = MakeObject<System::Net::WebClient>();
{
auto stream = MakeObject<System::IO::MemoryStream>(client->DownloadData(url));
// The URL is used again as a baseUri to ensure that any relative image paths are retrieved correctly.
auto options = MakeObject<LoadOptions>(LoadFormat::Html, u"", url);
// Load the HTML document from stream and pass the LoadOptions object.
auto doc = MakeObject<Document>(stream, options);
// At this stage, we can read and edit the document's contents and then save it to the local file system.
doc->Save(ArtifactsDir + u"Document.InsertHtmlFromWebPage.docx");
}
}

Shows how to specify a base URI when opening an html document.

// Suppose we want to load an .html document that contains an image linked by a relative URI
// while the image is in a different location. In that case, we will need to resolve the relative URI into an absolute one.
// We can provide a base URI using an HtmlLoadOptions object.
auto loadOptions = MakeObject<HtmlLoadOptions>(LoadFormat::Html, u"", ImageDir);
ASSERT_EQ(LoadFormat::Html, loadOptions->get_LoadFormat());
auto doc = MakeObject<Document>(MyDir + u"Missing image.html", loadOptions);
// While the image was broken in the input .html, our custom base URI helped us repair the link.
auto imageShape = System::DynamicCast<Shape>(doc->GetChildNodes(NodeType::Shape, true)->idx_get(0));
ASSERT_TRUE(imageShape->get_IsImage());
// This output document will display the image that was missing.
doc->Save(ArtifactsDir + u"HtmlLoadOptions.BaseUri.docx");

Member Function Documentation

◆ get_BaseUri()

System::String Aspose::Words::Loading::LoadOptions::get_BaseUri ( ) const

Gets or sets the string that will be used to resolve relative URIs found in the document into absolute URIs when required. Can be null or empty string. Default is null.

This property is used to resolve relative URIs into absolute in the following cases:

  1. When loading an HTML document from a stream and the document contains images with relative URIs and does not have a base URI specified in the BASE HTML element.
  2. When saving a document to PDF and other formats, to retrieve images linked using relative URIs so the images can be saved into the output document.
Examples

Shows how to open an HTML document with images from a stream using a base URI.

{
SharedPtr<System::IO::Stream> stream = System::IO::File::OpenRead(MyDir + u"Document.html");
// Pass the URI of the base folder while loading it
// so that any images with relative URIs in the HTML document can be found.
auto loadOptions = MakeObject<LoadOptions>();
loadOptions->set_BaseUri(ImageDir);
auto doc = MakeObject<Document>(stream, loadOptions);
// Verify that the first shape of the document contains a valid image.
auto shape = System::DynamicCast<Shape>(doc->GetChild(NodeType::Shape, 0, true));
ASSERT_TRUE(shape->get_IsImage());
ASSERT_FALSE(shape->get_ImageData()->get_ImageBytes() == nullptr);
ASSERT_NEAR(32.0, ConvertUtil::PointToPixel(shape->get_Width()), 0.01);
ASSERT_NEAR(32.0, ConvertUtil::PointToPixel(shape->get_Height()), 0.01);
}

◆ get_ConvertMetafilesToPng()

bool Aspose::Words::Loading::LoadOptions::get_ConvertMetafilesToPng ( ) const

Gets or sets whether to convert metafile (Wmf or Emf) images to Png image format.

Examples

Shows how to convert WMF/EMF to PNG during loading document.

auto doc = MakeObject<Document>();
auto shape = MakeObject<Shape>(doc, ShapeType::Image);
shape->get_ImageData()->SetImage(ImageDir + u"Windows MetaFile.wmf");
shape->set_Width(100);
shape->set_Height(100);
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(shape);
doc->Save(ArtifactsDir + u"Image.CreateImageDirectly.docx");
shape = System::DynamicCast<Shape>(doc->GetChild(NodeType::Shape, 0, true));
TestUtil::VerifyImageInShape(1600, 1600, ImageType::Wmf, shape);
auto loadOptions = MakeObject<LoadOptions>();
loadOptions->set_ConvertMetafilesToPng(true);
doc = MakeObject<Document>(ArtifactsDir + u"Image.CreateImageDirectly.docx", loadOptions);
shape = System::DynamicCast<Shape>(doc->GetChild(NodeType::Shape, 0, true));

◆ get_ConvertShapeToOfficeMath()

bool Aspose::Words::Loading::LoadOptions::get_ConvertShapeToOfficeMath ( ) const

Gets or sets whether to convert shapes with EquationXML to Office Math objects.

Examples

Shows how to convert EquationXML shapes to Office Math objects.

auto loadOptions = MakeObject<LoadOptions>();
// Use this flag to specify whether to convert the shapes with EquationXML attributes
// to Office Math objects and then load the document.
loadOptions->set_ConvertShapeToOfficeMath(isConvertShapeToOfficeMath);
auto doc = MakeObject<Document>(MyDir + u"Math shapes.docx", loadOptions);
if (isConvertShapeToOfficeMath)
{
ASSERT_EQ(16, doc->GetChildNodes(NodeType::Shape, true)->get_Count());
ASSERT_EQ(34, doc->GetChildNodes(NodeType::OfficeMath, true)->get_Count());
}
else
{
ASSERT_EQ(24, doc->GetChildNodes(NodeType::Shape, true)->get_Count());
ASSERT_EQ(0, doc->GetChildNodes(NodeType::OfficeMath, true)->get_Count());
}

◆ get_Encoding()

System::SharedPtr<System::Text::Encoding> Aspose::Words::Loading::LoadOptions::get_Encoding ( ) const

Gets or sets the encoding that will be used to load an HTML, TXT, or CHM document if the encoding is not specified inside the document. Can be null. Default is null.

This property is used only when loading HTML, TXT, or CHM documents.

If encoding is not specified inside the document and this property is null, then the system will try to automatically detect the encoding.

Examples

Shows how to set the encoding with which to open a document.

// A FileFormatInfo object will detect this file as being encoded in something other than UTF-7.
SharedPtr<FileFormatInfo> fileFormatInfo = FileFormatUtil::DetectFileFormat(MyDir + u"Encoded in UTF-7.txt");
ASPOSE_ASSERT_NE(System::Text::Encoding::get_UTF7(), fileFormatInfo->get_Encoding());
// If we load the document with no loading configurations, Aspose.Words will detect its encoding as UTF-8.
auto doc = MakeObject<Document>(MyDir + u"Encoded in UTF-7.txt");
// The contents, parsed in UTF-8, create a valid string.
// However, knowing that the file is in UTF-7, we can see that the result is incorrect.
ASSERT_EQ(u"Hello world+ACE-", doc->ToString(SaveFormat::Text).Trim());
// In cases of ambiguous encoding such as this one, we can set a specific encoding variant
// to parse the file within a LoadOptions object.
auto loadOptions = MakeObject<LoadOptions>();
loadOptions->set_Encoding(System::Text::Encoding::get_UTF7());
// Load the document while passing the LoadOptions object, then verify the document's contents.
doc = MakeObject<Document>(MyDir + u"Encoded in UTF-7.txt", loadOptions);
ASSERT_EQ(u"Hello world!", doc->ToString(SaveFormat::Text).Trim());

◆ get_FontSettings()

System::SharedPtr<Aspose::Words::Fonts::FontSettings> Aspose::Words::Loading::LoadOptions::get_FontSettings ( ) const

Allows to specify document font settings.

When loading some formats, Aspose.Words may require to resolve the fonts. For example, when loading HTML documents Aspose.Words may resolve the fonts to perform font fallback.

If set to null, default static font settings DefaultInstance will be used.

The default value is null.

Examples

Shows how to designate font substitutes during loading.

auto loadOptions = MakeObject<LoadOptions>();
loadOptions->set_FontSettings(MakeObject<FontSettings>());
// Set a font substitution rule for a LoadOptions object.
// If the document we are loading uses a font which we do not have,
// this rule will substitute the unavailable font with one that does exist.
// In this case, all uses of the "MissingFont" will convert to "Comic Sans MS".
SharedPtr<TableSubstitutionRule> substitutionRule = loadOptions->get_FontSettings()->get_SubstitutionSettings()->get_TableSubstitution();
substitutionRule->AddSubstitutes(u"MissingFont", MakeArray<String>({u"Comic Sans MS"}));
auto doc = MakeObject<Document>(MyDir + u"Missing font.html", loadOptions);
// At this point such text will still be in "MissingFont".
// Font substitution will take place when we render the document.
ASSERT_EQ(u"MissingFont", doc->get_FirstSection()->get_Body()->get_FirstParagraph()->get_Runs()->idx_get(0)->get_Font()->get_Name());
doc->Save(ArtifactsDir + u"FontSettings.ResolveFontsBeforeLoadingDocument.pdf");

Shows how to apply font substitution settings while loading a document.

// Create a FontSettings object that will substitute the "Times New Roman" font
// with the font "Arvo" from our "MyFonts" folder.
auto fontSettings = MakeObject<FontSettings>();
fontSettings->SetFontsFolder(FontsDir, false);
fontSettings->get_SubstitutionSettings()->get_TableSubstitution()->AddSubstitutes(u"Times New Roman", MakeArray<String>({u"Arvo"}));
// Set that FontSettings object as a property of a newly created LoadOptions object.
auto loadOptions = MakeObject<LoadOptions>();
loadOptions->set_FontSettings(fontSettings);
// Load the document, then render it as a PDF with the font substitution.
auto doc = MakeObject<Document>(MyDir + u"Document.docx", loadOptions);
doc->Save(ArtifactsDir + u"LoadOptions.FontSettings.pdf");

◆ get_LanguagePreferences()

System::SharedPtr<Aspose::Words::Loading::LanguagePreferences> Aspose::Words::Loading::LoadOptions::get_LanguagePreferences ( ) const

Gets language preferences that will be used when document is loading.

Examples

Shows how to apply language preferences when loading a document.

auto loadOptions = MakeObject<LoadOptions>();
loadOptions->get_LanguagePreferences()->AddEditingLanguage(EditingLanguage::Japanese);
auto doc = MakeObject<Document>(MyDir + u"No default editing language.docx", loadOptions);
int localeIdFarEast = doc->get_Styles()->get_DefaultFont()->get_LocaleIdFarEast();
std::cout << (localeIdFarEast == (int)EditingLanguage::Japanese
? String(u"The document either has no any FarEast language set in defaults or it was set to Japanese originally.")
: String(u"The document default FarEast language was set to another than Japanese language originally, so it is not overridden."))
<< std::endl;

◆ get_LoadFormat()

Aspose::Words::LoadFormat Aspose::Words::Loading::LoadOptions::get_LoadFormat ( ) const

Specifies the format of the document to be loaded. Default is Auto.

It is recommended that you specify the Auto value and let Aspose.Words detect the file format automatically. If you know the format of the document you are about to load, you can specify the format explicitly and this will slightly reduce the loading time by the overhead associated with auto detecting the format. If you specify an explicit load format and it will turn out to be wrong, the auto detection will be invoked and a second attempt to load the file will be made.

Examples

Shows how to specify a base URI when opening an html document.

// Suppose we want to load an .html document that contains an image linked by a relative URI
// while the image is in a different location. In that case, we will need to resolve the relative URI into an absolute one.
// We can provide a base URI using an HtmlLoadOptions object.
auto loadOptions = MakeObject<HtmlLoadOptions>(LoadFormat::Html, u"", ImageDir);
ASSERT_EQ(LoadFormat::Html, loadOptions->get_LoadFormat());
auto doc = MakeObject<Document>(MyDir + u"Missing image.html", loadOptions);
// While the image was broken in the input .html, our custom base URI helped us repair the link.
auto imageShape = System::DynamicCast<Shape>(doc->GetChildNodes(NodeType::Shape, true)->idx_get(0));
ASSERT_TRUE(imageShape->get_IsImage());
// This output document will display the image that was missing.
doc->Save(ArtifactsDir + u"HtmlLoadOptions.BaseUri.docx");

◆ get_MswVersion()

Aspose::Words::Settings::MsWordVersion Aspose::Words::Loading::LoadOptions::get_MswVersion ( ) const

Allows to specify that the document loading process should match a specific MS Word version. Default value is Word2007

Examples

Shows how to emulate the loading procedure of a specific Microsoft Word version during document loading.

// By default, Aspose.Words load documents according to Microsoft Word 2019 specification.
auto loadOptions = MakeObject<LoadOptions>();
ASSERT_EQ(MsWordVersion::Word2019, loadOptions->get_MswVersion());
// This document is missing the default paragraph formatting style.
// This default style will be regenerated when we load the document either with Microsoft Word or Aspose.Words.
loadOptions->set_MswVersion(MsWordVersion::Word2007);
auto doc = MakeObject<Document>(MyDir + u"Document.docx", loadOptions);
// The style's line spacing will have this value when loaded by Microsoft Word 2007 specification.
ASSERT_NEAR(12.95, doc->get_Styles()->get_DefaultParagraphFormat()->get_LineSpacing(), 0.01);

◆ get_Password()

System::String Aspose::Words::Loading::LoadOptions::get_Password ( ) const

Gets or sets the password for opening an encrypted document. Can be null or empty string. Default is null.

You need to know the password to open an encrypted document. If the document is not encrypted, set this to null or empty string.

Examples

Shows how to sign encrypted document file.

// Create an X.509 certificate from a PKCS#12 store, which should contain a private key.
SharedPtr<CertificateHolder> certificateHolder = CertificateHolder::Create(MyDir + u"morzal.pfx", u"aw");
// Create a comment, date, and decryption password which will be applied with our new digital signature.
auto signOptions = MakeObject<SignOptions>();
signOptions->set_Comments(u"Comment");
signOptions->set_SignTime(System::DateTime::get_Now());
signOptions->set_DecryptionPassword(u"docPassword");
// Set a local system filename for the unsigned input document, and an output filename for its new digitally signed copy.
String inputFileName = MyDir + u"Encrypted.docx";
String outputFileName = ArtifactsDir + u"DigitalSignatureUtil.DecryptionPassword.docx";
DigitalSignatureUtil::Sign(inputFileName, outputFileName, certificateHolder, signOptions);

◆ get_PreserveIncludePictureField()

bool Aspose::Words::Loading::LoadOptions::get_PreserveIncludePictureField ( ) const

Gets or sets whether to preserve the INCLUDEPICTURE field when reading Microsoft Word formats. The default value is false.

By default, the INCLUDEPICTURE field is converted into a shape object. You can override that if you need the field to be preserved, for example, if you wish to update it programmatically. Note however that this approach is not common for Aspose.Words. Use it on your own risk.

One of the possible use cases may be using a MERGEFIELD as a child field to dynamically change the source path of the picture. In this case you need the INCLUDEPICTURE to be preserved in the model.

Examples

Shows how to preserve or discard INCLUDEPICTURE fields when loading a document.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
auto includePicture = System::DynamicCast<FieldIncludePicture>(builder->InsertField(FieldType::FieldIncludePicture, true));
includePicture->set_SourceFullName(ImageDir + u"Transparent background logo.png");
includePicture->Update(true);
{
auto docStream = MakeObject<System::IO::MemoryStream>();
doc->Save(docStream, MakeObject<OoxmlSaveOptions>(SaveFormat::Docx));
// We can set a flag in a LoadOptions object to decide whether to convert all INCLUDEPICTURE fields
// into image shapes when loading a document that contains them.
auto loadOptions = MakeObject<Loading::LoadOptions>();
loadOptions->set_PreserveIncludePictureField(preserveIncludePictureField);
doc = MakeObject<Document>(docStream, loadOptions);
if (preserveIncludePictureField)
{
ASSERT_TRUE(doc->get_Range()->get_Fields()->LINQ_Any([](SharedPtr<Field> f) { return f->get_Type() == FieldType::FieldIncludePicture; }));
doc->UpdateFields();
doc->Save(ArtifactsDir + u"Field.PreserveIncludePicture.docx");
}
else
{
ASSERT_FALSE(doc->get_Range()->get_Fields()->LINQ_Any([](SharedPtr<Field> f) { return f->get_Type() == FieldType::FieldIncludePicture; }));
}
}

◆ get_ResourceLoadingCallback()

System::SharedPtr<Aspose::Words::Loading::IResourceLoadingCallback> Aspose::Words::Loading::LoadOptions::get_ResourceLoadingCallback ( ) const

Allows to control how external resources (images, style sheets) are loaded when a document is imported from HTML, MHTML.

Examples

Shows how to handle external resources when loading Html documents.

void LoadOptionsCallback()
{
auto loadOptions = MakeObject<LoadOptions>();
loadOptions->set_ResourceLoadingCallback(MakeObject<ExLoadOptions::HtmlLinkedResourceLoadingCallback>());
// When we load the document, our callback will handle linked resources such as CSS stylesheets and images.
auto doc = MakeObject<Document>(MyDir + u"Images.html", loadOptions);
doc->Save(ArtifactsDir + u"LoadOptions.LoadOptionsCallback.pdf");
}
class HtmlLinkedResourceLoadingCallback : public IResourceLoadingCallback
{
public:
ResourceLoadingAction ResourceLoading(SharedPtr<ResourceLoadingArgs> args) override
{
switch (args->get_ResourceType())
{
std::cout << "External CSS Stylesheet found upon loading: " << args->get_OriginalUri() << std::endl;
std::cout << "External Image found upon loading: " << args->get_OriginalUri() << std::endl;
const String newImageFilename = u"Logo.jpg";
std::cout << "\tImage will be substituted with: " << newImageFilename << std::endl;
SharedPtr<System::Drawing::Image> newImage = System::Drawing::Image::FromFile(ImageDir + newImageFilename);
auto converter = MakeObject<System::Drawing::ImageConverter>();
ArrayPtr<uint8_t> imageBytes =
System::DynamicCast<System::Array<uint8_t>>(converter->ConvertTo(newImage, System::ObjectExt::GetType<System::Array<uint8_t>>()));
args->SetData(imageBytes);
}
default:
break;
}
}
};

◆ get_TempFolder()

System::String Aspose::Words::Loading::LoadOptions::get_TempFolder ( ) const

Allows to use temporary files when reading document. By default this property is null and no temporary files are used.

The folder must exist and be writable, otherwise an exception will be thrown.

Aspose.Words automatically deletes all temporary files when reading is complete.

Examples

Shows how to load a document using temporary files.

// Note that such an approach can reduce memory usage but degrades speed
auto loadOptions = MakeObject<LoadOptions>();
loadOptions->set_TempFolder(u"C:\\TempFolder\\");
// Ensure that the directory exists and load
System::IO::Directory::CreateDirectory_(loadOptions->get_TempFolder());
auto doc = MakeObject<Document>(MyDir + u"Document.docx", loadOptions);

Shows how to use the hard drive instead of memory when loading a document.

// When we load a document, various elements are temporarily stored in memory as the save operation occurs.
// We can use this option to use a temporary folder in the local file system instead,
// which will reduce our application's memory overhead.
auto options = MakeObject<LoadOptions>();
options->set_TempFolder(ArtifactsDir + u"TempFiles");
// The specified temporary folder must exist in the local file system before the load operation.
System::IO::Directory::CreateDirectory_(options->get_TempFolder());
auto doc = MakeObject<Document>(MyDir + u"Document.docx", options);
// The folder will persist with no residual contents from the load operation.
ASSERT_EQ(0, System::IO::Directory::GetFiles(options->get_TempFolder())->get_Length());

◆ get_UpdateDirtyFields()

bool Aspose::Words::Loading::LoadOptions::get_UpdateDirtyFields ( ) const

Specifies whether to update the fields with the dirty attribute.

Examples

Shows how to use special property for updating field result.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Give the document's built-in "Author" property value, and then display it with a field.
doc->get_BuiltInDocumentProperties()->set_Author(u"John Doe");
auto field = System::DynamicCast<FieldAuthor>(builder->InsertField(FieldType::FieldAuthor, true));
ASSERT_FALSE(field->get_IsDirty());
ASSERT_EQ(u"John Doe", field->get_Result());
// Update the property. The field still displays the old value.
doc->get_BuiltInDocumentProperties()->set_Author(u"John & Jane Doe");
ASSERT_EQ(u"John Doe", field->get_Result());
// Since the field's value is out of date, we can mark it as "dirty".
// This value will stay out of date until we update the field manually with the Field.Update() method.
field->set_IsDirty(true);
{
auto docStream = MakeObject<System::IO::MemoryStream>();
// If we save without calling an update method,
// the field will keep displaying the out of date value in the output document.
doc->Save(docStream, SaveFormat::Docx);
// The LoadOptions object has an option to update all fields
// marked as "dirty" when loading the document.
auto options = MakeObject<Loading::LoadOptions>();
options->set_UpdateDirtyFields(updateDirtyFields);
doc = MakeObject<Document>(docStream, options);
ASSERT_EQ(u"John & Jane Doe", doc->get_BuiltInDocumentProperties()->get_Author());
field = System::DynamicCast<FieldAuthor>(doc->get_Range()->get_Fields()->idx_get(0));
// Updating dirty fields like this automatically set their "IsDirty" flag to false.
if (updateDirtyFields)
{
ASSERT_EQ(u"John & Jane Doe", field->get_Result());
ASSERT_FALSE(field->get_IsDirty());
}
else
{
ASSERT_EQ(u"John Doe", field->get_Result());
ASSERT_TRUE(field->get_IsDirty());
}
}

◆ get_WarningCallback()

System::SharedPtr<Aspose::Words::IWarningCallback> Aspose::Words::Loading::LoadOptions::get_WarningCallback ( ) const

Called during a load operation, when an issue is detected that might result in data or formatting fidelity loss.

Examples

Shows how to print and store warnings that occur during document loading.

void LoadOptionsWarningCallback()
{
// Create a new LoadOptions object and set its WarningCallback attribute
// as an instance of our IWarningCallback implementation.
auto loadOptions = MakeObject<LoadOptions>();
loadOptions->set_WarningCallback(MakeObject<ExLoadOptions::DocumentLoadingWarningCallback>());
// Our callback will print all warnings that come up during the load operation.
auto doc = MakeObject<Document>(MyDir + u"Document.docx", loadOptions);
SharedPtr<System::Collections::Generic::List<SharedPtr<WarningInfo>>> warnings =
(System::StaticCast<ApiExamples::ExLoadOptions::DocumentLoadingWarningCallback>(loadOptions->get_WarningCallback()))->GetWarnings();
ASSERT_EQ(3, warnings->get_Count());
}
class DocumentLoadingWarningCallback : public IWarningCallback
{
public:
void Warning(SharedPtr<WarningInfo> info) override
{
std::cout << String::Format(u"Warning: {0}", info->get_WarningType()) << std::endl;
std::cout << String::Format(u"\tSource: {0}", info->get_Source()) << std::endl;
std::cout << "\tDescription: " << info->get_Description() << std::endl;
mWarnings->Add(info);
}
SharedPtr<System::Collections::Generic::List<SharedPtr<WarningInfo>>> GetWarnings()
{
return mWarnings;
}
DocumentLoadingWarningCallback() : mWarnings(MakeObject<System::Collections::Generic::List<SharedPtr<WarningInfo>>>())
{
}
private:
SharedPtr<System::Collections::Generic::List<SharedPtr<WarningInfo>>> mWarnings;
};

◆ GetType()

virtual const System::TypeInfo& Aspose::Words::Loading::LoadOptions::GetType ( ) const
overridevirtual

◆ Is()

virtual bool Aspose::Words::Loading::LoadOptions::Is ( const System::TypeInfo target) const
overridevirtual

◆ set_BaseUri()

void Aspose::Words::Loading::LoadOptions::set_BaseUri ( System::String  value)

◆ set_ConvertMetafilesToPng()

void Aspose::Words::Loading::LoadOptions::set_ConvertMetafilesToPng ( bool  value)

◆ set_ConvertShapeToOfficeMath()

void Aspose::Words::Loading::LoadOptions::set_ConvertShapeToOfficeMath ( bool  value)

◆ set_Encoding()

void Aspose::Words::Loading::LoadOptions::set_Encoding ( System::SharedPtr< System::Text::Encoding value)

◆ set_FontSettings()

void Aspose::Words::Loading::LoadOptions::set_FontSettings ( System::SharedPtr< Aspose::Words::Fonts::FontSettings value)

◆ set_LoadFormat()

void Aspose::Words::Loading::LoadOptions::set_LoadFormat ( Aspose::Words::LoadFormat  value)

◆ set_MswVersion()

void Aspose::Words::Loading::LoadOptions::set_MswVersion ( Aspose::Words::Settings::MsWordVersion  value)

◆ set_Password()

void Aspose::Words::Loading::LoadOptions::set_Password ( System::String  value)

◆ set_PreserveIncludePictureField()

void Aspose::Words::Loading::LoadOptions::set_PreserveIncludePictureField ( bool  value)

◆ set_ResourceLoadingCallback()

void Aspose::Words::Loading::LoadOptions::set_ResourceLoadingCallback ( System::SharedPtr< Aspose::Words::Loading::IResourceLoadingCallback value)

◆ set_TempFolder()

void Aspose::Words::Loading::LoadOptions::set_TempFolder ( System::String  value)

◆ set_UpdateDirtyFields()

void Aspose::Words::Loading::LoadOptions::set_UpdateDirtyFields ( bool  value)

◆ set_WarningCallback()

void Aspose::Words::Loading::LoadOptions::set_WarningCallback ( System::SharedPtr< Aspose::Words::IWarningCallback value)

◆ Type()

static const System::TypeInfo& Aspose::Words::Loading::LoadOptions::Type ( )
static