Word文書のオプションと外観を操作する
場合によっては、言語設定やページあたりの行数の設定など、文書の外観を変更する必要がある場合があります。Aspose.Wordsは、ドキュメントの表示方法を制御する機能と、いくつかの追加オプションを提供します。 この記事では、そのような可能性について説明します。
文書表示オプションの設定
ViewOptionsクラスを使用して、Microsoft Wordで文書をどのように表示するかを制御できます。 たとえば、ZoomPercentプロパティを使用してドキュメントのズーム値を設定したり、ViewTypeプロパティを使用してビューモードを設定したりできます。
次のコード例は、Microsoft Wordで開いたときに文書が50%で表示されるようにする方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "Document.doc"); | |
doc.getViewOptions().setViewType(ViewType.PAGE_LAYOUT); | |
doc.getViewOptions().setZoomPercent(50); | |
doc.save(dataDir + "Document.SetZoom_out.doc"); |
ページ表示オプションの設定
1行あたりの文字数を設定する場合は、CharactersPerLineプロパティを使用します。 Word文書のページあたりの行数を設定することもできます–LinesPerPageプロパティを使用して、文書グリッド内のページあたりの行数を取得または設定します。
次のコード例は、Microsoft Wordドキュメントの1行あたりの文字数と1ページあたりの行数を設定する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
Document doc = new Document(dataDir + "Document.doc"); | |
// Set the layout mode for a section allowing to define the document grid | |
// behavior | |
// Note that the Document Grid tab becomes visible in the Page Setup dialog of | |
// MS Word if any Asian language is defined as editing language. | |
doc.getFirstSection().getPageSetup().setLayoutMode(SectionLayoutMode.GRID); | |
// Set the number of characters per line in the document grid. | |
doc.getFirstSection().getPageSetup().setCharactersPerLine(30); | |
// Set the number of lines per page in the document grid. | |
doc.getFirstSection().getPageSetup().setLinesPerPage(10); | |
// Save the document | |
doc.save(dataDir + "Document.PageSetup_out.doc"); |
言語設定の設定
Microsoft Wordで文書を表示する言語は、この文書のデフォルトとして設定されている言語によって異なります。 既定で言語が設定されていない場合、Microsoft Wordは、2019年Microsoft Wordの[ファイル]→[オプション]→[言語]の下にある[Office言語設定の設定]ダイアログボックスから情報を取得します。
Aspose.Wordsを使用すると、LanguagePreferencesクラスを使用して言語設定を設定することもできます。 また、ドキュメントを正しく表示するには、ドキュメントの読み込みプロセスが一致するMicrosoft Wordバージョンを設定する必要があることに注意してください。MswVersionプロパティを使用してこれを行うことができます。
次のコード例は、編集言語に日本語を追加する方法を示しています:
// 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."); |
次のコード例は、ロシア語を既定の編集言語として設定する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Specify LoadOptions to set Default Editing Language | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.getLanguagePreferences().setDefaultEditingLanguage(EditingLanguage.RUSSIAN); | |
Document doc = new Document(dataDir + "languagepreferences.docx", loadOptions); | |
int localeId = doc.getStyles().getDefaultFont().getLocaleId(); | |
if (localeId == (int) EditingLanguage.RUSSIAN) | |
System.out.println("The document either has no any language set in defaults or it was set to Russian originally."); | |
else | |
System.out.println("The document default language was set to another than Russian language originally, so it is not overridden."); |
特定のWordバージョン用に文書を最適化する
OptimizeForメソッドを使用すると、ドキュメントの内容を最適化することができ、Microsoft Wordの特定のバージョンのデフォルトのAspose.Words動作も可能になります。 このメソッドを使用すると、文書の読み込み時にMicrosoft Wordが"互換モード"リボンを表示しないようにすることができます。 また、Compliance
プロパティをIso29500_2008_Transitional以上に設定する必要があることに注意してください。
次のコード例は、Microsoft Word2016のドキュメントコンテンツを最適化する方法を示しています:
Document doc = new Document(dataDir + "Document.docx");
// Set Word2016 version for document
doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2016);
// Save the document.
doc.save(dataDir + "output.docx");