ロードオプションの指定
ドキュメントをロードするときに、いくつかの詳細プロパティを設定できます。 Aspose.Words は、ロード プロセスをより正確に制御できる LoadOptions クラスを提供します。一部のロード形式には、このロード形式のロード オプションを保持する対応するクラスがあります。たとえば、PDF 形式にロードする場合は PdfLoadOptions、TXT にロードする場合は TxtLoadOptions があります。この記事では、LoadOptions クラスのオプションを使用する例を示します。
Microsoft Word バージョンを設定して外観を変更する
Microsoft Word アプリケーションのバージョンが異なると、ドキュメントの表示方法も異なります。たとえば、WPS Office を使用して作成された DOCX や DOTX などの OOXML ドキュメントにはよく知られた問題があります。このような場合、重要なドキュメント マークアップ要素が欠落しているか、異なる解釈が行われる可能性があり、その結果、Microsoft Word 2019 ではそのようなドキュメントが Microsoft Word 2010 と比べて異なって表示されます。
デフォルトでは、Aspose.Words は Microsoft Word 2019 ルールを使用してドキュメントを開きます。ドキュメントの読み込みを以前の Microsoft Word アプリケーション バージョンのいずれかで行われたように表示する必要がある場合は、LoadOptions クラスの MswVersion プロパティを使用して目的のバージョンを明示的に指定する必要があります。
次のコード例は、ロード オプションを使用して Microsoft Word バージョンを設定する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// Create a new LoadOptions object, which will load documents according to MS Word 2019 specification by default | |
// and change the loading version to Microsoft Word 2010. | |
LoadOptions loadOptions = new LoadOptions { MswVersion = MsWordVersion.Word2010 }; | |
Document doc = new Document(MyDir + "Document.docx", loadOptions); | |
doc.Save(ArtifactsDir + "WorkingWithLoadOptions.SetMsWordVersion.docx"); |
言語設定を行って外観を変更する
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-.NET | |
LoadOptions loadOptions = new LoadOptions(); | |
// Set language preferences that will be used when document is loading. | |
loadOptions.LanguagePreferences.AddEditingLanguage(EditingLanguage.Japanese); | |
Document doc = new Document(MyDir + "No default editing language.docx", loadOptions); | |
int localeIdFarEast = doc.Styles.DefaultFont.LocaleIdFarEast; | |
Console.WriteLine( | |
localeIdFarEast == (int)EditingLanguage.Japanese | |
? "The document either has no any FarEast language set in defaults or it was set to Japanese originally." | |
: "The document default FarEast language was set to another than Japanese language originally, so it is not overridden."); |
WarningCallback を使用してドキュメントのロード時の問題を制御する
一部のドキュメントは破損しているか、無効なエントリが含まれているか、Aspose.Words で現在サポートされていない機能を含んでいる可能性があります。ドキュメントのロード中に発生した問題について知りたい場合は、Aspose.Words が IWarningCallback インターフェイスを提供します。
次のコード例は、IWarningCallback インターフェイスの実装を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
public class DocumentLoadingWarningCallback : IWarningCallback | |
{ | |
public void Warning(WarningInfo info) | |
{ | |
// Prints warnings and their details as they arise during document loading. | |
Console.WriteLine($"WARNING: {info.WarningType}, source: {info.Source}"); | |
Console.WriteLine($"\tDescription: {info.Description}"); | |
} | |
} |
ロード時間中のすべての問題に関する情報を取得するには、WarningCallback プロパティを使用します。
次のコード例は、このプロパティの使用方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
LoadOptions loadOptions = new LoadOptions { WarningCallback = new DocumentLoadingWarningCallback() }; | |
Document doc = new Document(MyDir + "Document.docx", loadOptions); |
ResourceLoadingCallback を使用して外部リソースの読み込みを制御する
ドキュメントには、ローカル ディスク、ネットワーク、またはインターネット上のどこかにある画像への外部リンクが含まれている場合があります。 Aspose.Words はそのような画像をドキュメントに自動的に読み込みますが、このプロセスを制御する必要がある場合があります。たとえば、特定の画像を本当にロードする必要があるか、それともスキップする必要があるかを判断するためです。 ResourceLoadingCallback ロード オプションを使用すると、これを制御できます。
次のコード例は、IResourceLoadingCallback インターフェイスの実装を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
private class HtmlLinkedResourceLoadingCallback : IResourceLoadingCallback | |
{ | |
public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args) | |
{ | |
switch (args.ResourceType) | |
{ | |
case ResourceType.CssStyleSheet: | |
{ | |
Console.WriteLine($"External CSS Stylesheet found upon loading: {args.OriginalUri}"); | |
// CSS file will don't used in the document. | |
return ResourceLoadingAction.Skip; | |
} | |
case ResourceType.Image: | |
{ | |
// Replaces all images with a substitute. | |
Image newImage = Image.FromFile(ImagesDir + "Logo.jpg"); | |
ImageConverter converter = new ImageConverter(); | |
byte[] imageBytes = (byte[])converter.ConvertTo(newImage, typeof(byte[])); | |
args.SetData(imageBytes); | |
// New images will be used instead of presented in the document. | |
return ResourceLoadingAction.UserProvided; | |
} | |
case ResourceType.Document: | |
{ | |
Console.WriteLine($"External document found upon loading: {args.OriginalUri}"); | |
// Will be used as usual. | |
return ResourceLoadingAction.Default; | |
} | |
default: | |
throw new InvalidOperationException("Unexpected ResourceType value."); | |
} | |
} | |
} |
次のコード例は、ResourceLoadingCallback プロパティの使用方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
LoadOptions loadOptions = new LoadOptions { ResourceLoadingCallback = new HtmlLinkedResourceLoadingCallback() }; | |
// When we open an Html document, external resources such as references to CSS stylesheet files | |
// and external images will be handled customarily by the loading callback as the document is loaded. | |
Document doc = new Document(MyDir + "Images.html", loadOptions); | |
doc.Save(ArtifactsDir + "WorkingWithLoadOptions.ResourceLoadingCallback.pdf"); |
TempFolder を使用してメモリ例外を回避する
Aspose.Words は、豊富なコンテンツが詰まった何千ページもの非常に大きなドキュメントをサポートします。このようなドキュメントをロードするには、大量の RAM が必要になる場合があります。読み込みの過程で、Aspose.Words はドキュメントの解析に使用される一時構造を保持するためにさらに多くのメモリを必要とします。
ドキュメントの読み込み中にメモリ不足例外が発生する場合は、TempFolder プロパティを使用してみてください。この場合、Aspose.Words は一部のデータをメモリではなく一時ファイルに保存するため、このような例外を回避できます。
次のコード例は、TempFolder を設定する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
LoadOptions loadOptions = new LoadOptions { TempFolder = ArtifactsDir }; | |
Document doc = new Document(MyDir + "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-.NET | |
LoadOptions loadOptions = new LoadOptions { Encoding = Encoding.UTF7 }; | |
Document doc = new Document(MyDir + "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-.NET | |
Document doc = new Document(MyDir + "Encrypted.docx", new LoadOptions("docPassword")); |
ファイルが暗号化されているかどうかが事前にわからない場合は、FileFormatUtil クラスを使用できます。このクラスは、ファイル形式の検出やファイル拡張子とファイル形式の列挙間の変換など、ファイル形式を操作するためのユーティリティ メソッドを提供します。ドキュメントが暗号化されており、開くためにパスワードが必要かどうかを検出するには、IsEncrypted プロパティを使用します。
次のコード例は、OpenDocument が暗号化されているかどうかを確認する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
FileFormatInfo info = FileFormatUtil.DetectFileFormat(MyDir + "Encrypted.odt"); | |
Console.WriteLine(info.IsEncrypted); |