Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert pdf to jpeg using a free c# solution [closed]

Tags:

c#

image

pdf

jpeg

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

I have searched a lot of information but seems that I don't get anything 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 a strange encoding.

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

like image 775
FrioneL Avatar asked Jul 21 '11 11:07

FrioneL


People also ask

How do I convert a PDF to a JPG for free?

Convert PDF to JPG. Drag and drop a PDF, then convert to JPG, PNG or TIFF file formats. Select a PDF, then convert to JPG, PNG or TIFF file formats. Drag and drop a PDF file to convert it into a JPG file format.

Can I change a PDF to a JPG?

Step 1: Open your PDF file in preview. Step 2: Select the page you want to convert to JPG, and go to File > Export… Step 3: A dialogue box will open. Under Format, select JPEG.

How do I convert a high quality PDF to a JPEG?

Open your PDF in Adobe Acrobat Pro and choose file. Export it to the new file format by going to the right pane and choosing “Export PDF” tool. Or, go to the menu and select “File” > “Export to” > “Image.” Choose image format type (e.g., JPG file, TIFF, etc.).

How do I make a multiple page PDF into one JPEG?

Drag and drop your file in the PDF to JPG converter. Select 'Convert entire pages' or 'Extract single images'. Click on 'Choose option' and wait for the process to complete. Download the converted files as single JPG files, or collectively in a ZIP file.


2 Answers

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

like image 87
Vijay Gill Avatar answered Sep 17 '22 14:09

Vijay Gill


This is how I did it with PDFLibNet:

public void ConvertPDFtoHojas(string filename, String dirOut) {     PDFLibNet.PDFWrapper _pdfDoc = new PDFLibNet.PDFWrapper();     _pdfDoc.LoadPDF(filename);      for (int i = 0; i < _pdfDoc.PageCount; i++)     {          Image img = RenderPage(_pdfDoc, i);          img.Save(Path.Combine(dirOut, string.Format("{0}{1}.jpg", i,DateTime.Now.ToString("mmss"))));      }     _pdfDoc.Dispose();     return; } public  Image RenderPage(PDFLibNet.PDFWrapper doc, int page) {     doc.CurrentPage = page + 1;     doc.CurrentX = 0;     doc.CurrentY = 0;      doc.RenderPage(IntPtr.Zero);          // create an image to draw the page into         var buffer = new Bitmap(doc.PageWidth, doc.PageHeight);         doc.ClientBounds = new Rectangle(0, 0, doc.PageWidth, doc.PageHeight);         using (var g = Graphics.FromImage(buffer))         {             var hdc = g.GetHdc();             try             {                 doc.DrawPageHDC(hdc);             }             finally             {                 g.ReleaseHdc();             }         }         return buffer;  } 
like image 25
Jorge Alberto Chavez Barrera Avatar answered Sep 16 '22 14:09

Jorge Alberto Chavez Barrera