Pracuj z opcjami i wyglądem dokumentów programu Word

Czasami może zaistnieć potrzeba zmiany wyglądu dokumentu, na przykład ustawienia preferencji językowych lub liczby wierszy na stronie. Aspose.Words zapewnia możliwość kontrolowania sposobu wyświetlania dokumentu, a także kilka dodatkowych opcji. W artykule opisano takie możliwości.

Ustaw opcje wyświetlania dokumentu

Możesz kontrolować sposób wyświetlania dokumentu w Microsoft Word za pomocą klasy ViewOptions. Na przykład można ustawić wartość powiększenia dokumentu za pomocą właściwości ZoomPercent lub tryb widoku za pomocą właściwości ViewType.

Poniższy przykład kodu pokazuje, jak zapewnić wyświetlanie dokumentu w 50% po otwarciu w 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();
// 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);

Ustaw opcje wyświetlania strony

Jeśli chcesz ustawić liczbę znaków w linii, użyj właściwości CharactersPerLine. Możesz także ustawić liczbę wierszy na stronę w dokumencie programu Word - użyj właściwości LinesPerPage, aby uzyskać lub ustawić liczbę wierszy na stronę w siatce dokumentu.

Poniższy przykład kodu pokazuje, jak ustawić liczbę znaków w wierszu i liczbę wierszy na stronę dla dokumentu 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);

Ustaw preferencje językowe

Wyświetlanie dokumentu w formacie Microsoft Word zależy od tego, jakie języki są ustawione jako domyślne dla tego dokumentu. Jeśli domyślnie nie ustawiono żadnego języka, Microsoft Word pobiera informacje z okna dialogowego “Ustaw preferencje językowe pakietu Office”, które można znaleźć na przykład w “Plik → Opcje → Język” w Microsoft Word 2019.

Dzięki Aspose.Words możesz także skonfigurować preferencje językowe za pomocą klasy LanguagePreferences. Pamiętaj też, że do poprawnego wyświetlenia Twojego dokumentu konieczne jest ustawienie wersji Microsoft Word, z którą ma się zgadzać proces ładowania dokumentu – można to zrobić za pomocą właściwości MswVersion.

Poniższy przykład kodu pokazuje, jak dodać język japoński do języków edycji:

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

Poniższy przykład kodu pokazuje, jak ustawić rosyjski jako domyślny język edycji:

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

Zoptymalizuj dokument pod kątem określonej wersji programu Word

Metoda OptimizeFor umożliwia optymalizację zawartości dokumentu, a także domyślnego zachowania Aspose.Words dla konkretnej wersji Microsoft Word. Możesz użyć tej metody, aby zapobiec wyświetlaniu wstążki “Tryb zgodności” w formacie Microsoft Word podczas ładowania dokumentu. Pamiętaj, że może być konieczne ustawienie właściwości Compliance na Iso29500_2008_Transitional lub wyższą.

Poniższy przykład kodu pokazuje, jak zoptymalizować zawartość dokumentu dla 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);