Getting Started with PDF Images Extractor for .NET |
ExpertPdf PDF Images Extractor extracts the images embedded in PDF documents in any .NET application: console and Windows Forms applications, services, ASP.NET web sites. The images are saved as image files, and each one is also given to your code through an event, with the number of its page.
The main class is PdfImgExtract, in the eppdfimgextract.dll assembly.
Extract the images of all the pages of a document or of a range of pages, from a file, a stream or a byte array.
Save them as PNG, JPEG, BMP, GIF or TIFF files, with the file name prefix you choose.
Receive every image in memory, as a System.Drawing.Bitmap, 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 (new in version 10).
Skip the small images (icons, bullets) with a minimum width and height, and the repeated images (a logo on every page) (new in version 10).
Open password protected documents; read the document information and the page count.
.NET Framework 4.0 or later. A build for .NET Framework 2.0 - 3.5 applications is also included.
.NET Core 2.0 or later and .NET 5 to .NET 11, through the .NET Standard 2.0 build of the NuGet package.
Windows 10, Windows 11, Windows Server 2016 or later, 32-bit or 64-bit.
The eptools.dep file, the PDF engine of the library, in the same folder as eppdfimgextract.dll at run time. The NuGet package copies it to the output folder of your application.
The easiest way to install the library is the ExpertPdf.PdfImgExtract package (https://www.nuget.org/packages/ExpertPdf.PdfImgExtract/). It works for .NET Framework, .NET Core and .NET 5 to .NET 11 projects. In Visual Studio open the NuGet Package Manager and install the package, or run:
dotnet add package ExpertPdf.PdfImgExtract
The package is for AnyCPU and x86 applications. ExpertPdf.PdfImgExtract.x64 (https://www.nuget.org/packages/ExpertPdf.PdfImgExtract.x64/) is the x64 optimized build, for applications that target the x64 platform.
The download contains:
Bin\.NET_4.0\eppdfimgextract.dll - the library for .NET Framework 4.0 and later
Bin\eppdfimgextract.dll - the library for .NET Framework 2.0 - 3.5
eptools.dep next to each of them - the PDF engine
Samples (.NET Framework 4.6.2) and Samples_NET_10 (.NET 10 and .NET 11, NuGet package) - C# and VB.NET samples
Add a reference to eppdfimgextract.dll and copy eptools.dep from the same folder to the output folder of your application. In a project file you can add it as a linked file that is copied to the output folder:
<None Include="..\Bin\.NET_4.0\eptools.dep" Link="eptools.dep" CopyToOutputDirectory="PreserveNewest" />
If eptools.dep has to be in another folder, set its full path with the PdfToolsFullPath property.
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). The ImageExtractedEvent event is raised for every image, before it is saved:
using System.Drawing.Imaging; using ExpertPdf; PdfImgExtract extractor = new PdfImgExtract(); // extractor.LicenseKey = "your license key"; extractor.ExtractPath = @"C:\Documents\images"; // an existing folder extractor.ExtractPrefix = "image_"; extractor.ExtractImageFormat = ImageFormat.Png; extractor.ImageExtractedEvent += delegate (ImageExtractedEventArgs e) { Console.WriteLine("page {0}: {1} x {2}", e.PageNumber, e.ExtractedBitmap.Width, e.ExtractedBitmap.Height); }; extractor.ExtractImages(@"C:\Documents\report.pdf", true);
StartPageNumber and EndPageNumber - the pages to extract the images from. The first page is 1; an end page of 0 means the last page of the document.
ExtractImageFormat, ExtractPath and ExtractPrefix - the format, the folder and the file name prefix of the image files.
MinimumWidth and MinimumHeight - a smaller image is skipped: no event, no file, not in the list (0 by default: every image).
SkipDuplicateImages - an image with the same size and the same pixels as an image extracted before is skipped (false by default).
UserPassword - the password of a protected document.
GetPageCount(String) - the number of pages of a document; DocumentInformation - the title, author, subject, keywords, dates and page count, after an extraction.
PdfToolsTimeout - the maximum time of an extraction, in seconds (600 by default).
With false as the second argument of ExtractImages(Stream, Boolean) the extractor saves nothing: your event handler receives every image in the ExtractedBitmap field and the page number in PageNumber, and decides what to keep. In this mode the extractor does not use the bitmap after the event, so the handler can dispose it.
byte[] pdfBytes = File.ReadAllBytes(@"C:\Documents\report.pdf"); // or from a database, an upload... PdfImgExtract extractor = new PdfImgExtract(); extractor.ImageExtractedEvent += delegate (ImageExtractedEventArgs e) { // keep the photos and charts, skip the small images (logos, icons) if (e.ExtractedBitmap.Width >= 100 || e.ExtractedBitmap.Height >= 100) { string file = string.Format(@"C:\Documents\photos\page{0}-{1}x{2}.jpg", e.PageNumber, e.ExtractedBitmap.Width, e.ExtractedBitmap.Height); e.ExtractedBitmap.Save(file, ImageFormat.Jpeg); } e.ExtractedBitmap.Dispose(); }; extractor.ExtractImages(new MemoryStream(pdfBytes), false);
When the extractor saves the images itself (true as the second argument), it saves the bitmap after the event: do not dispose it in the handler.
ExtractImagesToMemory(String) returns all the images in a list of ExtractedImage: the Image, 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; do not dispose the bitmaps in the handler when you use the list.
PdfImgExtract extractor = new PdfImgExtract(); extractor.MinimumWidth = 100; // no icons and bullets extractor.MinimumHeight = 100; extractor.SkipDuplicateImages = true; // the logo of every page only once List<ExtractedImage> images = extractor.ExtractImagesToMemory(@"C:\Documents\report.pdf"); foreach (ExtractedImage image in images) { string file = string.Format(@"C:\Documents\photos\page{0}-{1}.png", image.PageNumber, image.IndexOnPage); image.Image.Save(file, ImageFormat.Png); image.Image.Dispose(); }
The download has each sample in C# and VB.NET, for .NET Framework 4.6.2 (Samples folder) and for .NET 10 / .NET 11 (Samples_NET_10 folder). 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 - receives the images of a document read from memory through the event and keeps only the large ones.
The ExtractPdfImages sample is a complete command line tool:
ExtractPdfImages [/pdf:file.pdf] [/out:folder] [/imgformat:png|jpg|bmp|gif|tiff]
[/start:n] [/end:n] [/prefix:name] [/userpswd:password]Examples:
ExtractPdfImages /pdf:sample.pdf /imgformat:jpg /start:4 /end:4 ExtractPdfImages /pdf:"C:\My Documents\protected.pdf" /userpswd:open
Version 10 is part of the ExpertPdf 22.0 release and needs a version 10 license key.
All the images in one list: ExtractImagesToMemory(String).
Minimum image size: the MinimumWidth and MinimumHeight properties.
Repeated images extracted once: the SkipDuplicateImages property.
New samples for .NET Framework, .NET 10 and .NET 11, in C# and VB.NET: a command line tool and a sample that works in memory.
A license key is emailed to you when you purchase the product. Set it with the LicenseKey property before you call the extractor. Without a license key the extractor works the same and draws an evaluation notice on the images that are more than 100 pixels wide.
Support: office@html-to-pdf.net, https://www.html-to-pdf.info