Friday, October 11, 2013

Convert PDF to image And Extract them into individual Pages from Existing PDF to a new File (C# .NET sample)

Convert PDF to image And Extract them into individual Pages from Existing PDF to a new File (C# .NET sample) 

I was recently working on received PDF files from a website which it then needed to store in a Folder and split these files into images and individual pages. I wanted the ability to display previews of these documents within the application. While there are a number of solutions for creating PDF files from C#, options for viewing a PDF within your application is much more limited, unless you purchase expensive commercial products, or use COM interop to embed Acrobat Reader into your application.
This article describes an alternate solution, in which the pages in a PDF are converted into images using GhostScript, from where you can then display them in your application.


I Used itextsharp.dll and ghostscript(gsdll32.dll)

Step 1:- Add reference of itextsharp.dll from  given source code dll's folder.

Step 2:- Copy gsdll32.dll from given source code dll's folder and paste dll (gsdll32.dll) into following path.

if you are using x86 based operating system then gsdll32.dll file should be on (C:\Windows\System32)
C:\Windows\System32\gsdll32.dll
if you are using x64 based operating system then gsdll32.dll file should be on (C:\Windows\SysWOW64) 

C:\Windows\SysWOW64\ gsdll32.dll



Defining conversion settings

The Pdf2ImageSettings class allows you to customize various properties of the output image. The following properties are available:

  • AntiAliasMode - specifies the antialiasing level between Low, Medium and High. This internally will set the dTextAlphaBits and dGraphicsAlphaBits GhostScript switches to appropriate values.
  • Dpi - dots per inch. Internally sets the r switch. This property is not used if a paper size is set.
  • GridFitMode - controls the text readability mode. Internally sets the dGridFitTT switch.
  • ImageFormat - specifies the output image format. Internally sets the sDEVICE switch.
  • PaperSize - specifies a paper size from one of the standard sizes supported by GhostScript.
  • TrimMode - specifies how the image should be sized. Your milage may vary if you try and use the paper size option. Internally sets either the dFIXEDMEDIA and sPAPERSIZE or the dUseCropBox or the dUseTrimBox switches.

Converting the PDF

To convert a PDF file into a series of images, use the Pdf2Image class. The following properties and methods are offered:

  • ConvertPdfPageToImage - converts a given page in the PDF into an image which is saved to disk
  • GetImage - converts a page in the PDF into an image and returns the image
  • GetImages - converts a range of pages into the PDF into images and returns an image array
  • PageCount - returns the number of pages in the source PDF
  • PdfFilename - returns or sets the filename of the PDF document to convert
  • PdfPassword - returns or sets the password of the PDF document to convert
  • Settings - returns or sets the settings object described above

Usage when running as a service

  • Sometimes problems may occure when running pdfaPilot/pdfToolbox as a service with Microsoft Office. Automation is not supported by Microsoft officially, but the following workaround was successful in most cases:
    • Create the following folder (with appropriate permissions) on your system:
      • Windows x64: C:\Windows\SysWOW64\config\systemprofile\Desktop
      • Windows x86: C:\Windows\System32\config\systemprofile\Desktop


Download Source Code Here...