# Document Protection

Learn how to protect documents with password using PdfDocumentSource.

## Content



**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](https://cdn.mescius.io/document-site-files/images/a0c58113-bcd8-49ac-b5d4-2d50837f0be4/images/add-password.gif)

## 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:
    
    ```csharp
    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.
    
    ```csharp
    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);
    }
    ```