Getting Started with PDF To Image Converter for .NET |
ExpertPdf PDF to Image Converter renders the pages of PDF documents to images in any .NET application: console and Windows Forms applications, services, ASP.NET web sites. The images are returned in memory, saved as image files, or put together in one multi-page TIFF document.
The main class is PdfToImageConverter, in the eppdftoimage.dll assembly.
Convert all the pages of a document or a range of pages, from a file, a stream or a byte array.
Any resolution: low for thumbnails, high for printing or OCR.
Color (RGB) or gray images.
The images in memory, as System.Drawing.Image objects, or saved as PNG, JPEG, BMP, GIF or TIFF files with the file name prefix you choose.
One multi-page TIFF document with all the converted pages, in memory or in a file (new in version 10).
Thumbnails that fit in a box of the size you choose, keeping the aspect ratio of each page (new in version 10).
One page to an image or to the bytes of an image file in one call (new in version 10).
The JPEG quality, the TIFF compression (LZW, none, PackBits, CCITT Group 3 and Group 4) and the PNG compression level of the images (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 eppdftoimage.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.PdfToImage package (https://www.nuget.org/packages/ExpertPdf.PdfToImage/). 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.PdfToImage
The package is for AnyCPU and x86 applications. ExpertPdf.PdfToImage.x64 (https://www.nuget.org/packages/ExpertPdf.PdfToImage.x64/) is the x64 optimized build, for applications that target the x64 platform.
The download contains:
Bin\.NET_4.0\eppdftoimage.dll - the library for .NET Framework 4.0 and later
Bin\eppdftoimage.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 eppdftoimage.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 PdfToImageConverter, set the options and call ConvertToImagesInFolder(String) to save one image file for each page in the ImagesPath folder:
using System.Drawing.Imaging; using ExpertPdf; PdfToImageConverter converter = new PdfToImageConverter(); // converter.LicenseKey = "your license key"; converter.Resolution = 150; // DPI converter.ImagesFormat = ImageFormat.Png; converter.ImagesPath = @"C:\Documents\images"; // an existing folder converter.ImagesPrefix = "page_"; // page_1.png, page_2.png, ... converter.ConvertToImagesInFolder(@"C:\Documents\report.pdf");
The files are numbered from 1 for the first converted page. The default prefix is "pdfimage_" and the default format is PNG.
StartPageNumber and EndPageNumber - the pages to convert. The first page is 1; an end page of 0 means the last page of the document.
Resolution - the resolution in DPI (120 by default). An A4 page is 992 x 1403 pixels at 120 DPI. Use 24 - 72 DPI for thumbnails and 200 - 300 DPI for printing or OCR.
ColorSpace - RGB (the default) or Gray.
ImagesFormat, ImagesPath and ImagesPrefix - the format, the folder and the file name prefix of the image files.
JpegQuality - the quality of the JPEG images, from 0 to 100 (91 by default), and TiffCompression - the compression of the TIFF images (Lzw by default). Ccitt3 and Ccitt4 give black and white images, the smallest files for text documents and fax services.
PngCompressionLevel - the compression level of the PNG images, from 0 (no compression, the fastest) to 9 (the smallest files). PNG compression is lossless: every level gives the same pixels, a higher level a smaller file in more time. The default, -1, saves the images with the PNG encoder of the system, which has no compression level; with a level from 0 to 9 the converter writes the PNG files itself. A gray page is saved with one gray channel.
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 a conversion.
PdfToolsTimeout - the maximum time of a conversion, in seconds (600 by default).
ConvertToImages(Stream) and ConvertToImages(String) return a System.Drawing.Image for each converted page, without writing any file - for example to show thumbnails or to store the images in a database:
byte[] pdfBytes = File.ReadAllBytes(@"C:\Documents\report.pdf"); // or from a database, an upload... PdfToImageConverter converter = new PdfToImageConverter(); converter.Resolution = 36; converter.StartPageNumber = 1; converter.EndPageNumber = 1; Image[] images = converter.ConvertToImages(new MemoryStream(pdfBytes)); using (Image thumbnail = images[0]) { thumbnail.Save(@"C:\Documents\thumbnail.png", ImageFormat.Png); }
ConvertToThumbnails(String, Int32, Int32) returns a thumbnail for each converted page, as large as fits in the box you give (a maximum width and height in pixels; 0 leaves that side free), keeping the aspect ratio of each page. You do not have to choose a resolution: each page is rendered at the resolution its thumbnail needs.
ConvertPageToImage(String, Int32) converts one page to an Image and ConvertPageToImageBytes(String, Int32, ImageFormat) to the bytes of an image file in the format you choose, at the Resolution of the converter. They do not use or change StartPageNumber and EndPageNumber. All have overloads for streams.
PdfToImageConverter converter = new PdfToImageConverter(); // thumbnails of all the pages, at most 160 x 160 pixels Image[] thumbnails = converter.ConvertToThumbnails(@"C:\Documents\report.pdf", 160, 160); for (int i = 0; i < thumbnails.Length; i++) { thumbnails[i].Save(@"C:\Documents\thumb" + (i + 1) + ".png", ImageFormat.Png); } // page 3 as a JPEG file in memory, for a web response or a database converter.Resolution = 96; converter.JpegQuality = 80; byte[] jpeg = converter.ConvertPageToImageBytes(@"C:\Documents\report.pdf", 3, ImageFormat.Jpeg); // page 3 as a PNG file, the smallest one: compression level 9 converter.PngCompressionLevel = 9; byte[] png = converter.ConvertPageToImageBytes(@"C:\Documents\report.pdf", 3, ImageFormat.Png); // a black and white TIFF document for a fax service converter.Resolution = 200; converter.TiffCompression = PdfToImageTiffCompression.Ccitt4; File.WriteAllBytes(@"C:\Documents\report-fax.tiff", converter.ConvertToTiff(@"C:\Documents\report.pdf"));
ConvertToTiff(String) returns one TIFF image with a frame for each converted page, compressed as set by the TiffCompression property (LZW by default) - the format of document archives and fax services. ConvertToTiffInFolder(String) saves it as <ImagesPrefix>.tiff in the ImagesPath folder. Both have overloads for streams.
PdfToImageConverter converter = new PdfToImageConverter(); converter.Resolution = 200; converter.ColorSpace = PdfToImageColorSpace.Gray; byte[] tiff = converter.ConvertToTiff(@"C:\Documents\report.pdf"); File.WriteAllBytes(@"C:\Documents\report.tiff", tiff); // or directly to C:\Documents\archive\report.tiff converter.ImagesPath = @"C:\Documents\archive"; converter.ImagesPrefix = "report"; converter.ConvertToTiffInFolder(@"C:\Documents\report.pdf");
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".
ConvertPdfToImages - a command line tool with all the options. Without arguments it converts the pages of sample.pdf to PNG files.
PdfToImageInMemory - thumbnails of all the pages put together in one image, and a gray multi-page TIFF, from a document in memory.
WinForms_PdfToImage - a Windows Forms application with all the options and a preview of the first image.
The command line tool:
ConvertPdfToImages [/pdf:file.pdf] [/out:folder] [/format:png|jpg|bmp|gif|tiff] [/tiff]
[/dpi:n] [/gray] [/start:n] [/end:n] [/prefix:name] [/userpswd:password]
[/quality:0-100] [/compression:lzw|none|rle|ccitt3|ccitt4] [/pngcompression:0-9]
[/thumbs:size]
ConvertPdfToImages /pdf:sample.pdf /format:jpg /dpi:72 /start:1 /end:2
ConvertPdfToImages /pdf:sample.pdf /tiff /gray /dpi:200
ConvertPdfToImages /pdf:sample.pdf /pngcompression:9Multi-page TIFF documents: the ConvertToTiff and ConvertToTiffInFolder methods.
Thumbnails that keep the aspect ratio: ConvertToThumbnails(String, Int32, Int32).
One page to an Image or to image file bytes: ConvertPageToImage and ConvertPageToImageBytes.
The JPEG quality and the TIFF compression: the JpegQuality and TiffCompression properties.
The PNG compression level, 0 to 9: the PngCompressionLevel property.
Version 10 is part of the ExpertPdf 22.0 release and needs a version 10 license key.
New samples for .NET Framework, .NET 10 and .NET 11, in C# and VB.NET: a command line tool, a sample that works in memory, and a Windows Forms application.
A license key is emailed to you when you purchase the product. Set it with the LicenseKey property before you call the converter. Without a license key the converter works the same and draws an evaluation notice on the images.
Support: office@html-to-pdf.net, https://www.html-to-pdf.info