Encrypt PDF with password from word to PDF conversion

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