Click here to Skip to main content
12,395,117 members (68,008 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

69.9K views
9.1K downloads
12 bookmarked
Posted

Simple and Free PDF to Image Conversion

, 23 Feb 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Simple and free Adobe Acrobat PDF to image conversion.

2-7-2010_2-00-29_PM.png

Introduction

This article is about extracting image files from a PDF file. I was looking for a free solution for converting .pdf files to image files, but I didn't find a simple and free solution. I therefore tried until I found a free solution by using the "Adobe Acrobat COM component" and using the CAcroPDDoc class.

Pre-requisites

You must have "Adobe Acrobat Reader" installed on your system. My system had Adobe Acrobat 9.0 installed.

Using the code and description

First, I added a reference to the Adobe Acrobat COM component.

AddRefrence.png

I then wrote the PDFConvertor class that has a method named Convert(....) which would do the conversion. Here is the code in my class used for accessing a PDF document:

Acrobat.CAcroPDPage pdfPage = null;//the pdf page
Acrobat.CAcroRect pdfRect = new Acrobat.AcroRect();//the pdf region
Acrobat.AcroPoint pdfPoint =new Acrobat.AcroPoint();//the pdf point
  • CAcroPDDoc class is for working with the PDF file
  • CAcroPDPage class is for working with the pages in the PDF file
  • CAcroRect and AcroPoint classes are for defining the dimensions of a page

Here is how I open a PDF document:

if (pdfDoc.Open(sourceFileName))//check file is opened?

For opening a specified PDF file, I use the open() method of the pdfDoc object; it returns false in the case of an error.

pageCount = pdfDoc.GetNumPages();//get the count of pdf pages

After reading the page count withpdfDoc.GetNumPages() , I then read a page.

pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i); //reading current page

Then, I extract a pages in the PDF file using pdfDoc.AcquirePage(i) and assign it to the pdfPage object; the variable i indicates the number of the current page.

pdfPoint = (Acrobat.AcroPoint)pdfPage.GetSize();//geting page size
pdfRect.Left = 0;
pdfRect.right = pdfPoint.x;
pdfRect.Top = 0;
pdfRect.bottom = pdfPoint.y;

I then get the page size with pdfPage.GetSize() and put it into a pdfPoint object. This is required for specifying the region of the PDF page for copying the page into the Clipboard.

pdfPage.CopyToClipboard(pdfRect, 0, 0, 100);//coping region to clipboard

Make sure that pdfPage doesn't have any method for saving as the referenced page. pdfPage.CopyToClipboard(pdfRect, 0, 0, 100) can help us there. It has four arguments: first is the rect of the page that was previously discussed, second and third are x, y offsets of the page that usually are 0, and fourth is the zoom percent.

Clipboard.GetImage().Save(outimg, outPutImageFormat);// saving clipboard image

Finally, with Clipboard.GetImage().Save(...), we can save the output image.

Here is the code for the Convert method of my class:

#region Convert
/// 
/// Converting PDF Files TO Specified Image Format
/// 
/// sourceFileName : Source PDF File Path
/// DestinationPath : Destination PDF File Path
/// outPutImageFormat: Type Of Exported Image
/// Returns Count Of Exported Images
public int Convert(string sourceFileName, 
       string DestinationPath, ImageFormat outPutImageFormat)
{
    if (pdfDoc.Open(sourceFileName))
    {
        // pdfapp.Hide();
        pageCount = pdfDoc.GetNumPages();

        for (int i = 0; i < pageCount; i++)
        {
            pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i);

            pdfPoint = (Acrobat.AcroPoint)pdfPage.GetSize();
            pdfRect.Left = 0;
            pdfRect.right = pdfPoint.x;
            pdfRect.Top = 0;
            pdfRect.bottom = pdfPoint.y;

            pdfPage.CopyToClipboard(pdfRect, 500, 110, 100);

            string outimg = "";
            string filename=sourceFileName.Substring(
                       sourceFileName.LastIndexOf("\\")); 

            if (pageCount == 1)
                outimg = DestinationPath + "\\" + filename + 
                  "." + outPutImageFormat.ToString();
            else
                outimg = DestinationPath + "\\" + filename + 
                  "_" + i.ToString() + "." + outPutImageFormat.ToString();
            
            Clipboard.GetImage().Save(outimg, outPutImageFormat);

            ////////////Firing Progress Event 
            OnExportProgressChanging(outimg);
        }

          Dispose();
    }
    else
    {
        Dispose();
        throw new System.IO.FileNotFoundException(sourceFileName + 
                  " Not Found!");
    }
    return pageCount;
}
#endregion

I have also implemented a class for indicating the progress with the event handler ProgressChangingEventHandler that exists in the source of my project.

Enjoy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

