How to write protection options in DOCX document using C# and .NET

  1. Add SautinSoft.Document from Nuget.
  2. Load/Create a DOCX document.
  3. Specify a password in WriteProtection property.
  4. Save the document as DOCX.

    A Word document (.docx file) with write protection is opened in Word applications with a read only mode. The document's content can be edited, but it cannot be re-saved with the same file name.

In this code example, we create a document and save it with write protection options.

Complete code

using System;
using SautinSoft.Document;
using SautinSoft.Document.Drawing;
using System.IO;
using System.Linq;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get your free 30-day key here:   
            // https://sautinsoft.com/start-for-free/

            WriteProtection();
        }

        /// <summary>
        /// Create a write protected DOCX document.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/write-protection-options-net-csharp-vb.php
        /// </remarks>
        public static void WriteProtection()
        {
            string filePath = @"ProtectedDocument.docx";

            DocumentCore dc = new DocumentCore();

            // Insert paragraphs into the document.
            dc.Sections.Add(
           new Section(dc,
               new Paragraph(dc, "This document has been opened in read only mode."),
               new Paragraph(dc, "To keep your changes, you 'll need to save the document with a new name or in a different location."),
               new Paragraph(dc, "To make changes to the current document, restart with the password '12345'.")));

            // Sets the write protection password "12345".
            DocumentWriteProtection protection = dc.WriteProtection;
            protection.SetPassword("12345");

            // Save a document as the DOCX file with write protection options.
            dc.Save(filePath);

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(filePath) { UseShellExecute = true });
        }
    }
}

Download

Imports System
Imports SautinSoft.Document
Imports SautinSoft.Document.Drawing
Imports System.IO
Imports System.Linq

Namespace Example
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			WriteProtection()
		End Sub
                ''' Get your free 30-day key here:   
                ''' https://sautinsoft.com/start-for-free/
		''' <summary>
		''' Create a write protected DOCX document.
		''' </summary>
		''' <remarks>
		''' Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/write-protection-options-net-csharp-vb.php
		''' </remarks>
		Public Shared Sub WriteProtection()
			Dim filePath As String = "ProtectedDocument.docx"

			Dim dc As New DocumentCore()

			' Insert paragraphs into the document.
			dc.Sections.Add(New Section(dc, New Paragraph(dc, "This document has been opened in read only mode."),
										New Paragraph(dc, "To keep your changes, you 'll need to save the document with a new name or in a different location."),
										New Paragraph(dc, "To make changes to the current document, restart with the password '12345'.")))

			' Sets the write protection password "12345".
			Dim protection As DocumentWriteProtection = dc.WriteProtection
			protection.SetPassword("12345")

			' Save a document as the DOCX file with write protection options.
			dc.Save(filePath)

			' Open the result for demonstration purposes.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(filePath) With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.