Word 文書のオプションと外観を操作する

場合によっては、言語設定やページごとの行数の設定など、ドキュメントの外観を変更する必要がある場合があります。Aspose.Words には、ドキュメントの表示方法を制御する機能といくつかの追加オプションが用意されています。この記事ではそのような可能性について説明します。

ドキュメント表示オプションの設定

ViewOptions クラスを使用して、Microsoft Word でのドキュメントの表示方法を制御できます。たとえば、zoom_percent プロパティを使用してドキュメントのズーム値を設定したり、view_type プロパティを使用して表示モードを設定したりできます。

次のコード例は、Microsoft Word でドキュメントを開いたときにドキュメントが 50% で表示されるようにする方法を示しています。

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Document.docx")
doc.view_options.view_type = aw.settings.ViewType.PAGE_LAYOUT
doc.view_options.zoom_percent = 50
doc.save(docs_base.artifacts_dir + "WorkingWithDocumentOptionsAndSettings.view_options.docx")

ページ表示オプションの設定

1 行あたりの文字数を設定する場合は、characters_per_line プロパティを使用します。 Word 文書のページごとの行数を設定することもできます。lines_per_page プロパティを使用して、文書グリッドのページごとの行数を取得または設定します。

次のコード例は、Microsoft Word ドキュメントの 1 行あたりの文字数と 1 ページあたりの行数を設定する方法を示しています。

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Document.docx")
# 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.first_section.page_setup.layout_mode = aw.SectionLayoutMode.GRID
doc.first_section.page_setup.characters_per_line = 30
doc.first_section.page_setup.lines_per_page = 10
doc.save(docs_base.artifacts_dir + "WorkingWithDocumentOptionsAndSettings.document_page_setup.docx")

言語設定を行う

Microsoft Word でのドキュメントの表示は、このドキュメントのデフォルトとして設定されている言語によって異なります。デフォルトで言語が設定されていない場合、Microsoft Word は [Office の言語設定] ダイアログ ボックスから情報を取得します。このダイアログ ボックスは、たとえば、Microsoft Word 2019 の [ファイル → オプション → 言語] にあります。

Aspose.Words では、LanguagePreferences クラスを使用して言語設定をセットアップすることもできます。また、ドキュメントを正しく表示するには、ドキュメントの読み込みプロセスに一致する Microsoft Word バージョンを設定する必要があることにも注意してください。これは、msw_version プロパティを使用して行うことができます。

次のコード例は、編集言語に日本語を追加する方法を示しています。

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
loadOptions = aw.loading.LoadOptions()
# Set language preferences that will be used when document is loading.
loadOptions.language_preferences.add_editing_language(aw.loading.EditingLanguage.JAPANESE)

次のコード例は、ロシア語をデフォルトの編集言語として設定する方法を示しています。

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
loadOptions = aw.loading.LoadOptions()
loadOptions.language_preferences.default_editing_language = aw.loading.EditingLanguage.RUSSIAN
doc = aw.Document(docs_base.my_dir + "No default editing language.docx", loadOptions)
localeId = doc.styles.default_font.locale_id
print("The document either has no any language set in defaults or it was set to Russian originally." if (localeId == aw.loading.EditingLanguage.RUSSIAN)
else "The document default language was set to another than Russian language originally, so it is not overridden.")

特定の Word バージョンに合わせて文書を最適化する

optimize_for メソッドを使用すると、ドキュメントのコンテンツだけでなく、特定のバージョンの Microsoft Word のデフォルトの Aspose.Words 動作も最適化できます。このメソッドを使用すると、Microsoft Word がドキュメントの読み込み時に「互換モード」リボンを表示しないようにすることができます。 compliance プロパティを ISO29500_2008_TRANSITIONAL 以上に設定する必要がある場合もあることに注意してください。

次のコード例は、Microsoft Word 2016 用にドキュメント コンテンツを最適化する方法を示しています。

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Document.docx")
doc.compatibility_options.optimize_for(aw.settings.MsWordVersion.WORD2016)
doc.save(docs_base.artifacts_dir + "WorkingWithDocumentOptionsAndSettings.optimize_for_ms_word.docx")