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();
document.LoadFromFile("Sample.docx");

Step 2: Create an instance of ToPdfParameterList.

ToPdfParameterList toPdf = new ToPdfParameterList();

Step 3: Set the user password for the resulted PDF file.

toPdf.PdfSecurity.UserPassword = "e-iceblue";

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:

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

ToPdfParameterList toPdf = new ToPdfParameterList();
toPdf.PdfSecurity.UserPassword = "e-iceblue";

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