pdf links

PDF Rendering
Convert PDF to Image (.NET)
Convert PDF to image on Android (Xamarin)
Convert PDF to image on iOS (Xamarin)
Convert PDF to image in Windows Store apps (.NET)
Convert PDF to image in Windows Phone apps (.NET)
PDF to image in Universal Windows Store apps (.NET)
Free PDF Viewer control for Windows Forms (.NET)
How to integrate PDF Viewer control in WPF app (.NET)
Creating WPF PDF Viewer supporting bookmarks (.NET)
Cross-platform PDF Viewer using GTK# (MONO)
Silverlight PDF viewer control (Silverlight 5)
Multithreaded PDF rendering (.NET)
Convert pdf to image in Silverlight app (C# sample)
How to set fallback fonts for PDF rendering (C#)
Avoiding the out-of-memory exception on rendering (C#)
PDF viewer single page application (WebAPI, AngularJS)
PDF viewer control for Windows 10 universal applications
Use custom ICC profile for CMYK to RGB conversion
PDF layers - separate images, text, annotations, graphics

PDF Forms Creation PDF Security
Conversion to PDF/A
Other topics
PDF Document Manipulation PDF Content Generation
Fixed and Flow layout document API (.NET)
Creation of grids and tables in PDF (C# sample)
How to create interactive documents using Actions (C# sample)
Text flow effects in PDF (C# sample)
How to generate ordered and bulleted lists in PDF (C# sample)
Convert HTML to PDF using flow layout API (C# sample)
How to use custom fonts for PDF generation (.NET)
Create document with differently sized pages (C#)
Create PDF documents using MONO (C#/MONO/Windows/OSX)
How to use background images for content elements (C#/PDF Kit/FlowLayout)
Add transparent images to PDF document (C#)
Draw round rect borders in PDF documents(C#)
ICC color profiles and and ICC based colors in PDF (C#)
How to use bidirectional and right to left text in PDF (C#)
Create PDF documents from XML templates (C# sample)
How to resize PDF pages and use custom stamps (C#)
Add header and footer to PDF page (.NET sample)
How to use clipping mask for drawing on PDF page
Fill graphics path with gradient brushes in PDF (Shadings)
Apitron PDF Kit and Rasterizer engine settings
Add layers to PDF page (optional content, C# sample)
How to create free text annotation with custom appearance
PDF Content Extraction PDF Navigation
PDF to TIFF conversion
Contact us if you have a PDF related question and we'll cover it in our blog.

2015-06-05

PDF Security, part 1 – create or open password protected PDF documents

Introduction


PDF allows you to protect the contents of your documents by setting a password. There are two types of passwords that can be set - an owner password and a user password.

Whether additional operations will be allowed on a decrypted document depends on which password (if any) was supplied when the document was opened, and on any access restrictions that were specified when the document was created:
  • Opening the document with the correct owner password should allow full access to the document. This unlimited access includes the ability to change the document’s passwords and access permissions.
  • Opening the document with the correct user password, or opening a document with the default password, should allow additional operations to be performed according to the user access permissions specified for the document.

The complete documentation can be found in section 7.6.4 Public-Key Security Handlers in PDF specification.

User access permissions control the printing, content extraction and editing, and other possible modifications of the source document. For the complete list of user access permissions see the section 7.6.4.2 Public-Key Encryption Dictionary. 

Setting PDF document's password


The code below opens existing PDF document and sets both passwords for it. It also sets the optional encryption algorithm and permissions entry.

public void SetDocumentPassword()
{
    // open existing document and create output stream
    using (Stream inputStream = File.OpenRead("hello world.pdf"),
        outputStream = File.Create("password protected.pdf"))
    {
        using (FixedDocument doc = new FixedDocument(inputStream))
        {
            // create security settings for the document
            doc.SecuritySettings = new SecuritySettings();
            // set document's passwords
            doc.SecuritySettings.OwnerPassword = "owner";
            doc.SecuritySettings.UserPassword = "user";
            // set document's encryption level and permissions
            doc.SecuritySettings.EncryptionLevel = EncryptionLevel.AES_128bit;
            doc.SecuritySettings.Permissions = Permissions.AllowAllPermissions;

            // save document
            doc.Save(outputStream);
        }
    }
}

When you open the generated document the password request windows appears:

Pic. 1 Opening PDF document - password request window

Pic. 1 Opening PDF document - password request window


After entering the correct password, you’ll be able to see document’s content and examine its security properties. See the image below:


Pic. 2 Opened password protected PDF document

Pic. 2 Opened password protected PDF document

Clicking on Permission Details opens the details window showing all properties set:

Pic. 3 Document security properties

Pic. 3 Document security properties


Opening password protected PDF document


The sample below opens password protected document created using the code from previous example, and prints the number of pages in this document and encryption level used.

public void OpenPasswordProtectedDocument()
{
    // open existing pdf document
    using (Stream inputStream = File.OpenRead("password protected.pdf"))
    {
        // provide the owner password to read the document
        FixedDocument doc = new FixedDocument(inputStream, "owner");
       
        Console.WriteLine("Page count: {0}", doc.Pages.Count);                       
        Console.WriteLine("Encryption level: {0}",
        doc.SecuritySettings.EncryptionLevel);                       
    }
}

Resulting output is shown below:

Pic. 4 Reading password protected pdf document

Pic. 4 Reading password protected pdf document


Conclusion


In this post we demonstrated how to create or open password protected PDF documents using Apitron PDF Kit library. This library can be used by applications targeting many platforms like Android and iOS (via Xamarin), Windows, Windows Phone, Windows Store and others. It also can be used to create server, cloud and web solutions, ranging from small business to enterprise level. Read our free book showing the component in action, and see for yourself. Contact us if you have any questions, we are always ready to help.

Downloadable version of this article can be found by the following link [PDF].

No comments:

Post a Comment