ロードオプションの指定
ドキュメントを読み込むときに、いくつかの高度なプロパティを設定できます。 Aspose.Wordsは、ロードプロセスのより正確な制御を可能にするLoadOptionsクラスを提供します。 たとえば、PDF形式への読み込みにはPdfLoadOptions、TXT形式への読み込みにはTxtLoadOptionsがあります。 この記事では、LoadOptionsクラスのオプションを使用する例を示します。
外観を変更するにはMicrosoft Wordバージョンを設定します
Microsoft Wordアプリケーションの異なるバージョンでは、文書を異なる方法で表示できます。 たとえば、WPSOfficeを使用して作成された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-Java | |
// Specify load option to specify MS Word version | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.setMswVersion(MsWordVersion.WORD_2003); | |
Document doc = new Document(dataDir + "document.doc", loadOptions); | |
doc.save(dataDir + "Word2003_out.docx"); |
外観を変更するには、言語環境設定を設定します
Microsoft Wordでのドキュメントの表示の詳細は、アプリケーションのバージョンとMswVersionプロパティの値だけでなく、言語設定にも依存します。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-Java | |
// Specify LoadOptions to add Editing Language | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.getLanguagePreferences().addEditingLanguage(EditingLanguage.JAPANESE); | |
Document doc = new Document(dataDir + "languagepreferences.docx", loadOptions); | |
int localeIdFarEast = doc.getStyles().getDefaultFont().getLocaleIdFarEast(); | |
if (localeIdFarEast == (int) EditingLanguage.JAPANESE) | |
System.out.println("The document either has no any FarEast language set in defaults or it was set to Japanese originally."); | |
else | |
System.out.println("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-Java | |
private static class DocumentLoadingWarningCallback implements IWarningCallback { | |
public void warning(WarningInfo info) { | |
// Prints warnings and their details as they arise during document loading. | |
System.out.println("WARNING: " + info.getWarningType() + " source:" + info.getSource()); | |
System.out.println("\tDescription: " + info.getDescription()); | |
} | |
} |
読み込み時間中のすべての問題に関する情報を取得するには、WarningCallbackプロパティを使用します。
次のコード例は、このプロパティを使用する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Create a new LoadOptions object and set its WarningCallback property. | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.setWarningCallback(new DocumentLoadingWarningCallback()); | |
Document doc = new Document(dataDir + "input.docx", loadOptions); |
ResourceLoadingCallbackを使用して外部リソースの読み込みを制御します
ドキュメントには、ローカルディスク、ネットワーク、またはインターネット上のどこかにあるイメージへの外部リンクが含まれている場合があります。 Aspose.Wordsはこのような画像を自動的に文書に読み込みますが、このプロセスを制御する必要がある状況があります。 たとえば、特定の画像を本当にロードする必要があるのか、それともスキップする必要があるのかを判断します。 ResourceLoadingCallbackloadオプションを使用すると、これを制御できます。
次のコード例は、IResourceLoadingCallbackインターフェイスの実装を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
private static class HtmlLinkedResourceLoadingCallback implements IResourceLoadingCallback { | |
public int resourceLoading(ResourceLoadingArgs args) throws Exception { | |
switch (args.getResourceType()) { | |
case ResourceType.CSS_STYLE_SHEET: { | |
System.out.println("External CSS Stylesheet found upon loading: " + args.getOriginalUri()); | |
// CSS file will don't used in the document | |
return ResourceLoadingAction.SKIP; | |
} | |
case ResourceType.IMAGE: { | |
// Replaces all images with a substitute | |
String newImageFilename = "Logo.jpg"; | |
System.out.println("\tImage will be substituted with: " + newImageFilename); | |
BufferedImage newImage = ImageIO | |
.read(new File(Utils.getDataDir(LoadOptionsCallbacks.class) + newImageFilename)); | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
ImageIO.write(newImage, "jpg", baos); | |
baos.flush(); | |
byte[] imageBytes = baos.toByteArray(); | |
baos.close(); | |
args.setData(imageBytes); | |
// New images will be used instead of presented in the document | |
return ResourceLoadingAction.USER_PROVIDED; | |
} | |
case ResourceType.DOCUMENT: { | |
System.out.println("External document found upon loading: " + args.getOriginalUri()); | |
// Will be used as usual | |
return ResourceLoadingAction.DEFAULT; | |
} | |
default: | |
throw new Exception("Unexpected ResourceType value."); | |
} | |
} | |
} |
次のコード例は、ResourceLoadingCallbackプロパティを使用する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Create a new LoadOptions object and set its ResourceLoadingCallback attribute | |
// as an instance of our IResourceLoadingCallback implementation | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.setResourceLoadingCallback(new 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 | |
Document doc = new Document(dataDir + "Images.html", loadOptions); | |
doc.save(dataDir + "Document.LoadOptionsCallback_out.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-Java | |
// Specify LoadOptions to set Temp Folder | |
LoadOptions lo = new LoadOptions(); | |
lo.setTempFolder("C:\\TempFolder\\"); | |
Document doc = new Document(dataDir + "document.doc", lo); |
エンコーディングを明示的に設定する
最新のドキュメント形式のほとんどは、コンテンツをUnicodeで保存し、特別な処理を必要としません。 一方、Unicode以前のエンコーディングを使用しており、エンコーディング情報を見逃したり、エンコーディング情報を本質的にサポートしていないドキュメント Aspose.Wordsはデフォルトで適切なエンコーディングを自動的に検出しようとしますが、まれにエンコーディング認識アルゴリズムで検出されたエンコーディングとは異なるエンコーディングを使用する必要がある場合があります。 この場合、Encodingプロパティを使用してエンコーディングを取得または設定します。
次のコード例は、自動的に選択されたエンコーディングを上書きするようにエンコーディングを設定する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Set the Encoding attribute in a LoadOptions object to override the | |
// automatically chosen encoding with the one we know to be correct | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.setEncoding(java.nio.charset.Charset.forName("UTF-8")); | |
Document doc = new Document(dataDir + "Encoded in UTF-8.txt", loadOptions); |
暗号化された文書を読み込む
パスワードで暗号化されたWord文書を読み込むことができます。 これを行うには、LoadOptionsオブジェクトを受け入れる特別なコンストラクタオーバーロードを使用します。 このオブジェクトには、パスワード文字列を指定するPasswordプロパティが含まれています。
次のコード例は、パスワードで暗号化されたドキュメントを読み込む方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// For complete examples and data files, please go to | |
// https://github.com/aspose-words/Aspose.Words-for-Java | |
// Load the encrypted document from the absolute path on disk. | |
Document doc = new Document(dataDir + "LoadEncrypted.docx", new LoadOptions("aspose")); |
ファイルが暗号化されているかどうかが事前にわからない場合は、FileFormatUtilクラスを使用して、ファイル形式を検出したり、ファイル拡張子をファイル形式の列挙に変換したりするなど、ファイル形式を操作するためのユーティリティメソッドを提供します。 文書が暗号化されていて、それを開くためにパスワードが必要かどうかを検出するには、IsEncryptedプロパティを使用します。
次のコード例は、OpenDocumentが暗号化されているかどうかを確認する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
FileFormatInfo info = FileFormatUtil.detectFileFormat(dataDir + "encrypted.odt"); | |
System.out.println(info.isEncrypted()); |