Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to convert PDF page to Image using itextSharp?

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.

like image 410
Max Avatar asked May 26 '15 10:05

Max


People also ask

What is ITextSharp used for?

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.

How do I convert PDF to image in PyPDF2?

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.

How to export (convert) image to PDF using iTextSharp?

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.

How to add bin folder in iTextSharp?

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.

How to convert webpage to PDF in Visual Studio 2010?

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.

How do I open a PDF file in spire?

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.


1 Answers

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();

    }
like image 136
Max Avatar answered Oct 05 '22 17:10

Max