Secure PDF Files With a Password in C#, VB, and Java

In the “Merge PDF Files – C#, VB, and Java Code” article, we described how to use the PDFFile class from the PDF SDK to merge multiple PDF files into one with just a couple lines of code. Using the PDFFile class also makes PDF encryption, decryption, and password protection just as easy! The PDFFile class is available for .NET FX, .NET Core, and Java.

There are two types of PDF password protection:

  • User password: required to view the PDF. The usage of this password can encryption the PDF file. Also known as a “Document open password” in the Acrobat documentation.
  • Owner password: not required to view the document, but is required to change permissions, such as copy content and print. Also known as the “Permissions password” in the Acrobat documentation.

If both passwords are applied to a PDF, then either password can be used to to view the PDF document. When setting the User password, you can specify either RC 40-bit or RC 128-bit encryption.

Below are some C#, VB, and Java code snippets that can be used to password protect PDF files.

Encrypt PDF Files C# Code

private static void Main(string[] args)
   // Check to make sure number of args is correct, files exist, etc...
   if (FailsPreFlight(args)) return;

      ProtectPdfFile(args[0], args[1]);
   catch (Exception ex)

private static void ProtectPdfFile(string destinationPdf, string sourcePdf)
    string sourcePdfPassword = PDFFile.IsEncrypted(sourcePdf) ? "User_9@55w0Rd" : "";
    new PDFFile(sourcePdf, sourcePdfPassword)
        SecurityOptions = new PDFSecurityOptions
             UserPassword = "User_9@55w0Rd",
             OwnerPassword = "Owner_9@55w0Rd",
             EncryptionMode = PDFEncryptionMode.RC128Bit,
             AssemblyEnabled = false,
             AnnotationsEnabled = true,
             CopyEnabled = false,
             CopyForAccessibilityEnabled = false,
             EditEnabled = false,
             PrintEnabled = false,
             HighQualityPrintEnabled = false,
             FormFieldFillingEnabled = false
    }.Convert(1, -1, destinationPdf);

Encrypt PDF Files VB Code

Dim sourcePdfPassword As String = If(PDFFile.IsEncrypted(sourcePdf), "User_9@55w0Rd", "")
New PDFFile(sourcePdf, sourcePdfPassword) With {
    .SecurityOptions = New PDFSecurityOptions With {
        .UserPassword = "User_9@55w0Rd",
        .OwnerPassword = "Owner_9@55w0Rd",
        .EncryptionMode = PDFEncryptionMode.RC128Bit,
        .AssemblyEnabled = False,
        .AnnotationsEnabled = True,
        .CopyEnabled = False,
        .CopyForAccessibilityEnabled = False,
        .EditEnabled = False,
        .PrintEnabled = False,
        .HighQualityPrintEnabled = False,
        .FormFieldFillingEnabled = False
}.Convert(1, -1, destinationPdf)

Encrypt PDF Files Java Code

String sourcePdfPassword = PDFFile.isEncrypted(sourcePdf) ? "User_9@55w0Rd" : "";

PDFSecurityOptions securityOptions = new PDFSecurityOptions();

PDFFile pdfFile = new PDFFile(sourcePdf, sourcePdfPassword);
pdfFile.convert(1, -1, destinationPdf);

See For Yourself – Free Evaluation

Download the LEADTOOLS SDK for free. It’s fully-functional for 60 days and comes with free chat and email support.

Stay Tuned For More Conversion Samples

Did you see our previous post, “Merge PDF Files – C#, VB, and Java Code”? Stay tuned for more conversion examples to see how LEADTOOLS easily fits into any workflow converting PDF files into other document files or images and back again. Need help in the meantime? Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team via email or call us at +1-704-332-5532.


Developer Advocate

    Find more about me on:
  • linkedin
  • twitter
  • youtube
This entry was posted in PDF and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *