Work with Options and Appearance of Word Documents
Sometimes you may need to change the appearance of a document, for example, set language preferences or the number of lines per page.Aspose.Words provides the ability to control how the document will be displayed, as well as some additional options. This article describes such possibilities.
Set Document Display Options
You can control how a document will be displayed in Microsoft Word using the ViewOptions class. For example, you can set a document zoom value using the ZoomPercent property, or the view mode using the ViewType property.
The following code example shows how to ensure that a document is displayed at 50% when opened in Microsoft Word:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "Document.doc"); | |
doc.getViewOptions().setViewType(ViewType.PAGE_LAYOUT); | |
doc.getViewOptions().setZoomPercent(50); | |
doc.save(dataDir + "Document.SetZoom_out.doc"); |
Set Page Display Options
If you want to set the number of characters per line, use the CharactersPerLine property. You can also set the number of lines per page for a Word document – use the LinesPerPage property to get or set the number of lines per page in the document grid.
The following code example shows how to set the number of characters per line and the number of lines per page for a Microsoft Word document:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
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.getFirstSection().getPageSetup().setLayoutMode(SectionLayoutMode.GRID); | |
// Set the number of characters per line in the document grid. | |
doc.getFirstSection().getPageSetup().setCharactersPerLine(30); | |
// Set the number of lines per page in the document grid. | |
doc.getFirstSection().getPageSetup().setLinesPerPage(10); | |
// Save the document | |
doc.save(dataDir + "Document.PageSetup_out.doc"); |
Set Language Preferences
Displaying a document in Microsoft Word depends on which languages are set as defaults for this document. If no languages are set in defaults, Microsoft Word takes information from the “Set Office Language Preferences” dialog box, which, for example, can be found under “File → Options → Language” in Microsoft Word 2019.
With Aspose.Words, you can also set up language preferences using the LanguagePreferences class. Also note that for the correct display of your document it is necessary to set the Microsoft Word version that the document loading process should match – this can be done using the MswVersion property.
The following code example shows how to add Japanese to editing languages:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Specify LoadOptions to add Editing Language | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.getLanguagePreferences().addEditingLanguage(EditingLanguage.JAPANESE); | |
Document doc = new Document(dataDir + "languagepreferences.docx", loadOptions); | |
int localeIdFarEast = doc.getStyles().getDefaultFont().getLocaleIdFarEast(); | |
if (localeIdFarEast == (int) EditingLanguage.JAPANESE) | |
System.out.println("The document either has no any FarEast language set in defaults or it was set to Japanese originally."); | |
else | |
System.out.println("The document default FarEast language was set to another than Japanese language originally, so it is not overridden."); |
The following code example shows how to set Russian as the default editing language:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Specify LoadOptions to set Default Editing Language | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.getLanguagePreferences().setDefaultEditingLanguage(EditingLanguage.RUSSIAN); | |
Document doc = new Document(dataDir + "languagepreferences.docx", loadOptions); | |
int localeId = doc.getStyles().getDefaultFont().getLocaleId(); | |
if (localeId == (int) EditingLanguage.RUSSIAN) | |
System.out.println("The document either has no any language set in defaults or it was set to Russian originally."); | |
else | |
System.out.println("The document default language was set to another than Russian language originally, so it is not overridden."); |
Optimize a Document for a Particular Word Version
The OptimizeFor method allows optimizing document content, as well as default Aspose.Words behaviour for a particular version of Microsoft Word. You can use this method to prevent Microsoft Word from displaying the “Compatibility mode” ribbon upon document loading. Note that you may also need to set the Compliance
property to Iso29500_2008_Transitional or higher.
The following code example shows how to optimize document content for Microsoft Word 2016:
Document doc = new Document(dataDir + "Document.docx");
// Set Word2016 version for document
doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2016);
// Save the document.
doc.save(dataDir + "output.docx");