zendehdel
Web Developer SETIN SANAT CO
Iran (Islamic Republic of) Iran (Islamic Republic of)
i am working in programming field for about 7 years
Group type: Social Group

1 members


You may also be interested in...

Comments and Discussions

 
QuestionWhen WE save Image quality degrades more can we save PNG image with original quality? Pin
deipak8-Apr-16 1:51
memberdeipak8-Apr-16 1:51 
Generalcan you change the DPI setting Pin
Member 114356825-Dec-15 7:37
memberMember 114356825-Dec-15 7:37 
Questionconvert pdf to image in WPF Pin
D3athRace2-Dec-15 19:04
memberD3athRace2-Dec-15 19:04 
QuestionI am trying to add Acrobat COM in My Project, But It is not available in list. Please help...... Pin
Member 1136818824-Apr-15 0:15
memberMember 1136818824-Apr-15 0:15 
SuggestionIt doesn't work on 64-bit system Pin
openxxs10-Sep-13 18:10
memberopenxxs10-Sep-13 18:10 
SuggestionYou are misleading readers. Pin
Member 87055856-Aug-13 1:00
memberMember 87055856-Aug-13 1:00 
GeneralRe: You are misleading readers. Pin
zendehdel23-Jul-14 22:22
groupzendehdel23-Jul-14 22:22 
Questioni downloaded your project and it is nice but the below error coming how can i solve this problem Pin
sivajuluri9-May-13 0:19
membersivajuluri9-May-13 0:19 
AnswerRe: i downloaded your project and it is nice but the below error coming how can i solve this problem Pin
zbw91110-Aug-13 0:12
memberzbw91110-Aug-13 0:12 
GeneralMy vote of 3 Pin
DanWiify12-Apr-13 6:29
memberDanWiify12-Apr-13 6:29 
GeneralNice Post Pin
Member 967854127-Jan-13 6:59
memberMember 967854127-Jan-13 6:59 
GeneralRe: Nice Post Pin
zendehdel20-Jul-14 23:34
groupzendehdel20-Jul-14 23:34 
QuestionThis is nice and simple code that works! Pin
yugangliang22-Jan-13 6:36
memberyugangliang22-Jan-13 6:36 
Unlike many users who ranked this code only 1 star, I give it a 5 star. I have a need to convert nearly 1 million PDF docs to images and I did not find reliable and simple ways to fulfill this need. This code is the first simple enough code for me for my need, and the Acrobat component is free (it is free because you can download an Acrobat reader for free). This code also works with my free MS Visual Webdevelper 2010. And it is simple for me to run and copy to my own code.

Thank you for sharing this code.
GeneralMy vote of 5 Pin
yugangliang22-Jan-13 6:30
memberyugangliang22-Jan-13 6:30 
GeneralAdobe Acrobat Reader does not support it Pin
RubyPdf29-Sep-10 16:43
memberRubyPdf29-Sep-10 16:43 
GeneralRe: Adobe Acrobat Reader does not support it Pin
zendehdel22-Sep-12 2:19
groupzendehdel22-Sep-12 2:19 
Generalproblems with windows 7 Pin
ScratchFish31-Mar-10 4:36
memberScratchFish31-Mar-10 4:36 
GeneralRe: problems with windows 7 Pin
zendehdel10-Apr-10 19:30
memberzendehdel10-Apr-10 19:30 
GeneralRe: problems with windows 7 Pin
ScratchFish12-Apr-10 3:50
memberScratchFish12-Apr-10 3:50 
GeneralI am using Visual Studio 2008, and Adobe Acrobat reader 8.2.0 I have no Acrobat COM Pin
MicroImaging11-Mar-10 10:43
memberMicroImaging11-Mar-10 10:43 
GeneralRe: I am using Visual Studio 2008, and Adobe Acrobat reader 8.2.0 I have no Acrobat COM Pin
zendehdel10-Apr-10 19:24
memberzendehdel10-Apr-10 19:24 
Generalok Pin
zendehdel16-Feb-10 21:23
memberzendehdel16-Feb-10 21:23 
GeneralMy vote of 1 Pin
Programm3r7-Feb-10 19:31
memberProgramm3r7-Feb-10 19:31 
GeneralMy vote of 1 Pin
Dave Kreskowiak7-Feb-10 8:28
mvpDave Kreskowiak7-Feb-10 8:28 
GeneralMy vote of 1 Pin
Mark Nischalke7-Feb-10 2:16
mvpMark Nischalke7-Feb-10 2:16 
GeneralMy vote of 1 Pin
John Simmons / outlaw programmer7-Feb-10 2:01
memberJohn Simmons / outlaw programmer7-Feb-10 2:01 
GeneralMy vote of 1 Pin
Md. Marufuzzaman7-Feb-10 1:28
mvpMd. Marufuzzaman7-Feb-10 1:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 24 Feb 2010
Article Copyright 2010 by zendehdel
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid