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-C | |
// The path to the documents directories. | |
System::String inputDataDir = GetInputDataDir_WorkingWithDocument(); | |
System::String outputDataDir = GetOutputDataDir_WorkingWithDocument(); | |
// Load the template document. | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile.doc"); | |
// Set view option. | |
doc->get_ViewOptions()->set_ViewType(ViewType::PageLayout); | |
doc->get_ViewOptions()->set_ZoomPercent(50); | |
System::String outputPath = outputDataDir + u"SetViewOption.doc"; | |
// Save the finished document. | |
doc->Save(outputPath); |
ページ表示オプションの設定
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-C | |
// The path to the documents directories. | |
System::String inputDataDir = GetInputDataDir_WorkingWithDocument(); | |
System::String outputDataDir = GetOutputDataDir_WorkingWithDocument(); | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"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->get_FirstSection()->get_PageSetup()->set_LayoutMode(SectionLayoutMode::Grid); | |
//Set the number of characters per line in the document grid. | |
doc->get_FirstSection()->get_PageSetup()->set_CharactersPerLine(30); | |
//Set the number of lines per page in the document grid. | |
doc->get_FirstSection()->get_PageSetup()->set_LinesPerPage(10); | |
System::String outputPath = outputDataDir + u"DocumentPageSetup.doc"; | |
doc->Save(outputPath); |
言語設定の設定
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-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); |
次のコード例は、ロシア語を既定の編集言語として設定する方法を示しています:
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::SharedPtr<LoadOptions> loadOptions = System::MakeObject<LoadOptions>(); | |
loadOptions->get_LanguagePreferences()->set_DefaultEditingLanguage(EditingLanguage::Russian); | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"languagepreferences.docx", loadOptions); | |
int32_t localeId = doc->get_Styles()->get_DefaultFont()->get_LocaleId(); | |
if (localeId == static_cast<int32_t>(EditingLanguage::Russian)) | |
{ | |
std::cout << "The document either has no any language set in defaults or it was set to Russian originally." << std::endl; | |
} | |
else | |
{ | |
std::cout << "The document default language was set to another than Russian language originally, so it is not overridden." << std::endl; | |
} |
特定のWordバージョン用に文書を最適化する
OptimizeForメソッドを使用すると、ドキュメントの内容を最適化することができ、Microsoft Wordの特定のバージョンのデフォルトのAspose.Words動作も可能になります。 このメソッドを使用すると、文書の読み込み時にMicrosoft Wordが"互換モード"リボンを表示しないようにすることができます。 また、Compliance
プロパティをIso29500_2008_Transitional以上に設定する必要がある場合もあります。
次のコード例は、Microsoft Word2016のドキュメントコンテンツを最適化する方法を示しています:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile.docx"); | |
doc->get_CompatibilityOptions()->OptimizeFor(MsWordVersion::Word2016); | |
System::String outputPath = outputDataDir + u"SetCompatibilityOptions.docx"; | |
// Save the document to disk. | |
doc->Save(outputPath); |