Document Library for WinForms | ComponentOne
PdfDocumentSource / Document Protection
In This Topic
    Document Protection
    In This Topic

    PdfDocumentSource allows you to set password for protecting your PDF document. It allows you to create a password protected PDF using the PdfSecurityOptions class from C1.Win.Document.Export namespace. The PdfSecurityOptions class lets you set a password to open the PDF document using the UserPassword property.

    GID showing user adding password and opening files

    Add password via code

    Let's say the user wants to generate a password-protected PDF document at the click of a button. For this, you can follow the steps below:

    1. Load the PDF document using the LoadFromFile method of PdfDocumentSource component:

      C#
      Copy Code
      private void Form1_Load(object sender, EventArgs e)
      {
          c1PdfDocumentSource1.LoadFromFile(@"..\..\DefaultDocument.pdf");
      }
      
    2. Create an object of PDFFilter class to access the PDF document using the FileName property and set the password using the PdfSecurityOptions.UserPassword property. Then, export the PDF with password using the Export method.

      C#
      Copy Code
      private void pwBtn_Click(object sender, EventArgs e)
      {
          //export pdf with password
          var filter = new PdfFilter();
          filter.FileName = @"..\..\Password.pdf";
          filter.PdfSecurityOptions.UserPassword = "pass1234";
          c1PdfDocumentSource1.Export(filter);
      }