Click or drag to resize

PDF to Image Converter

The PDF to image part of ExpertPdf Anywhere renders the pages of PDF documents to images on Windows, Linux and macOS. The images are returned in memory as the bytes of image files, saved as image files, or put together in one multi-page TIFF document.

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

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

  • Any resolution: low for thumbnails, high for printing or OCR.

  • Color (RGB) or gray images.

  • The images in memory, as the bytes of PNG, JPEG, BMP or TIFF files, or saved as image files with the file name prefix you choose.

  • One multi-page TIFF document with all the converted pages, in memory or in a file.

  • Thumbnails that fit in a box of the size you choose, keeping the aspect ratio of each page.

  • One page to the bytes of an image file in one call.

  • The JPEG quality, the TIFF compression (LZW, none, PackBits, CCITT Group 3 and Group 4) and the PNG compression level of the images.

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

Installation

The converter 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 PdfToImageConverter, set the options and call ConvertToImagesInFolder(String) to save one image file for each page in the ImagesPath folder:

using ExpertPdf.Anywhere;

PdfToImageConverter converter = new PdfToImageConverter();

converter.Resolution = 150;                     // DPI
converter.ImagesFormat = ImageFormat.Png;
converter.ImagesPath = "images";                // an existing folder
converter.ImagesPrefix = "page_";               // page_1.png, page_2.png, ...

converter.ConvertToImagesInFolder("report.pdf");

The files are numbered from 1 for the first converted page. The default prefix is "pdfimage_" and the default format is PNG. ImageFormat is the image format type of the library, in the ExpertPdf.Anywhere.Drawing namespace: Png, Jpeg, Bmp or Tiff.

Conversion options
  • 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 as the renderer writes them; with a level from 0 to 9 the converter writes the PNG files again at that level.

  • 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).

Images in memory

ConvertToImages(Stream) and ConvertToImages(String) return the bytes of an image file for each converted page, in the ImagesFormat format, without writing any file - for example to show thumbnails or to store the images in a database:

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

PdfToImageConverter converter = new PdfToImageConverter();
converter.Resolution = 36;
converter.StartPageNumber = 1;
converter.EndPageNumber = 1;

byte[][] images = converter.ConvertToImages(new MemoryStream(pdfBytes));
File.WriteAllBytes("thumbnail.png", images[0]);
Thumbnails and single pages

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 the bytes of an image file in the ImagesFormat format, and ConvertPageToImageBytes(String, Int32, ImageFormat) 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
byte[][] thumbnails = converter.ConvertToThumbnails("report.pdf", 160, 160);
for (int i = 0; i < thumbnails.Length; i++)
{
    File.WriteAllBytes("thumb" + (i + 1) + ".png", thumbnails[i]);
}

// 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("report.pdf", 3,
    ImageFormat.Jpeg);

// page 3 as a PNG file, the smallest one: compression level 9
converter.PngCompressionLevel = 9;
byte[] png = converter.ConvertPageToImageBytes("report.pdf", 3,
    ImageFormat.Png);

// a black and white TIFF document for a fax service
converter.Resolution = 200;
converter.TiffCompression = PdfToImageTiffCompression.Ccitt4;
File.WriteAllBytes("report-fax.tiff", converter.ConvertToTiff(
    "report.pdf"));
Multi-page TIFF documents

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("report.pdf");
File.WriteAllBytes("report.tiff", tiff);

// or directly to archive/report.tiff
converter.ImagesPath = "archive";
converter.ImagesPrefix = "report";
converter.ConvertToTiffInFolder("report.pdf");
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".

  • 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 a PDF contact sheet, a page as JPEG bytes and a gray multi-page TIFF, from a document in memory.

The command line tool:

ConvertPdfToImages [/pdf:file.pdf] [/out:folder] [/format:png|jpg|bmp|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:9
Licensing

Set LicenseKey once when the application starts, or the LicenseKey property before you call it. Without a license key the converter works the same and draws an evaluation notice on the images. See Licensing.

See Also