Hi I have been using itextSharp for all pdf related projects in dot.net. I came across a requirement where I need to convert PDF pages to images. I could not find any sample of such a thing. I found that another tool ghostscript is able to do it the problem with that is I am on a shared hosting & I don't think ghostscript will run on server as in my local machine I had to manually copy ghost script dlls to system32 folder which is not possible in a shared hosting.
Itextsharp is an advanced tool library which is used for creating complex pdf repors. itext is used by different techonologies -- Android , . NET, Java and GAE developer use it to enhance their applications with PDF functionality.
PyPDF2 also doesn't have any capabilities to convert a PDF file into an image, which is understandable since it does not use any core PDF libraries. So if you want to convert your PDF to an image file, the best you can do is extract text and write it to an image file.
In this article I will explain with an example, how to export (convert) Image to PDF using iTextSharp in ASP.Net with C# and VB.Net. The Image file will be first uploaded using FileUpload control and saved into a Folder (Directory), then the Image file will be added into the iTextSharp PDF document and ultimately downloaded as PDF file in ASP.Net.
To Add a Bin Folder -> Right Click on the website -> Add ASP.NET Folder -> Bin. Now we will have to add the ItextSharp dll that we have downloaded in the earlier step.
Converting the webpage to PDF. Now we have to create an empty website in Visual Studio 2010. Now we will have to add a Bin Folder in ASP.NET website. To Add a Bin Folder -> Right Click on the website -> Add ASP.NET Folder -> Bin. Now we will have to add the ItextSharp dll that we have downloaded in the earlier step.
To open a document the Spire.PDF library contains a PdfDocument class, that allows loading PDF documents in many formats, stream, byte, and so on. Here I use the file source path: Iterate through the PDF document pages and save it as an image.
Ok I searched all over and found out that there is a nuget package for Ghost Script, so problem for me was solved by going to package manager console and adding ghost script to fresh project (I created a fresh project since the old one had all kinds of reference to win32 ghostscript dlls) by "PM> Install-Package Ghostscript.NET". So the answer to my question is: 1.> itextSharp cannot directly convert PDF pages to image. 2.> The "Ghostscript.NET 1.2.0" does it quite easily. Following is a code example.
public void LoadImage(string InputPDFFile,int PageNumber)
{
string outImageName = Path.GetFileNameWithoutExtension(InputPDFFile);
outImageName = outImageName+"_"+PageNumber.ToString() + "_.png";
GhostscriptPngDevice dev = new GhostscriptPngDevice(GhostscriptPngDeviceType.Png256);
dev.GraphicsAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
dev.TextAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
dev.ResolutionXY = new GhostscriptImageDeviceResolution(290, 290);
dev.InputFiles.Add(InputPDFFile);
dev.Pdf.FirstPage = PageNumber;
dev.Pdf.LastPage = PageNumber;
dev.CustomSwitches.Add("-dDOINTERPOLATE");
dev.OutputPath = Server.MapPath(@"~/tempImages/" + outImageName);
dev.Process();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With