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 100-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 });
}
}
}
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 100-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
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: