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 x64 based operating system then gsdll32.dll file should be on (C:\Windows\SysWOW64)
Defining conversion settings
The
Converting the PDF
To convert a PDF file into a series of images, use the
Download Source Code Here...
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.
if you are using x86 based operating system then gsdll32.dll file should be on (C:\Windows\System32)
C:\Windows\System32\gsdll32.dll
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 thedTextAlphaBits
anddGraphicsAlphaBits
GhostScript switches to appropriate values.Dpi
- dots per inch. Internally sets ther
switch. This property is not used if a paper size is set.GridFitMode
- controls the text readability mode. Internally sets thedGridFitTT
switch.ImageFormat
- specifies the output image format. Internally sets thesDEVICE
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 thedFIXEDMEDIA
andsPAPERSIZE
or thedUseCropBox
or thedUseTrimBox
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 diskGetImage
- converts a page in the PDF into an image and returns the imageGetImages
- converts a range of pages into the PDF into images and returns an image arrayPageCount
- returns the number of pages in the source PDFPdfFilename
- returns or sets the filename of the PDF document to convertPdfPassword
- returns or sets the password of the PDF document to convertSettings
- 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...