AVAILABLE NOW: Spring 2025 Release

C# and Java Code to Extract Images Embedded in a PDF

By Apryse | 2025 Apr 18

Sanity Image
Read time

6 min

PDF files are ubiquitous in the world of business. They are used to store documents, images, and other data. In the past, people have asked if there is an easy way to extract graphics such as a chart or photo from a PDF file. If you're looking to get all of the images from a PDF file or have hundreds or more PDF files to process, then the answer is to use Apryse.

Extracting images that are embedded in a PDF file is easy with Apryse. Below are C# and Java code samples that use Apryse to extract images from a PDF file.

C# code to extract images embedded in a PDF

Copied to clipboard

using pdftron;  

using pdftron.PDF;  

namespace ConsoleApp1  

{  

    internal class Program  

    {  

        static void Main(string[] args)  

        {  

            PDFNet.Initialize(PDFTronLicense.License);  

  

            using (PDFDoc doc = new PDFDoc("samples\\Leadtools.pdf"))  

            {  

                doc.InitSecurityHandler();  

  

                int imageCounter = 1;  

  

                for (PageIterator itr = doc.GetPageIterator(); itr.HasNext(); itr.Next())  

                {  

                    pdftron.PDF.Page page = itr.Current();  

                    ElementReader reader = new ElementReader();  

                    reader.Begin(page);  

  

                    Element element;  

                    while ((element = reader.Next()) != null)  

                    {  

                        if (element.GetType() == Element.Type.e_image)  

                        {  

                            Image image = new Image(element.GetXObject());  

                            string outputPath = $"image_{imageCounter}";  

                            image.Export(outputPath);  

                            imageCounter++;  

                        }  

                    }  

  

                    reader.End();  

                }  

            }  

  

            PDFNet.Terminate();  

        }  

    }  

}  

Java code to extract images embedded in a PDF

Copied to clipboard

import com.pdftron.common.PDFNetException;  

import com.pdftron.pdf.*;  

public class App {  

public static void main(String[] args) {  

  try{  

  

    PDFNet.initialize(PDFTronLicense.LICENSE);  

    PDFDoc doc = new PDFDoc("src\\samples\\Leadtools.pdf");  

      doc.initSecurityHandler();  

  

      int imageCounter = 1;  

  

      for (PageIterator itr = doc.getPageIterator(); itr.hasNext(); ) {  

        com.pdftron.pdf.Page page = itr.next();  

        ElementReader reader = new ElementReader();  

        reader.begin(page);  

  

        Element element;  

        while ((element = reader.next()) != null) {  

          if (element.getType() == Element.e_image) {  

            Image image = new Image(element.getXObject());  

            String outputPath = "image_" + imageCounter;  

            image.export(outputPath);  

            imageCounter++;  

          }  

        }  

  

        reader.end();  

        reader.close();  

      }  

        

      doc.close();  

    PDFNet.terminate();  

  

  } catch (PDFNetException e) {  

    System.out.println(e.getMessage());  

  }  

}  

}  

With Apryse in your collection of toolkits, there is nothing you cannot do with PDF files.

See For Yourself - Free Evaluation

Copied to clipboard

Download the Apryse SDK for free. It’s fully functional and comes with free chat and email support.

Stay Tuned for More Conversion Samples

Stay tuned for more conversion examples to see how Apryse easily fits into any workflow converting PDF files into other document files or images and back again. Need help in the meantime? Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team.

Sanity Image

Apryse

Share this post

email
linkedIn
twitter