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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithDocument(); | |
// Load the template document. | |
Document doc = new Document(dataDir + "TestFile.doc"); | |
// Set view option. | |
doc.ViewOptions.ViewType = ViewType.PageLayout; | |
doc.ViewOptions.ZoomPercent = 50; | |
dataDir = dataDir + "TestFile.SetZoom_out.doc"; | |
// Save the finished document. | |
doc.Save(dataDir); |
ページ表示オプションの設定
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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithDocument(); | |
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.FirstSection.PageSetup.LayoutMode = SectionLayoutMode.Grid; | |
//Set the number of characters per line in the document grid. | |
doc.FirstSection.PageSetup.CharactersPerLine = 30; | |
//Set the number of lines per page in the document grid. | |
doc.FirstSection.PageSetup.LinesPerPage = 10; | |
dataDir = dataDir + "Document.PageSetup_out.doc"; | |
doc.Save(dataDir); |
言語設定を行う
Microsoft Word でのドキュメントの表示は、このドキュメントのデフォルトとして設定されている言語によって異なります。デフォルトで言語が設定されていない場合、Microsoft Word は [Office の言語設定] ダイアログ ボックスから情報を取得します。このダイアログ ボックスは、たとえば、Microsoft Word 2019 の [ファイル → オプション → 言語] にあります。
Aspose.Words では、LanguagePreferences クラスを使用して言語設定をセットアップすることもできます。また、ドキュメントを正しく表示するには、ドキュメントの読み込みプロセスに一致する Microsoft Word バージョンを設定する必要があることにも注意してください。これは、MswVersion プロパティを使用して行うことができます。
次のコード例は、編集言語に日本語を追加する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// Create a new LoadOptions object. | |
LoadOptions loadOptions = new LoadOptions(); | |
// Set language preferences that will be used when document is loading. | |
loadOptions.LanguagePreferences.AddEditingLanguage(EditingLanguage.Japanese); | |
Document doc = new Document(dataDir + @"languagepreferences.docx", loadOptions); |
次のコード例は、ロシア語をデフォルトの編集言語として設定する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.LanguagePreferences.DefaultEditingLanguage = EditingLanguage.Russian; | |
Document doc = new Document(dataDir + @"languagepreferences.docx", loadOptions); | |
int localeId = doc.Styles.DefaultFont.LocaleId; | |
if (localeId == (int)EditingLanguage.Russian) | |
Console.WriteLine("The document either has no any language set in defaults or it was set to Russian originally."); | |
else | |
Console.WriteLine("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 Word 2016 用にドキュメント コンテンツを最適化する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(fileName); | |
doc.CompatibilityOptions.OptimizeFor(Settings.MsWordVersion.Word2016); | |
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName); | |
// Save the document to disk. | |
doc.Save(dataDir); |