Convert PDF to image in C# and VB.NET
There are cases when you need to convert PDFs to images. For example, you might need to create thumbnail images from PDFs or perform optical character recognition (OCR) on some PDF documents.
You can use Docotic.Pdf library to convert PDF documents to images in C# and VB.NET projects. The library is 100% managed without external dependencies. It does not use System.Drawing.dll and GDI+ for PDF rasterization. Therefore, you will get consistent output on Windows, Linux, macOS, iOS, and Android.
9.5.17664-dev 14,820 passed Total NuGet downloads 4,998,853The library is available on NuGet and from our site. Get the library and a free time-limited license key on the Download C# .NET PDF library page.
Docotic.Pdf provides many options for PDF to image conversion. And it can also render and print PDF documents. Let's look at how you can convert PDF to images using Docotic.Pdf library.
Save PDF page as PNG, JPG, or TIFF image
Docotic.Pdf library allows you to convert PDF pages to PNG, JPEG, or TIFF images. You can write resulting images to a stream or a file. This C# snippet saves PDF pages to PNG images with a white background and 300x300 dpi resolution:
using BitMiracle.Docotic.Pdf;
using (var pdf = new PdfDocument("your_document.pdf"))
{
PdfDrawOptions options = PdfDrawOptions.Create();
options.BackgroundColor = new PdfRgbColor(255, 255, 255);
options.HorizontalResolution = 300;
options.VerticalResolution = 300;
for (int i = 0; i < pdf.PageCount; ++i)
pdf.Pages[i].Save($"page_{i}.png", options);
}
You can instruct the library to produce JPEG or TIFF images instead of PNG using PdfDrawOptions.Compression property. The following snippet shows how to switch to making JPEG images:
PdfDrawOptions options = ..;
options.Compression = ImageCompressionOptions.CreateJpeg();
Related C# and VB.NET samples are available on GitHub.
PDF document to multipage TIFF in .NET
You can save the entire document to a multipage TIFF image using PdfDocument.SaveAsTiff method. Here is a C# sample for PDF to TIFF conversion:
using (var pdf = new PdfDocument("document.pdf"))
{
PdfDrawOptions options = PdfDrawOptions.Create();
options.BackgroundColor = new PdfRgbColor(255, 255, 255);
pdf.SaveAsTiff("result.tiff", options);
}
Download and try the Save PDF as multipage TIFF sample code from GitHub. Both C# and VB.NET versions are available there.
Create thumbnails from PDF
With the help of Docotic.Pdf library, you can generate thumbnails from PDF pages. This C# sample creates a 200x200 pixels thumbnail from the first page of the given PDF and writes the thumbnail to a memory stream:
using (var pdf = new PdfDocument("document.pdf"))
{
PdfDrawOptions options = PdfDrawOptions.CreateFitSize(new PdfSize(200, 200), false);
options.BackgroundColor = new PdfGrayColor(100);
using (var stream = new MemoryStream())
{
pdf.Pages[0].Save(stream, options);
..
}
}
You can also use PdfDrawOptions.CreateFitWidth or PdfDrawOptions.CreateFitHeight method to produce thumbnail images of specified width or height.
Convert PDF to bitonal or grayscale images in .NET
The library can convert PDF documents to bitonal (black and white) TIFF images. This sample shows how to produce a bitonal TIFF image from a PDF document in C#:
using (var pdf = new PdfDocument("colored.pdf"))
{
PdfDrawOptions options = PdfDrawOptions.Create();
options.BackgroundColor = new PdfRgbColor(255, 255, 255);
options.Compression = ImageCompressionOptions.CreateTiff().SetBitonal();
pdf.SaveAsTiff("bitonal.tiff", options);
}
You can also specify a threshold level when using the TiffImageCompressionOptions.SetBitonalThreshold method. The threshold level defines how to transform grayscale, RGB, or CMYK colors to black-and-white.
Docotic.Pdf also supports PDF to grayscale PNG, JPG, or TIFF images conversion. Just use one of the PngImageCompressionOptions.SetGrayscale, JpegImageCompressionOptions.SetGrayscale, TiffImageCompressionOptions.SetGrayscale methods like in the following snippet:
PdfDrawOptions options = ..;
options.Compression = ImageCompressionOptions.CreatePng().SetGrayscale();
Zoom PDF
PdfDrawOptions.CreateZoom method allows you to get an image of a PDF page with a specified zoom level. This C# sample converts a PDF page to an image with 400% zoom level:
using (var pdf = new PdfDocument(@"document.pdf"))
{
PdfDrawOptions options = PdfDrawOptions.CreateZoom(400);
pdf.Pages[0].Save("zoomed.png", options);
}
Partial PDF to image rendering
You may need to convert only a part of a PDF page to image. For example, to split a large PDF page to tiles. Or to crop some unwanted content.
This C# code saves an image of a 256x256 point area at the left-top corner of a PDF page:
using (var pdf = new PdfDocument("large.pdf"))
{
PdfDrawOptions options = PdfDrawOptions.Create();
options.BackgroundColor = new PdfRgbColor(255, 255, 255);
PdfPage page = pdf.Pages[0];
PdfBox cropBoxBefore = page.CropBox;
page.CropBox = new PdfBox(0, cropBoxBefore.Height - 256, 256, cropBoxBefore.Height);
page.Save("part.png", options);
page.CropBox = cropBoxBefore;
}
Tiled PDF to image rendering
Docotic.Pdf provides built-in support for tiled rendering of PDF to TIFF. This method is useful when you need to process large PDF pages using a fixed amount of memory.
For example, your PDF document contains a page that has a size of 10000x10000 points. Regular conversion of this page to an RGB image at 300 DPI resolution requires over 1 Gb (= 10000 * 10000 * 3 * 300 / 72.0 bytes) of memory for the intermediate uncompressed RGB image data. Using tiled rendering with 1024x1024 tiles, you can limit memory consumption for the uncompressed RGB image data to 12 Mb (= 1024 * 1024 * 3 * 300 / 72.0 bytes).
Note that tiled PDF to image conversion is slower than regular conversion. For every tile, the library needs to process all PDF page operations, even if some of them do not affect the current tile.
This C# sample shows how to convert PDF to image using 512x512 tiles:
using (var pdf = new PdfDocument("large.pdf"))
{
PdfDrawOptions options = PdfDrawOptions.Create();
options.Compression = ImageCompressionOptions.CreateTiff().SetTileSize(512, 512);
options.BackgroundColor = new PdfGrayColor(100);
options.HorizontalResolution = 300;
options.VerticalResolution = 300;
pdf.Pages[0].Save("tiled.tiff", options);
}
Conclusion
Docotic.Pdf library provides different options for PDF to image conversion in .NET. You can convert PDF pages to full size or thumbnail images in PNG, TIFF, and JPEG formats. And you can save PDF documents as multipage TIFF files. The library can also produce bitonal and grayscale images.
Download and try PDF to image samples from GitHub.
Contact us if you have questions about PDF to image conversion.
Related resources
- Render and print PDF in C# and VB.NET article
- Convert PDF to JPG in .NET article
- PDF to image samples