Click or drag to resize

Extracting Images from PDF

The image extraction part of ExpertPdf Anywhere extracts the images embedded in PDF documents on Windows, Linux and macOS. The images are saved as image files, returned in a list, or given to your code through an event, with the number of their page. Each image is the bytes of an image file in the format you choose.

The main class is PdfImgExtract, in the ExpertPdf.Anywhere namespace. The images are read by the native PDF tool of the library, ExpertPdf.Tools (ExpertPdf.Tools.exe on Windows), which the extractor runs as a separate process. The extractor is disposable: a using statement, or Close, releases the document read by the last call.

Features
  • Extract the images of all the pages of a document or of a range of pages, from a file or a stream (a MemoryStream for a document in memory).

  • Save them as PNG, JPEG, BMP or TIFF files, with the file name prefix you choose.

  • Receive every image in memory, as the bytes of an image file, with the number of its page - to filter, index or store the images yourself.

  • Get all the images in one list, with the page number and position of each image.

  • Skip the small images (icons, bullets) with a minimum width and height, and the repeated images (a logo on every page).

  • Open password protected documents; read the document information and the page count.

Installation

The extractor is part of the ExpertPdf.Anywhere package. The PDF tool comes with the native package of your platform (ExpertPdf.Anywhere.Native.<rid>) or with a platform package (ExpertPdf.Anywhere.Windows, .Linux, .Linux.Arm64, .MacOS), which copies it next to your application when it builds and when it is published. See Installation.

dotnet add package ExpertPdf.Anywhere.Windows

The tool is found next to the application. If it is in another folder, set its full path, or the folder that holds it, with the PdfToolsFullPath property.

Getting started

Create a PdfImgExtract, set the options and call ExtractImages(String, Boolean). With true as the second argument the images are saved in the ExtractPath folder, as <ExtractPrefix><page number>_<image index>.<format> (the index counts from 0), in the ExtractImageFormat format (ImageFormat: Png, Jpeg, Bmp or Tiff). The ImageExtractedEvent event is raised for every image:

using ExpertPdf.Anywhere;

PdfImgExtract extractor = new PdfImgExtract();

extractor.ExtractPath = "images";             // an existing folder
extractor.ExtractPrefix = "image_";
extractor.ExtractImageFormat = ImageFormat.Png;

extractor.ImageExtractedEvent += delegate (ImageExtractedEventArgs e)
{
    Console.WriteLine("page {0}: {1} bytes", e.PageNumber, e.ExtractedBitmap.Length);
};

extractor.ExtractImages("report.pdf", true);
Extraction options
Images in memory

With false as the second argument of ExtractImages(Stream, Boolean) the extractor saves nothing: your event handler receives every image in the ExtractedBitmap field - the bytes of an image file in the ExtractImageFormat format - and the page number in PageNumber, and decides what to keep.

using ExpertPdf.Anywhere;

// or from a database, an upload...
byte[] pdfBytes = File.ReadAllBytes("report.pdf");

PdfImgExtract extractor = new PdfImgExtract();
extractor.ExtractImageFormat = ImageFormat.Jpeg;
// keep the photos and charts, skip the small images (logos, icons)
extractor.MinimumWidth = 100;
extractor.MinimumHeight = 100;

int count = 0;
extractor.ImageExtractedEvent += delegate (ImageExtractedEventArgs e)
{
    count++;
    string file = string.Format("page{0}-{1}.jpg", e.PageNumber, count);
    File.WriteAllBytes(Path.Combine("photos", file), e.ExtractedBitmap);
};

extractor.ExtractImages(new MemoryStream(pdfBytes), false);

ExtractImagesToMemory(String) returns all the images in a list of ExtractedImage: the Image bytes, its PageNumber, its Index among all the images of the document (the number in the names of the files the extractor saves) and its IndexOnPage. The event is raised for these images too.

PdfImgExtract extractor = new PdfImgExtract();
extractor.MinimumWidth = 100;          // no icons and bullets
extractor.MinimumHeight = 100;
extractor.SkipDuplicateImages = true;  // the logo of every page only once
extractor.ExtractImageFormat = ImageFormat.Png;

List<ExtractedImage> images = extractor.ExtractImagesToMemory("report.pdf");
foreach (ExtractedImage image in images)
{
    string file = string.Format("page{0}-{1}.png", image.PageNumber,
        image.IndexOnPage);
    File.WriteAllBytes(Path.Combine("photos", file), image.Image);
}
Samples

The samples download has these console applications, in C# and VB.NET, for Windows, Linux and macOS. They work on the sample.pdf document that comes with them; sample-protected.pdf is the same document protected with the password "open".

  • ExtractPdfImages - a command line tool with all the options. Without arguments it saves the images of sample.pdf as PNG files.

  • ExtractImagesInMemory - extracts the images of a document read from memory into a list, keeps only the large ones and each repeated image once.

PDF Images Extractor command line tool

The ExtractPdfImages sample is a complete command line tool:

ExtractPdfImages [/pdf:file.pdf] [/out:folder] [/imgformat:png|jpg|bmp|tiff]
                 [/start:n] [/end:n] [/prefix:name] [/userpswd:password]
                 [/minwidth:n] [/minheight:n] [/nodup]

Examples:

ExtractPdfImages /pdf:sample.pdf /imgformat:jpg /start:4 /end:4
ExtractPdfImages /pdf:sample.pdf /minwidth:100 /minheight:100 /nodup
ExtractPdfImages /pdf:sample-protected.pdf /userpswd:open
Licensing

Set LicenseKey once when the application starts, or the LicenseKey property before you call it. Without a license key the extractor works the same and draws an evaluation notice on the images that are more than 100 pixels wide. See Licensing.

See Also