How to Decrypt PDF and Extract Image from It – C#/VB.NET

Last time, I had introduced methods about PDF security, including how to encrypt PDF and add digital certificate. In this post, I want to show another method about how to decrypt PDF document with C#/VB.NET.

In my example, I have an encrypted PDF document, which saves an image about employee information. What I will do is to decrypt this document with password and extract image from it.

Also, I use s component Spire.PDF in my method. Therefore, I add its dll file as reference when starting with the following steps.

Steps:

  1. Declare a string encryptedPdf and assign its value as the document which has been encrypted. Then, create a new PDF document. There are two parameters passed to PdfDocument method, string encryptedPdf and password, which is used to open the encrypted document.  It means that the new PDF document we create is original document which has been decrypted.
  2. Extract Image. Suppose that there are many pages in document. We need to extract images from each page.
  3. Save and launch image file.

Original Document

Main Coding:

C#

using System;

using System.Drawing;

using Spire.Pdf;

namespace PDFDecrypt

{

    class Decrypt

    {

        static void Main(string[] args)

        {

            //Create Document

            String encryptedPdf = @”D:\work\My Documents\Encryption.pdf”;

            PdfDocument doc = new PdfDocument(encryptedPdf, “123456”);

 

            //Extract Image

            Image image = doc.Pages[0].ImagesInfo[0].Image;

 

            doc.Close();

 

            //Save

            image.Save(“EmployeeInfo.png”, System.Drawing.Imaging.ImageFormat.Png);

 

            //Launch

            System.Diagnostics.Process.Start(“EmployeeInfo.png”);

        }

    }

}

VB.NET

Imports System.Drawing

Imports Spire.Pdf

Module Decrypt

    Sub Main()

        ‘Create Document

        Dim encryptedPdf As String = “D:\work\My Documents\Encryption.pdf”

        Dim doc As New PdfDocument(encryptedPdf, “123456”)

 

        ‘Extract Image

        Dim image As Image = doc.Pages(0).ImagesInfo(0).Image

        doc.Close()

 

        ‘Save

        image.Save(“EmployeeInfo.png”, System.Drawing.Imaging.ImageFormat.Png)

 

        ‘Launch

        System.Diagnostics.Process.Start(“EmployeeInfo.png”)

    End Sub

End Module

Result Shown by Following:

______________________________________________________________________________________________

Click Here to Learn more about Spire.PDF

Click Here to Download Spire.PDF

Related Posts:

Encrypt PDF

Create PDF Digital Certificate

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s