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 468801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:43:06+00:00 2026-05-12T23:43:06+00:00

I wanted to know how to grab an image element in a PDF file

  • 0

I wanted to know how to grab an image element in a PDF file using c#. I know how to add the elements to a PDF file I just need to know how to access the image elements.

I’m also using iTextSharp.

  • 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-12T23:43:06+00:00Added an answer on May 12, 2026 at 11:43 pm

    I believe you can do this with itextsharp.

    I had this code stored on my machine, but never used it. I got it off a forum and it’s not tested, but I’m sure you could make it work.

    using iTextSharp.text;
    using iTextSharp.text.pdf;
    
    #region ExtractImagesFromPDF
            public static void ExtractImagesFromPDF(string sourcePdf, string outputPath)
            {
                // NOTE:  This will only get the first image it finds per page.
                PdfReader pdf = new PdfReader(sourcePdf);
                RandomAccessFileOrArray raf = new iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdf);
    
                try
                {
                    for (int pageNumber = 1; pageNumber <= pdf.NumberOfPages; pageNumber++)
                    {
                        PdfDictionary pg = pdf.GetPageN(pageNumber);
                        PdfDictionary res =
                          (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));
                        PdfDictionary xobj =
                          (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
                        if (xobj != null)
                        {
                            foreach (PdfName name in xobj.Keys)
                            {
                                PdfObject obj = xobj.Get(name);
                                if (obj.IsIndirect())
                                {
                                    PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj);
                                    PdfName type =
                                      (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));
                                    if (PdfName.IMAGE.Equals(type))
                                    {
    
                                        int XrefIndex = Convert.ToInt32(((PRIndirectReference)obj).Number.ToString(System.Globalization.CultureInfo.InvariantCulture));
                                        PdfObject pdfObj = pdf.GetPdfObject(XrefIndex);
                                        PdfStream pdfStrem = (PdfStream)pdfObj;
                                        byte[] bytes = PdfReader.GetStreamBytesRaw((PRStream)pdfStrem);
                                        if ((bytes != null))
                                        {
                                            using (System.IO.MemoryStream memStream = new System.IO.MemoryStream(bytes))
                                            {
                                                memStream.Position = 0;
                                                System.Drawing.Image img = System.Drawing.Image.FromStream(memStream);
                                                // must save the file while stream is open.
                                                if (!Directory.Exists(outputPath))
                                                    Directory.CreateDirectory(outputPath);
    
                                                string path = Path.Combine(outputPath, String.Format(@"{0}.jpg", pageNumber));
                                                System.Drawing.Imaging.EncoderParameters parms = new System.Drawing.Imaging.EncoderParameters(1);
                                                parms.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 0);
    // GetImageEncoder is found below this method
                                                System.Drawing.Imaging.ImageCodecInfo jpegEncoder = GetImageEncoder("JPEG");
                                                img.Save(path, jpegEncoder, parms);
                                                break;
    
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
    
                catch
                {
                    throw;
                }
                finally
                {
                    pdf.Close();
                }
    
    
            }
            #endregion
    
           #region GetImageEncoder
            public static System.Drawing.Imaging.ImageCodecInfo GetImageEncoder(string imageType)
            {
                imageType = imageType.ToUpperInvariant();
    
    
    
                foreach (ImageCodecInfo info in ImageCodecInfo.GetImageEncoders())
                {
                    if (info.FormatDescription == imageType)
                    {
                        return info;
                    }
                }
    
                return null;
            }
            #endregion
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just wanted to know your experience with using resharper. We have a very
I need to develop a file indexing application in python and wanted to know
Just wanted to know if it is possible to create a hidden window using
I wanted to know if it is possible to create an image using html5.
Just wanted to know if it is possible to disallow the whole site for
I just wanted to know what's the difference between clear() and str(); For example:
I'm looking at using OpenID for my authentication scheme and wanted to know what
So I wanted to know if there was a way to grab a specific
Just wanted to know that does Visual web Developer 2010 [Free tool] contains MVC
Just wanted to know what the best way of transitioning between pages in flash.

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.