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

  • SEARCH
  • Home
  • 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 6867449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:18:41+00:00 2026-05-27T03:18:41+00:00

I am trying to extract images from a PDF file. I found an example

  • 0

I am trying to extract images from a PDF file. I found an example on the web, that worked fine:

    PdfReader reader;

    File file = new File("example.pdf");
    reader = new PdfReader(file.getAbsolutePath());
    for (int i = 0; i < reader.getXrefSize(); i++) {
        PdfObject pdfobj = reader.getPdfObject(i);
        if (pdfobj == null || !pdfobj.isStream()) {
            continue;
        }
        PdfStream stream = (PdfStream) pdfobj;
        PdfObject pdfsubtype = stream.get(PdfName.SUBTYPE);
        if (pdfsubtype != null && pdfsubtype.toString().equals(PdfName.IMAGE.toString())) {
            byte[] img = PdfReader.getStreamBytesRaw((PRStream) stream);
            FileOutputStream out = new FileOutputStream(new File(file.getParentFile(), String.format("%1$05d", i) + ".jpg"));
            out.write(img);
            out.flush();
            out.close();
        }
    }

That gave me all the images, but the images were in the wrong order. My next attempt looked like this:

for (int i = 0; i <= reader.getNumberOfPages(); i++) {
  PdfDictionary d = reader.getPageN(i);
  PdfIndirectReference ir = d.getAsIndirectObject(PdfName.CONTENTS);
  PdfObject o = reader.getPdfObject(ir.getNumber());
  PdfStream stream = (PdfStream) o;
  // rest from example above
}

Although o.isStream() == true, I only get /Length and /Filter and the stream is only about 100 bytes long. No image to be found at all.

My question would be what the correct way would be to get all the images from a PDF file in the correct order.

  • 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-27T03:18:41+00:00Added an answer on May 27, 2026 at 3:18 am

    I found an answer elsewhere, namely the iText mailing list.

    The following code works for me – please note that I switched to PdfBox:

    PDDocument document = null; 
    document = PDDocument.load(inFile); 
    List pages = document.getDocumentCatalog().getAllPages();
    Iterator iter = pages.iterator(); 
    while (iter.hasNext()) {
                PDPage page = (PDPage) iter.next();
                PDResources resources = page.getResources();
                Map pageImages = resources.getImages();
                if (pageImages != null) { 
                    Iterator imageIter = pageImages.keySet().iterator();
                    while (imageIter.hasNext()) {
                        String key = (String) imageIter.next();
                        PDXObjectImage image = (PDXObjectImage) pageImages.get(key);
                        image.write2OutputStream(/* some output stream */);
                    }
                }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to extract images from flash on the following web-site: http://meijer.shoplocal.com/meijer/default.aspx?action=entryflash&storeref=120 I
I am trying to extract all the images from a pdf using itextsharp but
I am trying to extract the images from the given web page. I have
Trying to extract strings that are wrapped in double brackets. For example [[this is
I am trying to extract numbers from a typical scoreboard that you would find
I am trying to extract the palette from a 5-6-5 (16bit) bitmap image that
I am trying to extract images from a document saved as WordML. Some of
I am trying to extract specific content(links, text, images) from an HTML page. Is
I am trying to extract icons images from specific files or directory using Java.
I´m using JAVA and I´m trying to extract or save images files from a

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.