Word processing control Aspose.Words function demonstration: use C# to programmatically protect or unprotect Word documents

Aspose.Words  is a high-level Word document processing API for performing various document management and manipulation tasks. The API supports generating, modifying, converting, rendering and printing documents without using Microsoft Word directly in the cross-platform application. also,

Aspose API supports popular file format processing and allows exporting or converting various types of documents to fixed layout file formats and most commonly used image/multimedia formats.

Aspose.words latest download (qun: 761297826) icon-default.png?t=N2N8https://www.evget.com/product/4116/download

Microsoft Word supports various protection functions to protect the whole Word document or some parts of the document. Document protection comes into play when you need to share documents with other people or groups. In such cases, you may wish to limit the third party's access or permissions. On the other hand, you can also specify a password to avoid unauthorized access to the document. This article also contains some simple ways to programmatically protect Word documents. Also, you'll learn how to unprotect Word documents without a password.

.NET API for protecting/unprotecting Word documents

Aspose.Words for .NET is an API for programmatically manipulating Word documents in .NET applications. Among other document manipulation functions, the API provides simple yet powerful functions to protect and unprotect Word documents. You can download Aspose.Words for .NET DLL or install it into your project in Visual Studio using:

NuGet package manager

package manager console

PM> Install-Package Aspose.Words

Protect Word Documents Using C#

Aspose.Words for .NET provides the following protection types to protect Word documents:

  • AllowOnlyComments  - Only allow comments to be modified.
  • AllowOnlyFormFields  - Allows only data entry form fields.
  • AllowOnlyRevisions  - Allows only revision markers to be added.
  • ReadOnly  - completely read-only (does not allow any changes to the document).
  • NoProtection  - No protection at all.

Here are the steps to apply protection to a Word document:

  • Create an instance of the Document class and initialize it with the path to the Word document.
  • Call the Document.Protect(ProtectionType) method by providing the desired ProtectionType value.
  • Call the Document.Save(String) method to save the protected Word document.

The following code sample shows how to protect a Word document in C#.

// Load Word document
Document doc = new Document("Document.docx");
// Protect with a protection type
doc.Protect(ProtectionType.AllowOnlyComments);
// Save the document
doc.Save("Protected Document.docx");

Password Protect Word Documents in C#

Here are the steps to password protect a Word document:

  • Create an instance of the Document class and initialize it with the path to the Word document.
  • Call the Document.Protect(ProtectionType, String) method by providing a ProtectionType value and a password.
  • Call the Document.Save(String) method to save the protected Word document.

The following code sample shows how to password protect a Word document in C#.

// Load Word document
Document doc = new Document("Document.docx");
string password = "******";
// Protect with a protection type and password
doc.Protect(ProtectionType.ReadOnly, password);
// Save the document
doc.Save("Protected Document.docx");

Unprotect a Word document without a password in C#

Aspose.Words for .NET can unprotect Word documents even if you don't have a password. Here are the steps to unprotect a Word document:

  • Create an instance of the Document class and initialize it with the path to the Word document.
  • Call the Document.Unprotect() method.
  • Call the Document.Save(String) method to save the unprotected Word document.

The following code sample shows how to unprotect a Word document without a password in C#.

// Load Word document
Document doc = new Document("Protected Document.docx");
// Unprotect Word document
doc.Unprotect();
// Save the document
doc.Save("Unprotected Document.docx");

The above is how to use C# to programmatically protect or unprotect Word documents. If you have other questions about the product, welcome to consult us, or join our official technical exchange group.

Guess you like

Origin blog.csdn.net/m0_67129275/article/details/130056520