ロードオプションの指定

ドキュメントを読み込むときに、いくつかの高度なプロパティを設定できます。 Aspose.Wordsは、ロードプロセスのより正確な制御を可能にするLoadOptionsクラスを提供します。 たとえば、PDF形式への読み込みにはPdfLoadOptions、TXTへの読み込みにはTxtLoadOptionsがあります。 この記事では、LoadOptionsクラスのオプションを使用する例を示します。

外観を変更するにはMicrosoft Wordバージョンを設定します

Microsoft Wordアプリケーションの異なるバージョンでは、ドキュメントを無関心に表示できます。 たとえば、WPS Officeを使用して作成されたDOCXやDOTXなどのOOXML文書にはよく知られている問題があります。 このような場合、必須の文書マークアップ要素が欠落している可能性があるか、Microsoft Word2019がMicrosoft Word2010と比較してそのような文書を異なる方法で表示する原因とな

デフォルトでAspose.WordsはMicrosoft Word2019ルールを使用してドキュメントを開きます。 以前のMicrosoft Wordアプリケーションバージョンのいずれかで発生するようにドキュメントのロードを表示する必要がある場合は、LoadOptionsクラスのMswVersionプロパティを使用して目的のバージョンを明示的に指定する必要があります。

次のコード例は、loadオプションを使用してMicrosoft Wordバージョンを設定する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<LoadOptions> loadOptions = System::MakeObject<LoadOptions>();
// Change the loading version to Microsoft Word 2010.
loadOptions->set_MswVersion(MsWordVersion::Word2010);
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Document.docx", loadOptions);
System::String outputPath = outputDataDir + u"Load_Options.SetMSWordVersion.docx";
doc->Save(outputPath);

外観を変更するには、言語環境設定を設定します

Microsoft Word でのドキュメントの表示の詳細は、アプリケーションのバージョンと MswVersion プロパティ値だけでなく、言語設定にも依存します。Microsoft Word では、[ファイル] → [オプション] → [言語] にある [Office 言語設定] ダイアログの設定に応じて、ドキュメントの表示が異なる場合があります。このダイアログを使用して、ユーザーは、たとえば、プライマリ言語、校正言語、表示言語などを選択できます。Aspose.Words では、このダイアログに相当する LanguagePreferences プロパティが提供されます。Aspose.Words の出力が Microsoft Word の出力と異なる場合は、EditingLanguage に適切な値を設定します。これにより、出力ドキュメントが改善されます。

次のコード例は、日本語をEditingLanguageに設定する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Create a new LoadOptions object.
System::SharedPtr<LoadOptions> loadOptions = System::MakeObject<LoadOptions>();
// Set language preferences that will be used when document is loading.
loadOptions->get_LanguagePreferences()->AddEditingLanguage(EditingLanguage::Japanese);
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"languagepreferences.docx", loadOptions);

ドキュメントの読み込み中の問題を制御するにはWarningCallbackを使用します

一部の文書が破損しているか、無効なエントリが含まれているか、現在Aspose.Wordsでサポートされていない機能を持っている可能性があります。 ドキュメントの読み込み中に発生した問題について知りたい場合は、Aspose.WordsはIWarningCallbackインターフェイスを提供します。

次のコード例は、IWarningCallbackインターフェイスの実装を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
class DocumentLoadingWarningCallback : public IWarningCallback
{
public:
System::SharedPtr<WarningInfoCollection> mWarnings;
void Warning(System::SharedPtr<WarningInfo> info) override;
DocumentLoadingWarningCallback();
};
void DocumentLoadingWarningCallback::Warning(System::SharedPtr<WarningInfo> info)
{
// Prints warnings and their details as they arise during document loading.
std::cout << "WARNING: {info->get_WarningType} " << std::endl;
std::cout << "Source: {info->get_Source} " << std::endl;
std::cout << "\tDescription: {info->get_Description} " << std::endl;
}
DocumentLoadingWarningCallback::DocumentLoadingWarningCallback() : mWarnings(System::MakeObject<WarningInfoCollection>())
{
}

読み込み時間中のすべての問題に関する情報を取得するには、WarningCallbackプロパティを使用します。

次のコード例は、このプロパティを使用する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Create a new LoadOptions object and set its WarningCallback property.
System::SharedPtr<LoadOptions> loadOptions = System::MakeObject<LoadOptions>();
System::SharedPtr<DocumentLoadingWarningCallback> callback = System::MakeObject<DocumentLoadingWarningCallback>();
loadOptions->set_WarningCallback(callback);
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Document.docx", loadOptions);

ResourceLoadingCallbackを使用して外部リソースの読み込みを制御します

ドキュメントには、ローカルディスク、ネットワーク、またはインターネット上のどこかにあるイメージへの外部リンクが含まれている場合があります。 Aspose.Wordsはこのような画像を自動的に文書に読み込みますが、このプロセスを制御する必要がある状況があります。 たとえば、特定の画像を本当にロードする必要があるのか、それともスキップする必要があるのかを判断します。 ResourceLoadingCallbackloadオプションを使用すると、これを制御できます。

