Protect Your Documents: Convert Word to Password-Protected PDFs with C#/VB.NET

Protect Your Documents: Convert Word to Password-Protected PDFs with C#/VB.NET

Sharing sensitive information requires a secure file format that provides protection against unauthorized access. One of the formats is password-protected PDF, which can be created programmatically using C# or VB.NET code. In this tutorial, we will use Spire.Doc for .NET to convert Word documents to password-protected PDFs. I have attached the C#/VB.NET code for your reference, along with the steps I took to sort things out.

How to Set Programming Environment?

In this test, Free Spire.Doc for .NET is introduced into the program. The Spire.Doc.dll file can be referenced by:

Method 1: Download Free Spire.Doc for .NET locally, unzip it, and install it. After the installation is complete, find Spire.Doc.dll in the BIN folder under the installation path. Then open the "Solution Explorer" in Visual Studio, right-click "References", "Add Reference", and add a reference to the dll file in the BIN folder of the local path to the program.

Method 2: Install via NuGet. It can be installed by the following 2 methods:

(1) You can open the "Solution Explorer" in Visual Studio, right-click "References", "Manage NuGet Packages", then search for "Free Spire.Doc", and click "Install". Wait for the program installation to complete.

(2) Copy the following content to the PM console installation.

Install-Package FreeSpire.Doc -Version 10.8.0

Convert Word documents to password-protected PDF

Follow the steps below to convert Word to PDF with password in just three steps.

Step 1: Create a Document object.

Step 2: Load a sample Word document using Document.LoadFromFile() method.

Step 3: Create a ToPdfParameterList object, which is used to set conversion options.

Step 4: Specify the open password and permission password and then set both passwords for the generated PDF using ToPdfParameterList.PdfSecurity.Encrypt() method.

Step 5: Save the Word document to PDF with password using Doucment.SaveToFile(string fileName, ToPdfParameterList paramList) method.

Full Code

C#

using Spire.Doc;

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

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

            //Create a ToPdfParameterList instance
            ToPdfParameterList parameters = new ToPdfParameterList();

            //Set open password and permission password for PDF
            string openPsd = " Carinababy";
            string permissionPsd = "abc123";
            parameters.PdfSecurity.Encrypt(openPsd, permissionPsd, Spire.Pdf.Security.PdfPermissionsFlags.Default, Spire.Pdf.Security.PdfEncryptionKeySize.Key128Bit);

            //Save the Word document to PDF with password
            document.SaveToFile("ToPDFWithPassword.pdf", parameters);
        }
    }
}

VB.NET

Imports Spire.Doc

Namespace ToPDFWithPassword
    Class Program
        Private Shared Sub Main(ByVal args() As String)
            'Create a Document object
            Dim document As Document = New Document

            'Load a sample Word document
            document.LoadFromFile("C:\Users\Administrator\Desktop\Test.docx")

            'Create a ToPdfParameterList instance
            Dim parameters As ToPdfParameterList = New ToPdfParameterList

            'Set open password and permission password for PDF
            Dim openPsd As String = "Carinababy"
            Dim permissionPsd As String = "abc123"
            parameters.PdfSecurity.Encrypt(openPsd, permissionPsd, Spire.Pdf.Security.PdfPermissionsFlags.Default, Spire.Pdf.Security.PdfEncryptionKeySize.Key128Bit)

            'Save the Word document to PDF with password
            document.SaveToFile("ToPDFWithPassword.pdf", parameters)
        End Sub
    End Class
End Namespace

Effective Shot

Conclusion:

In addition to Word documents to password-protected PDF conversion, Spire.Doc for .NET also offers many other useful features. For example, you can use Spire.Doc to Convert Word to Excel, Merge Word Documents, Insert Hyperlinks to Word Documents and so on. Apart from that, if you'd like to learn more, you can visit this link to explore more about for Spire.Doc for .NET.