PDF Encryption in C# and VB.NET

PDF encryption enables you to securely protect the content of your PDF file from unwanted viewers and against unwanted actions like printing, selecting text, etc.

GemBox.Document also supports reading encrypted PDF files.

Following example shows how to convert any document to encrypted PDF file with restricted permissions which requires a password to be decrypted, viewed and, optionally, modified.

Output password and owner password should be specified in the appropriate text box. Input password should also be specified if input PDF file is encrypted.

For more information about PDF encryption in GemBox.Document, see PdfSaveOptions.Permissions remarks.

Screenshot

PDF Encryption Screenshot

See the full code below, use RUN EXAMPLE to execute.

Upload your file (Drag files here)

1using System;
2using GemBox.Document;
3using GemBox.Document.Drawing;
4using System.IO;
5using System.Linq;
6
7class Sample
8{
9    [STAThread]
10    static void Main(string[] args)
11    {
12        // If using Professional version, put your serial key below.
13        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
14
15        string inputPassword = "inpass";
16        string outputPassword = "outpass";
17        string ownerPassword = "";
18
19        var document = DocumentModel.Load("PdfEncryption.pdf", new PdfLoadOptions() { Password = inputPassword });
20        var options = new PdfSaveOptions()
21        {
22            DocumentOpenPassword = outputPassword,
23            PermissionsPassword = ownerPassword,
24            Permissions = PdfPermissions.None
25        };
26
27        document.Save("PDF Encryption.pdf", options);
28    }
29}
1Imports System
2Imports GemBox.Document
3Imports GemBox.Document.Drawing
4Imports System.IO
5Imports System.Linq
6
7Module Samples
8
9    Sub Main()
10
11        ' If using Professional version, put your serial key below.
12        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
13
14        Dim inputPassword As String = "inpass"
15        Dim outputPassword As String = "outpass"
16        Dim ownerPassword As String = ""
17
18        Dim document = DocumentModel.Load("PdfEncryption.pdf", New PdfLoadOptions With {.Password = inputPassword})
19        Dim options = New PdfSaveOptions() With
20        {
21            .DocumentOpenPassword = outputPassword,
22            .PermissionsPassword = ownerPassword,
23            .Permissions = PdfPermissions.None
24        }
25
26        document.Save("PDF Encryption.pdf", options)
27
28    End Sub
29
30End Module

Check next sample or find out more about GemBox.Document and GemBox Software.