DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Usman Aziz
Usman Aziz

Posted on • Originally published at blog.groupdocs.com

Prevent watermark tampering in Word Documents using C#

Watermarking is a famous way of adding labels to the documents that may indicate the state of the document such as draft, confidential, etc. It can be used to add a company’s logo behind the text of the document to avoid the ownership dispute. In some cases, people add watermarks to the documents to protect the content. But, are you sure that the content is protected and no one can remove those watermarks? There is no guarantee because various third-party tools are available that can wipe out the watermarks from the documents.

In such scenarios, you need some way to lock the watermarks to restrict the editing. In this article, I’ll show you how toΒ protect your Word documents and prevent the editing of the watermarks programmatically inΒ C#Β using GroupDocs.Watermark for .NETΒ which is a document watermarking API.

Locking Watermark in Word Document

The above-mentioned API providesΒ 5Β variantsΒ of locking Word document when adding watermark.

Allowing Revisions Only

In this case, the user will only be able to add revision marks to the Word document. The content of the document as well as the watermark will be read-only.

Allowing Comments Only

If this type of restriction is applied then the user will only be able to modify the comments in the document.

Allowing Form Fields Only

With this option, the document is split into one-page sections and a locked section with the watermark is added between each two adjacent document sections.

Read Only with Editable Content

In this case, the document is marked as read-only but all the content except the watermark is marked as editable.

Completely Read Only

In this case, the entire document is marked as read-only and nothing can be edited.

Source Code

The following code sample shows how to impose the above-mentioned restrictions on a Word document when adding the watermark.

WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();
using (Watermarker watermarker = new Watermarker("document.docx", loadOptions))
{
    TextWatermark watermark = new TextWatermark("Watermark text", new Font("Arial", 19));
    watermark.ForegroundColor = Color.Red;

    WordProcessingWatermarkPagesOptions options = new WordProcessingWatermarkPagesOptions();
    options.IsLocked = true;
    options.LockType = WordProcessingLockType.AllowOnlyComments;
    /* 
    options.LockType = WordProcessingLockType.AllowOnlyRevisions;
    options.LockType = WordProcessingLockType.AllowOnlyFormFields;
    options.LockType = WordProcessingLockType.ReadOnlyWithEditableContent;
    options.LockType = WordProcessingLockType.ReadOnly; 
    */

    // To protect with password
    //options.Password = "7654321";

    watermarker.Add(watermark, options);
    watermarker.Save("document_watermarked.docx");
}
Enter fullscreen mode Exit fullscreen mode

Cheers!

Top comments (1)

Meeting a new developer

Stop by this week's meme thread!