次のコード例は、IResourceLoadingCallbackインターフェイスの実装を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
class HtmlLinkedResourceLoadingCallback : public IResourceLoadingCallback
{
public:
ResourceLoadingAction ResourceLoading(System::SharedPtr<ResourceLoadingArgs> args) override;
HtmlLinkedResourceLoadingCallback();
};
HtmlLinkedResourceLoadingCallback::HtmlLinkedResourceLoadingCallback()
{
}
ResourceLoadingAction HtmlLinkedResourceLoadingCallback::ResourceLoading(System::SharedPtr<ResourceLoadingArgs> args)
{
switch (args->get_ResourceType())
{
case ResourceType::CssStyleSheet:
{
std::cout << "External CSS Stylesheet found upon loading: " << args->get_OriginalUri().ToUtf8String() << std::endl;
// CSS file will don't used in the document
return ResourceLoadingAction::Skip;
}
case ResourceType::Image:
{
// Replaces all images with a substitute
System::String newImageFilename = u"Logo.jpg";
std::cout << "\tImage will be substituted with: " << newImageFilename.ToUtf8String() << std::endl;
System::SharedPtr<System::Drawing::Image> newImage = System::Drawing::Image::FromFile(GetInputDataDir_LoadingAndSaving() + newImageFilename);
System::SharedPtr<System::Drawing::ImageConverter> converter = System::MakeObject<System::Drawing::ImageConverter>();
auto imageBytes = System::DynamicCast<System::Array<uint8_t>>(converter->ConvertTo(nullptr, nullptr, newImage, System::ObjectExt::GetType<System::Array<uint8_t>>()));
//System::ArrayPtr<uint8_t> imageBytes = System::IO::File::ReadAllBytes(GetInputDataDir_LoadingAndSaving() + newImageFilename);
args->SetData(imageBytes);
// New images will be used instead of presented in the document
return ResourceLoadingAction::UserProvided;
}
case ResourceType::Document:
{
std::cout << "External document found upon loading: " << args->get_OriginalUri().ToUtf8String() << std::endl;
// Will be used as usual
return ResourceLoadingAction::Default;
}
default:
throw System::InvalidOperationException(u"Unexpected ResourceType value.");
}
}

次のコード例は、ResourceLoadingCallbackプロパティを使用する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Create a new LoadOptions object and set its ResourceLoadingCallback attribute as an instance of our IResourceLoadingCallback implementation
System::SharedPtr<LoadOptions> loadOptions = System::MakeObject<LoadOptions>();
loadOptions->set_ResourceLoadingCallback(System::MakeObject<HtmlLinkedResourceLoadingCallback>());
// When we open an Html document, external resources such as references to CSS stylesheet files and external images
// will be handled in a custom manner by the loading callback as the document is loaded
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Images.html", loadOptions);
doc->Save(outputDataDir + u"Load_Options.LoadOptionsResourceLoadingCallback.pdf");

メモリ例外を回避するにはTempFolderを使用します

Aspose.Wordsは、リッチコンテンツでいっぱいの数千ページの非常に大きな文書をサポートします。 負荷などの書類が必要かに違います。 読み込みの過程で、Aspose.Wordsは文書を解析するために使用される一時構造体を保持するためにさらに多くのメモリを必要とします。

ドキュメントの読み込み中にメモリ不足の例外に問題がある場合は、TempFolderプロパティを使用してみてください。 この場合、Aspose.Wordsは一部のデータをメモリではなく一時ファイルに保存し、このような例外を回避するのに役立ちます。

次のコード例は、TempFolderを設定する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<LoadOptions> loadOptions = System::MakeObject<LoadOptions>();
loadOptions->set_TempFolder(u"C:/TempFolder/");
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Document.docx", loadOptions);

エンコーディングを明示的に設定する

最新のドキュメント形式のほとんどは、コンテンツをUnicodeで保存し、特別な処理を必要としません。 一方、Unicode以前のエンコーディングを使用しており、エンコーディング情報を見逃したり、エンコーディング情報を本質的にサポートしていないドキュメント Aspose.Wordsはデフォルトで適切なエンコーディングを自動的に検出しようとしますが、まれにエンコーディング認識アルゴリズムで検出されたエンコーディングとは異なるエンコーディングを使用する必要がある場合があります。 この場合、Encodingプロパティを使用してエンコーディングを取得または設定します。

次のコード例は、自動的に選択されたエンコーディングを上書きするようにエンコーディングを設定する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Set the Encoding attribute in a LoadOptions object to override the automatically chosen encoding with the one we know to be correct
System::SharedPtr<LoadOptions> loadOptions = System::MakeObject<LoadOptions>();
loadOptions->set_Encoding(System::Text::Encoding::get_UTF7());
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Encoded in UTF-7.txt", loadOptions);

暗号化された文書を読み込む

パスワードで暗号化されたWord文書を読み込むことができます。 これを行うには、LoadOptionsオブジェクトを受け入れる特別なコンストラクタオーバーロードを使用します。 このオブジェクトには、パスワード文字列を指定するPasswordプロパティが含まれています。

次のコード例は、パスワードで暗号化されたドキュメントを読み込む方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directory.
System::String dataDir = GetInputDataDir_LoadingAndSaving();
// Loads encrypted document.
System::SharedPtr<Document> doc = System::MakeObject<Document>(dataDir + u"LoadEncrypted.docx", System::MakeObject<LoadOptions>(u"aspose"));

ファイルが暗号化されているかどうかが事前にわからない場合は、FileFormatUtilクラスを使用して、ファイル形式を検出したり、ファイル拡張子をファイル形式の列挙に変換したりするなど、ファイル形式を操作するためのユーティリティメソッドを提供します。 文書が暗号化されていて、それを開くためにパスワードが必要かどうかを検出するには、IsEncryptedプロパティを使用します。

次のコード例は、OpenDocumentが暗号化されているかどうかを確認する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<FileFormatInfo> info = FileFormatUtil::DetectFileFormat(inputDataDir + u"encrypted.odt");
std::cout << info->get_IsEncrypted() << std::endl;