Password Protecting PDF File

I have the following:

  • procedure X, which creates a PDF file daily.
  • Procedure Y, which attaches this file to Outlook email and sends it to recipients.

Both of the above are in VBA. They are called from a C # console application.

Once the PDF is created, I need to password protect it. It is quite difficult to do this using VBA without buying third-party software.

What is the simplest solution using C #?

(I suspect there will be a feedback between the amount we spend and the complexity of the answer!)

+8
c #
source share
1 answer

PDFSharp should be able to protect the PDF file with a password:

// Open an existing document. Providing an unrequired password is ignored. PdfDocument document = PdfReader.Open(filename, "some text"); PdfSecuritySettings securitySettings = document.SecuritySettings; // Setting one of the passwords automatically sets the security level to // PdfDocumentSecurityLevel.Encrypted128Bit. securitySettings.UserPassword = "user"; securitySettings.OwnerPassword = "owner"; // Don't use 40 bit encryption unless needed for compatibility reasons //securitySettings.DocumentSecurityLevel = PdfDocumentSecurityLevel.Encrypted40Bit; // Restrict some rights. securitySettings.PermitAccessibilityExtractContent = false; securitySettings.PermitAnnotations = false; securitySettings.PermitAssembleDocument = false; securitySettings.PermitExtractContent = false; securitySettings.PermitFormsFill = true; securitySettings.PermitFullQualityPrint = false; securitySettings.PermitModifyDocument = true; securitySettings.PermitPrint = false; // Save the document... document.Save(filename); 

Reference:
http://www.pdfsharp.net/wiki/ProtectDocument-sample.ashx

+10
source share

All Articles