Decrypt Unprotect PDF. Unlock file removing permissions and password from PDF files.

A secure PDF file enforces copyright laws with features to prevent reproduction or distribution by others using passwords or digital certificates. The owner or creator of a PDF file can restrict user access, printing, page combining, bookmarks adding and text copying with password protection.

If you have the administration password for the PDF file, you can remove the password protected encryption, and change the settings and unlock protected PDF files.

With the help of .NET PDF unlocker component, developers can make a right permission to each user easily. The following C# code shows three cases, user inputting the error, owner or user password.


PdfDocument document;

string filename = "Protect.pdf";

// Opening a document will fail with an invalid password.
try
{
    document = PdfReader.Open(filename, "error password");
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

// Open the document with the user password.
try
{
    document = PdfReader.Open(filename, "user password", PdfDocumentOpenMode.ReadOnly);

    // Use the property HasOwnerPermissions to decide whether the used password
    // was the user or the owner password. 
    bool hasOwnerAccess = document.SecuritySettings.HasOwnerPermissions;

    // if it's the user password, not having the owner permission
    if (!hasOwnerAccess)
    {
         // Maybe only allow user to view or print the pdf document
         // To do...
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

// Open the document with the owner password.
try
{
    document = PdfReader.Open(filename, "owner password");

    // Use the property HasOwnerPermissions to decide whether the used password
    // was the user or the owner password. 
    bool hasOwnerAccess = document.SecuritySettings.HasOwnerPermissions;

    // if it's the owner password, having the owner permission
    if (hasOwnerAccess)
    {
        // Allow user to make all processing and modifying to pdf file
        // Such as adding a text in the beginning of the 1st page
        XGraphics g = XGraphics.FromPdfPage(document.Pages[0]);
        g.DrawString("Can be edited!", new XFont("Times New Roman", 12), XBrushes.Red, 20, 20);

        // Save and show the document
        document.Save("Unprotect.pdf");
        Process.Start("Unprotect.pdf");
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}          

In the current C# sample, the old passwords are not automatically reused. If you want to save it protected, you must set the DocumentSecurityLevel and apply new passwords. Please see Password Protect PDF sample for further information.

More PDF tutorial