Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you extract an image from a pdf file using C# [closed]

Tags:

c#

image

pdf

bitmap

How can I get the image from a .pdf file into a System.Drawing.Bitmap?

like image 354
Matt Warren Avatar asked Apr 01 '09 23:04

Matt Warren


People also ask

How do I extract objects from a PDF?

Use Adobe Acrobat Professional. To extract information from a PDF in Acrobat DC, choose Tools > Export PDF and select an option. To extract text, export the PDF to a Word format or rich text format, and choose from several advanced options that include: Retain Flowing Text.


1 Answers

You may want to try Docotic.Pdf library for the task.

Here is a sample that shows how to create System.Drawing.Bitmap from an image in a PDF file:

static void GetImagesFromPdfAsBitmaps()
{
    string pathToPdf = "";
    using (PdfDocument pdf = new PdfDocument(pathToPdf))
    {
        for (int i = 0; i < pdf.Images.Count; i++)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                pdf.Images[i].Save(ms);

                // don't forget to rewind stream
                ms.Position = 0;

                System.Drawing.Image bitmap = System.Drawing.Bitmap.FromStream(ms);
                // ... use the bitmap and then dispose it
                bitmap.Dispose();
            }
        }
    }
}

The library can also save images to files. The library doesn't resample images (i.e. you'll get exactly the same image as in PDF)

Disclaimer: I work for Bit Miracle, vendor of the library.

like image 85
Bobrovsky Avatar answered Oct 26 '22 18:10

Bobrovsky