Adding the ability to edit permission area  in a Word document can help users specify certain sections for others to edit while protecting the rest of the document from accidental modifications. This is particularly useful for scenarios like collaborative documents, document reviews, and comments. On the other hand, removing editable area  functionality allows the document to be restored to a read-only state when specific sections do not need to be edited, ensuring the integrity and security of the document content. This article will explain how to use Spire.Doc for .NET to add or remove editable area  in a Word document within a C# project.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Add Editable Area in a Word Document in C#

The steps to add editable area  in a Word document involve inserting PermissionStart and PermissionEnd objects in the document and setting the document to read-only protection mode to ensure that the content within the specified areas can be edited while the rest remains read-only. Here are the detailed steps:

  • Create a Document object.
  • Load a Word document using the Document.LoadFromFile() method.
  • Access a section of the document through the Document.Sections[index] property.
  • Create a PermissionStart object using PermissionStart permissionStart = new PermissionStart(document, id) to mark the beginning of the editable area .
  • Create a PermissionEnd object using PermissionEnd permissionEnd = new PermissionEnd(document, id) to mark the end of the editable area .
  • Access a paragraph using the Section.Paragraphs[index] property.
  • Insert the permission start object at the beginning of the paragraph using the Paragraph.ChildObjects.Insert(0, permissionStart) method.
  • Add the permission end object at the end of the paragraph using the Paragraph.ChildObjects.Add(permissionEnd) method.
  • Set the document to read-only protection mode and restrict editing permissions using the Document.Protect(ProtectionType.AllowOnlyReading, password) method.
  • Save the resulting document using the Document.SaveToFile() method.
  • C#
using Spire.Doc;
using Spire.Doc.Documents;

namespace SpireDocDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create a new document object
            Document document = new Document();

            // Load the document from the specified path
            document.LoadFromFile("Sample1.docx");

            // Get the first section of the document
            Section section = document.Sections[0];

            // Create a permission start object
            PermissionStart permissionStart = new PermissionStart(document, "restricted1");

            // Create a permission end object
            PermissionEnd permissionEnd = new PermissionEnd(document, "restricted1");

            // Get the second paragraph in the section
            Paragraph paragraph = section.Paragraphs[1];

            // Insert the permission start object at the beginning of the paragraph
            paragraph.ChildObjects.Insert(0, permissionStart);

            // Add the permission end object at the end of the paragraph
            paragraph.ChildObjects.Add(permissionEnd);

            // Set the document to be read-only protected
            document.Protect(ProtectionType.AllowOnlyReading, "123456");

            // Save the modified document to the specified path
            document.SaveToFile("AddedEditingPermissionsArea.docx", FileFormat.Docx);

            // Close the document and release the resources occupied by the document object
            document.Close();
            document.Dispose();
        }
  }
}

C#: Add or Remove Editable area in a Word Document

Remove Editable Area in a Word Document in C#

The key steps to remove editable area  in a Word document involve iterating through each paragraph of the document and removing the PermissionStart and PermissionEnd objects. Here are the detailed steps:

  • Create a Document object.
  • Load a Word document using the Document.LoadFromFile() method.
  • Iterate through each paragraph in each section of the document, check for the presence of PermissionStart or PermissionEnd objects, and remove them.
  • Save the resulting document using the Document.SaveToFile() method.
  • C#
using Spire.Doc;
using Spire.Doc.Documents;

namespace SpireDocDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
             // Create a new document object
            Document document = new Document();

            // Load the document from the specified path
            document.LoadFromFile("Sample2.docx");

            // Iterate through the sections of the document
            for (int a = 0; a < document.Sections.Count; a++)
            {
                // Get the body of the current section
                Body body = document.Sections[a].Body;

                // Iterate through the child objects of the body
                for (int i = 0; i < body.ChildObjects.Count; i++)
                {
                    // Check if the child object is a paragraph
                    if (body.ChildObjects[i] is Paragraph)
                    {
                        // Get the current paragraph
                        Paragraph paragraph = (Paragraph)body.ChildObjects[i];

                        // Iterate backwards from the last child object of the paragraph
                        for (int j = paragraph.ChildObjects.Count - 1; j >= 0; j--)
                        {
                            // Get the current child object
                            DocumentObject documentObject = paragraph.ChildObjects[j];

                            // Remove the current child object if it is a permission start object
                            if (documentObject.DocumentObjectType == DocumentObjectType.PermissionStart)
                            {
                                paragraph.ChildObjects.RemoveAt(j);
                            }
                            // Remove the current child object if it is a permission end object
                            else if (documentObject.DocumentObjectType == DocumentObjectType.PermissionEnd)
                            {
                                paragraph.ChildObjects.RemoveAt(j);
                            }
                        }
                    }
                }
            }

            // Save the modified document to the specified path
            document.SaveToFile("RemovedEditingPermissionsArea.docx", FileFormat.Docx);

            // Close the document and release the resources occupied by the document object
            document.Close();
            document.Dispose();
        }
    }
}

