How to Work with Document Settings in .NET

How to Work with Document Settings in .NET

This guide shows how to work with application-level document settings in Aspose.Words FOSS for .NET: legacy compatibility switches, hyphenation preferences, the stored mail-merge configuration, view options, and write protection. Aspose.Words FOSS for .NET is not yet published as a NuGet package, so reference it by building from source first.

Step-by-Step Guide

Step 1: Get the Library

Clone the repository and build Aspose.Words.sln in Release configuration, then add a project reference to Aspose.Words.csproj from your own .NET project, as described in the Installation guide. Add using Aspose.Words; and using Aspose.Words.Settings; at the top of any file that reads or writes document settings.


Step 2: Set Legacy Compatibility Options

Document.CompatibilityOptions (a CompatibilityOptions instance) holds dozens of boolean switches that reproduce specific older-Word layout and spacing behaviors — for example GrowAutofit, UseWord2010TableStyleRules, AdjustLineHeightInTable, AlignTablesRowByRow, NoExtraLineSpacing, SuppressSpacingAtTopOfPage, and SpacingInWholePoints. Set the individual properties you need directly; OptimizeFor(MsWordVersion) is present on the class but throws NotImplementedException in this build, so there’s no shortcut that applies a whole version profile at once.


Step 3: Configure Hyphenation Preferences

Document.HyphenationOptions (a HyphenationOptions instance) stores Word’s hyphenation preferences for the document: AutoHyphenation, ConsecutiveHyphenLimit, HyphenateCaps, and HyphenationZone. These properties round-trip the request in the saved file; because this edition has no page-layout engine, the hyphenation breaks themselves are computed by Word when it opens and paginates the file, not by Aspose.Words FOSS.


Step 4: Read and Write Mail-Merge Configuration

Document.MailMergeSettings (a MailMergeSettings instance) stores the mail-merge configuration recorded in the document — MainDocumentType, CheckErrors, Destination, DataSource, DataType, Query, ConnectString, MailSubject, MailAsAttachment, ActiveRecord, and ViewMergedData — plus Clear() and Clone(). For an external data source connected through Word’s Office Data Source Object mechanism, MailMergeSettings.Odso exposes an Odso instance (DataSource, DataSourceType, TableName, ColumnDelimiter, FirstRowContainsColumnNames), whose FieldMapDatas and RecipientDatas collections describe the field mapping and recipient list. Reading and writing this stored configuration is available in this open-source edition; running the merge itself against a live data source is a separate concern outside this page’s scope.


Step 5: Configure Document View Settings

Document.ViewOptions (a ViewOptions instance) controls how Word displays the file once opened: ViewType, ZoomType, ZoomPercent, DisplayBackgroundShape, DoNotDisplayPageBoundaries, and FormsDesign. These properties are stored preferences that only affect Word’s own display of the document — they have no bearing on how Aspose.Words FOSS itself processes it.


Step 6: Apply Write Protection

Document.WriteProtection (a WriteProtection instance) exposes IsWriteProtected and ReadOnlyRecommended, with SetPassword() to establish a protection password and ValidatePassword() to check a candidate password against it. Call SetPassword() before setting IsWriteProtected = true, and use ValidatePassword() rather than storing or comparing the raw password string yourself.

Common Issues and Fixes

CompatibilityOptions.OptimizeFor() throws NotImplementedException. This method isn’t implemented in this build. Set the individual CompatibilityOptions boolean properties you need directly instead.

Changing ViewOptions.ZoomPercent has no visible effect on generated output. ViewOptions is a stored display preference read by Word, not something Aspose.Words FOSS itself applies during processing. No fix needed — this is expected.

WriteProtection.ValidatePassword() unexpectedly returns false. SetPassword() was never called, or a different password was set. Call SetPassword() first to establish the protected password before validating.

Mail-merge configuration is saved but the merge does not run. MailMergeSettings and Odso store the merge configuration only; this open-source edition does not execute the merge itself. Read the stored configuration and drive your own field-population logic against it.

Frequently Asked Questions

Does CompatibilityOptions.OptimizeFor() work?

No — it throws NotImplementedException in this build. Set the individual CompatibilityOptions properties you need directly.

How do I password-protect a document against edits?

Call WriteProtection.SetPassword() on Document.WriteProtection, then set IsWriteProtected to enable protection.

Do HyphenationOptions and ViewOptions change how the document looks when Aspose.Words FOSS processes it?

No. Both are stored preferences that Word itself applies when it opens and displays the file; this edition has no page-layout engine to act on them directly.

Where do I find the mail-merge data-source configuration for an external data source?

MailMergeSettings.Odso — its FieldMapDatas and RecipientDatas collections describe the column mapping and recipient inclusion state.

What’s the difference between Document.CompatibilityOptions and Document.ViewOptions?

CompatibilityOptions switches reproduce older-Word layout and spacing quirks; ViewOptions controls how Word’s own interface displays the file (zoom, formatting marks). Neither is computed by Aspose.Words FOSS itself.

See Also