在 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);

設定頁面顯示選項

若您想要設定每行文字數,請使用 CharactersPerLine 屬性。 您也可以為 Word 文檔設定每頁的行數 - 使用 LinesPerPage屬性來取得或設定文檔格子中的每頁行數。

接下來示範如何為 Microsoft Word 文檔設定每行字數與每頁字數:

// 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.");

為特定的字體版本優化文件

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);