C#: Add or Remove Editable area in a Word Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Security
Tuesday, 05 July 2022 07:35

C#/VB.NET: Digitally Sign Word Documents

A signature confirms that the digital document originated from the signer and has not been tampered with during transit. The use of digital signatures eliminates the need for sending paper documents, and reduces the number of the documents that need to be printed, mailed, and stored, saving you time and money. In this article, you will learn how to digitally sign a Word document in C# and VB.NET using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Add a Digital Signature to Word in C#, VB.NET

The steps are as follows.

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Specify the path and the password of a .pfx certificate.
  • Digitally sign the document while saving the document using Document.SaveToFile(string fileName, FileFormat fileFormat, string certificatePath, string securePassword) method. Here are some other methods that you can use to digitally sign a Word document.
    • public void SaveToFile(string fileName, FileFormat fileFormat, byte[] certificateData, string securePassword);
    • public void SaveToStream(Stream stream, FileFormat fileFormat, byte[] certificateData, string securePassword);
    • public void SaveToStream(Stream stream, FileFormat fileFormat, string certificatePath, string securePassword);
    • public static byte[] Document.Sign(Stream sourceStream, byte[] certificateData, string securePassword);
    • public static byte[] Document.Sign(Stream sourceStream, string certificatePath, string securePassword);
  • C#
  • VB.NET
using Spire.Doc;

namespace DigitallySignWord
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document doc = new Document();

            //Load a Word file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx");

            //Specify the certificate path
            string certificatePath = "C:\\Users\\Administrator\\Desktop\\gary.pfx";

            //Specify the password of the certificate
            string password = "e-iceblue";

            //Digitally sign the document while saving it to a .docx file
            doc.SaveToFile("AddDigitalSignature.docx", FileFormat.Docx2013, certificatePath, password);
        }
    }
}

C#/VB.NET: Digitally Sign Word Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Security

With the help of Spire.Doc, developers can encrypt word with password, and also convert the word document to PDF. This article will show you how to convert Word to PDF with encrypted password for the resulted PDF file.

Make sure Spire.Doc for .NET Version 5.8.92 (or above) has been installed correctly and then add Spire.Doc.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll".

Here comes to the code snippets of how to create password encrypted PDF directly from word to PDF conversion.

Step 1: Create a new word document and load the document from file.

Document document = new Document(false);
document.LoadFromFile("Sample.docx");

Step 2: Create an instance of ToPdfParameterList.

ToPdfParameterList toPdf = new ToPdfParameterList();

Step 3: Set open password, permission password and user's permission over the PDF document

toPdf.PdfSecurity.Encrypt("open password","permission password", PdfPermissionsFlags.None, PdfEncryptionKeySize.Key128Bit);

Step 4: Save the document to file.

document.SaveToFile("EncryptedPDF.pdf",toPdf);

Effective screenshot:

Encrypt PDF with password from word to PDF conversion

Full codes:

using Spire.Doc;
namespace EncryptPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document(false);
            document.LoadFromFile("Sample.docx");

            ToPdfParameterList toPdf = new ToPdfParameterList();
            toPdf.PdfSecurity.Encrypt("open password","permission password", PdfPermissionsFlags.None, PdfEncryptionKeySize.Key128Bit);

            document.SaveToFile("EncryptedPDF.pdf", toPdf);
        }
    }
}
Published in Security
Wednesday, 02 November 2022 08:03

C#/VB.NET: Protect or Unprotect a Word Document

