sidezr
sidezr

Reputation: 163

How to draw a PDF page into a System.Drawing.Image using iTextSharp?

I am kind of new to using iTextSharp. I have a repository of PDF documents at work that I need to copy into images (one image per page) and process them. These PDF have text, raster images and vector images, and possibly, more stuff in them. I am not very familiar with PDF structure, and I would rather use iTextSharp before having to buy some PDF package.

I have done work on extracting text and raster images from each PDF document using iTextSharp on C#, but trying to render them into an image gives mixed results, and if there are vector graphics, there is nothing I can do to extract and render them easily.

I apologize for my lack of knowledge on PDF internal works and iTextSharp, but is there a way, using iTextSharp, to draw each PDF page to a System.Drawing.Image object in the same way as they appear, say, on a PDF reader program? It would be great if there was a method such as System.Drawing.Bitmap RenderPage(PdfReader reader, int iPage).

Thanks to all. Any help will be appreciated.

Upvotes: 0

Views: 3202

Answers (1)

sidezr
sidezr

Reputation: 163

I found a way to do it using another library. I used Ghostscript.NET.

Ghostscript.NET is a .NET wrapper for Ghostscript library's native code, so, it probably won't work on Windows RT devices because it requires the actual native code DLL to work.

Instructions to install Ghostscript.NET via NuGet package are in this website:

https://www.nuget.org/packages/Ghostscript.NET/

Once the package is installed, you need the Ghostscript native code DLL. To obtain it, install Ghostscript first from the link bellow, then, find gsdll32.dll in the installation directory and copy it in a safe place:

http://www.ghostscript.com/download/gsdnld.html

This DLL is for 32 bits. If you are programming for 64 bits, you should download and install the 64 bits version. After obtaining the DLL, you can uninstall Ghostscript since the DLL is stand alone.

Finally, I wrote the following code (assumes that Ghostscript native DLL is on the same path as the application) to render a PDF's pages into System.Drawing.Images:

string sDLLPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath),
    "gsdll32.dll");
GhostscriptVersionInfo gvi = new GhostscriptVersionInfo(sDLLPath);
using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer())
{
    rasterizer.Open("sample.pdf", gvi, false);

    int dpi_x = 96;
    int dpi_y = 96;
    for (int i = 1; i <= rasterizer.PageCount; i++)
    {
        Image img = rasterizer.GetPage(dpi_x, dpi_y, i);
        // System.Drawing.Image obtained. Now it can be used at will.
        // Simply save it to storage as an example.
        img.Save(Path.Combine("C:\\Temp", "page_" + i + ".png")),
            System.Drawing.Imaging.ImageFormat.Png);
    }
}

Upvotes: 1

Related Questions