Save pdf to jpeg using c#

FrioneL picture FrioneL · Jul 21, 2011 · Viewed 78.7k times · Source

I need to convert a pdf file into jpeg using c#. And the solution(library) have to be free.

I have searched a lot of information but seems that I dont get nothing clear.

I already tried itextsharp and pdfbox(but this, the pdf2image is only for java, i think) with no success.

I tried to extract the images from the pdf individually, but I have an error of invalid parameters when i try to extract the images... seems that they have an extrange encoding.

Anyone can recommend me any library to save a pdf into a jpeg? Examples will be very apreciated too.

Thanks!

Answer

Vijay Gill picture Vijay Gill · Jul 21, 2011

The library pdfiumviewer might be helpful here. It is also available as nuget.

  1. Create a new winforms app. Add nuget "PdfiumViewer" to it.
  2. This will also add two native dll's named "pdfium.dll" in folders x86 and x64 to your project. Set "Copy to Output Directory" to "Copy Always".
  3. Try out the following code (change paths to suit your setup).

        try
        {
            using (var document = PdfiumViewer.PdfDocument.Load(@"input.pdf"))
            {
                var image = document.Render(0, 300, 300, true);
                image.Save(@"output.png", ImageFormat.Png);
            }
        }
        catch (Exception ex)
        {
            // handle exception here;
        }
    

    Edit 2: Changed code to show that page index is 0 based as pointed out in comment by S.C. below

Edit 1: Updated solution Have you tried pdfsharp?

This link might be helpful