Word documents can be protected in a variety of ways, depending on the security requirements. To prevent unauthorized people from opening a document, you can encrypt it with a password. To allow users to open the document, but not edit or modify its content, you can make the document read-only or mark it as final. To allow users to modify parts of the document, you can lock the entire document but let specified sections available for editing. This article focuses on how to protect or unprotect a Word document in C# and VB.NET using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Password Protect a Word Document in C#, VB.NET

Encrypting a document with a password makes sure that only you and certain people can read or edit it. The following are the steps to protect a Word document with a password using Spire.Doc for .NET.

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Encrypt the document with a password using Document.Encrypt() method.
  • Save the document to another Word file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace PasswordProtectWordDocument
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document document = new Document();

            //Load a Word file
            document.LoadFromFile(@"C:\Users\Administrator\Desktop\test.docx");

            //Encrypt the document with a password
            document.Encrypt("open-psd");

            //Save the document to another Word file
            document.SaveToFile("Encryption.docx", FileFormat.Docx);
        }
    }
}

C#/VB.NET - How to Protect or Unprotect a Word Document

Change Permission of a Word Document in C#, VB.NET

Documents encrypted with an open password cannot be opened by those who do not know the password. If you’d like to grant people permission to read your document but restrict the types of modifications that someone can make, you can set the document permission. The following are the steps to change permission of a Word document using Spire.Doc for .NET.

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Set the document permission and set the permission password using Document.Protect() method.
  • Save the document to another Word file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace ChangeDocumentPermission
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document document = new Document(); 

            //Load a Word document
            document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");

            //Set the document permission and set the permission password
            document.Protect(ProtectionType.AllowOnlyFormFields, "permission-psd");

            //Save the document to another Word file
            document.SaveToFile("Permission.docx");
        }
    }
}

C#/VB.NET - How to Protect or Unprotect a Word Document

Lock Specified Sections of a Word Document in C#, VB.NET

When you protect your document, you can lock parts of it so that they cannot be changed and leave the unlocked parts available for editing. The following are the steps to protect specified sections of a Word document using Spire.Doc for .NET.

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Set the editing restriction as AllowOnlyFormFields.
  • Unprotect a specific section by setting Document.Sections[index].ProtectForm to false. The rest sections will continue to be protected.
  • Save the document to another Word file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace ProtectSpecificSection
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");

            //Set editing restriction as "AllowOnlyFormFields"
            doc.Protect(ProtectionType.AllowOnlyFormFields, "permissionPsd");

            //Unprotect section 2
            doc.Sections[1].ProtectForm = false;

            //Save the document to another Word file
            doc.SaveToFile("ProtectSection.docx");
        }
    }
}

C#/VB.NET - How to Protect or Unprotect a Word Document

Mark a Word Document as Final in C#, VB.NET

By marking a document as Final, you disable typing, editing, and format changes capabilities and a message will appear to any reader that the document has been finalized. The following are the steps to mark a Word document as final using Spire.Doc for .NET.

  • Create a Document object.
  • Load a Word file using Document.LoadFromFile() method.
  • Get the CustomDocumentProperties object from the document.
  • Add a custom property "_MarkAsFinal" to the document.
  • Save the document to another Word file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace MarkAsFinal
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");

            //Get custom document properties 
            CustomDocumentProperties customProperties = doc.CustomDocumentProperties;

            //Add "_MarkAsFinal" property to the document
            customProperties.Add("_MarkAsFinal", true);

            //Save the document to another Word file
            doc.SaveToFile("MarkAsFinal.docx");
        }
    }
}

C#/VB.NET - How to Protect or Unprotect a Word Document

Remove Password from a Word Document in C#, VB.NET

You can remove the password from an encrypted document if the encryption is no longer needed. The following are the detailed steps.

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Remove the password using Document.RemoveEncryption() method.
  • Save the document to another Word file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace RemovePassword
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document document = new Document();

            //Load an encrypted Word document
            document.LoadFromFile(@"C:\Users\Administrator\Desktop\Encryption.docx", FileFormat.Docx, "open-psd");

            //Remove encryption
            document.RemoveEncryption();

            //Save the document to another Word file
            document.SaveToFile("Decryption.docx", FileFormat.Docx);
        }
    }
}

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Security