How to protect PDF with password using C#

I’m using Aspose.Words and I convert a Word document to a PDF which I then save to an online file storage.

Later on, I need to add a password to the PDF so the customer receives a protected PDF. Is this possible? If so, how would I go about this?

Kind regards,
Nathan

Can someone help?

@nathanvj

Following code example shows how to set password to PDF file. Hope this helps you.

Document doc = new Document(MyDir + "Rendering.docx");

PdfSaveOptions saveOptions = new PdfSaveOptions();

// Create encryption details and set owner password
PdfEncryptionDetails encryptionDetails =
    new PdfEncryptionDetails("password1", "password2", PdfEncryptionAlgorithm.RC4_128);

// Start by disallowing all permissions
encryptionDetails.Permissions = PdfPermissions.DisallowAll;

// Extend permissions to allow editing or modifying annotations
encryptionDetails.Permissions = PdfPermissions.ModifyAnnotations | PdfPermissions.DocumentAssembly;
saveOptions.EncryptionDetails = encryptionDetails;

// Render the document to PDF format with the specified permissions
doc.Save(ArtifactsDir + "Rendering.EncryptionPermissions.pdf", saveOptions);

Here you use a .docx file as input. Is it also possible to password protect an already converted pdf file?

Nevermind I’ll just generate the pdf again… unconvenient but it works.

@nathanvj

You can import PDF document into Aspose.Words’ DOM and use same PdfEncryptionDetails properties to get the desired output.

A post was split to a new topic: Add a password to the PDF file