Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4614454
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:42:08+00:00 2026-05-22T01:42:08+00:00

I am trying to extract all the images from a pdf using itextsharp but

  • 0

I am trying to extract all the images from a pdf using itextsharp but can’t seem to overcome this one hurdle.

The error occures on the line System.Drawing.Image ImgPDF = System.Drawing.Image.FromStream(MS); giving an error of “Parameter is not valid”.

I think it works when the image is a bitmap but not of any other format.

I have this following code – sorry for the length;

    private void Form1_Load(object sender, EventArgs e)
    {
        FileStream fs = File.OpenRead(@"reader.pdf");
        byte[] data = new byte[fs.Length];
        fs.Read(data, 0, (int)fs.Length);

        List<System.Drawing.Image> ImgList = new List<System.Drawing.Image>();

        iTextSharp.text.pdf.RandomAccessFileOrArray RAFObj = null;
        iTextSharp.text.pdf.PdfReader PDFReaderObj = null;
        iTextSharp.text.pdf.PdfObject PDFObj = null;
        iTextSharp.text.pdf.PdfStream PDFStremObj = null;

        try
        {
            RAFObj = new iTextSharp.text.pdf.RandomAccessFileOrArray(data);
            PDFReaderObj = new iTextSharp.text.pdf.PdfReader(RAFObj, null);

            for (int i = 0; i <= PDFReaderObj.XrefSize - 1; i++)
            {
                PDFObj = PDFReaderObj.GetPdfObject(i);

                if ((PDFObj != null) && PDFObj.IsStream())
                {
                    PDFStremObj = (iTextSharp.text.pdf.PdfStream)PDFObj;
                    iTextSharp.text.pdf.PdfObject subtype = PDFStremObj.Get(iTextSharp.text.pdf.PdfName.SUBTYPE);

                    if ((subtype != null) && subtype.ToString() == iTextSharp.text.pdf.PdfName.IMAGE.ToString())
                    {
                        byte[] bytes = iTextSharp.text.pdf.PdfReader.GetStreamBytesRaw((iTextSharp.text.pdf.PRStream)PDFStremObj);

                        if ((bytes != null))
                        {
                            try
                            {
                                System.IO.MemoryStream MS = new System.IO.MemoryStream(bytes);

                                MS.Position = 0;
                                System.Drawing.Image ImgPDF = System.Drawing.Image.FromStream(MS);

                                ImgList.Add(ImgPDF);

                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                }
            }
            PDFReaderObj.Close();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }



    } //Form1_Load
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:42:09+00:00Added an answer on May 22, 2026 at 1:42 am

    I have used this library in the past without any problems.

    http://www.winnovative-software.com/PdfImgExtractor.aspx

    private void btnExtractImages_Click(object sender, EventArgs e)
    {
        if (pdfFileTextBox.Text.Trim().Equals(String.Empty))
        {
            MessageBox.Show("Please choose a source PDF file", "Choose PDF file", MessageBoxButtons.OK);
            return;
        }
    
        // the source pdf file
        string pdfFileName = pdfFileTextBox.Text.Trim();
    
        // start page number
        int startPageNumber = int.Parse(textBoxStartPage.Text.Trim());
        // end page number
        // when it is 0 the extraction will continue up to the end of document
        int endPageNumber = 0;
        if (textBoxEndPage.Text.Trim() != String.Empty)
            endPageNumber = int.Parse(textBoxEndPage.Text.Trim());
    
        // create the PDF images extractor object
        PdfImagesExtractor pdfImagesExtractor = new PdfImagesExtractor();
    
        pdfImagesExtractor.LicenseKey = "31FAUEJHUEBQRl5AUENBXkFCXklJSUlQQA==";
    
        // the demo output directory
        string outputDirectory = Path.Combine(Application.StartupPath, @"DemoFiles\Output");
    
        Cursor = Cursors.WaitCursor;
    
        // set the handler to be called when an image was extracted
        pdfImagesExtractor.ImageExtractedEvent += pdfImagesExtractor_ImageExtractedEvent;
    
        try
        {
            // start images counting
            imageIndex = 0;
    
            // call the images extractor to raise the ImageExtractedEvent event when an images is extracted from a PDF page
            // the pdfImagesExtractor_ImageExtractedEvent handler below will be executed for each extracted image
            pdfImagesExtractor.ExtractImagesInEvent(pdfFileName, startPageNumber, endPageNumber);
    
            // Alternatively you can use the ExtractImages() and ExtractImagesToFile() methods
            // to extracted the images from a PDF document in memory or to image files in a directory
    
            // uncomment the line below to extract the images to an array of ExtractedImage objects
            //ExtractedImage[] pdfPageImages = pdfImagesExtractor.ExtractImages(pdfFileName, startPageNumber, endPageNumber);
    
            // uncomment the lines below to extract the images to image files in a directory
            //string outputDirectory = System.IO.Path.Combine(Application.StartupPath, @"DemoFiles\Output");
            //pdfImagesExtractor.ExtractImagesToFile(pdfFileName, startPageNumber, endPageNumber, outputDirectory, "pdfimage");
        }
        catch (Exception ex)
        {
            // The extraction failed
            MessageBox.Show(String.Format("An error occurred. {0}", ex.Message), "Error");
            return;
        }
        finally
        {
            // uninstall the event handler
            pdfImagesExtractor.ImageExtractedEvent -= pdfImagesExtractor_ImageExtractedEvent;
    
            Cursor = Cursors.Arrow;
        }
    
        try
        {
            System.Diagnostics.Process.Start(outputDirectory);
        }
        catch (Exception ex)
        {
            MessageBox.Show(string.Format("Cannot open output folder. {0}", ex.Message));
            return;
        }
    }
    
    /// <summary>
    /// The ImageExtractedEvent event handler called after an image was extracted from a PDF page.
    /// The event is raised when the ExtractImagesInEvent() method is used
    /// </summary>
    /// <param name="args">The handler argument containing the extracted image and the PDF page number</param>
    void pdfImagesExtractor_ImageExtractedEvent(ImageExtractedEventArgs args)
    {
        // get the image object and page number from even handler argument
        Image pdfPageImageObj = args.ExtractedImage.ImageObject;
        int pageNumber = args.ExtractedImage.PageNumber;
    
        // save the extracted image to a PNG file
        string outputPageImage = Path.Combine(Application.StartupPath, @"DemoFiles\Output", 
            "pdfimage_" + pageNumber.ToString() + "_" + imageIndex++ + ".png");
        pdfPageImageObj.Save(outputPageImage, ImageFormat.Png);
    
        args.ExtractedImage.Dispose();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to extract all matches from a EBML definition, which is something like
Im trying to extract a line from wget's result but having trouble with it.
I am attempting to extract all data from a file with one regex expression.
I am trying to extract a table of values from an excel (2003) spreadsheet
I'm trying to extract the domain name from a string in C#. You don't
I'm trying to write a log parsing script to extract failed events. I can
I'm trying to parse a html page and extract 2 values from a table
I'm trying something new, I would normally do this in C# or VB. But
I am trying to extract an image frame from a video taken from iPhone
I am trying to extract publisher information from a string. It comes in various

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.