Convert PDFs to Images in .NET with NuGet
If you have ever needed to convert a PDF to an image inside a .NET application, you have probably run into the usual options: shell out to a CLI tool, stitch something together with an open-source library, or spend hours wrestling with rendering edge cases. PDF2IMG from Datalogics ships as a NuGet package, integrates directly into your .NET project, and is built on Adobe PDF Library technology for accurate output across fonts, transparency, and color spaces. This tutorial walks through the real API from the official documentation, covering every step from installation to converting your first page.
Prerequisites
● .NET 6 or later (Windows 64-bit)
● Visual Studio 2022 or VS Code with the C# extension
● A PDF2IMG activation key (available via the free trial at datalogics.com/pdf2img-free-trial)
● NuGet Package Manager
Step 1: Install the NuGet Package
Open the NuGet Package Manager Console in Visual Studio and run:
PM> Install-Package PDF2IMG.LM.NET
Or via the .NET CLI:
dotnet add package PDF2IMG.LM.NET
The package includes the full PDF2IMG runtime for Windows 64-bit. No separate installer is needed when integrating via NuGet. Once installed, add the using directive at the top of your file:
using Datalogics.PDF2IMG;
Step 2: Understand the .NET Interface
The PDF2IMG .NET interface follows a straightforward five-step pattern, as defined in the official documentation:
● Create an instance of the PDF2IMG class
● Load your input PDF or XPS document using LoadInput
● Set image conversion options using SetImageConversionOptions
● Convert each page using ConvertPageToImage (or all pages to TIFF using ConvertAllPagesToTIFFImage)
● Dispose of the object
All five public methods on the PDF2IMG class are covered in this tutorial.
Step 3: Create a PDF2IMG Instance
The PDF2IMG constructor accepts three optional parameters:
var converter = new PDF2IMG();
The full constructor signature is:
PDF2IMG(IList<string> fonts = null, bool ignoreSystemFonts = false, bool ignoreCurrentDirectory = false)
fonts: an optional list of directory paths for the system to search for font files. Use this if your PDFs reference fonts not installed on the host machine.
ignoreSystemFonts: set to true to skip scanning default system font directories at startup. This speeds up initialization for PDFs that have all fonts embedded.
ignoreCurrentDirectory: set to true to skip searching the current working directory for font resources.
For most workflows, the no-argument constructor is sufficient.
Step 4: Load the Input PDF
LoadInput accepts a byte array containing the PDF document and an optional password string:
byte[] pdfBytes = File.ReadAllBytes(@"C:\Documents\sample.pdf");
int pageCount = converter.LoadInput(pdfBytes);
LoadInput returns the number of pages in the document. If the document is password-protected, pass the password as the second argument:
int pageCount = converter.LoadInput(pdfBytes, "yourpassword");
Note that the document must allow Content Copying and Page Extraction in its security settings for PDF2IMG to process it. If security restrictions prevent conversion, PDF2IMG will throw an exception with the message: The document security settings do not permit this operation.
Step 5: Set Image Conversion Options
Use SetImageConversionOptions to configure output format, resolution, color space, and other settings before converting. This method takes an ImageConversionOptions object:
var options = new ImageConversionOptions
{
outputType = OutputType.JPEG,
horizontalResolution = 150,
verticalResolution = 150,
colorSpace = ColorSpace.RGB
};
converter.SetImageConversionOptions(options);
Key ImageConversionOptions Properties
outputType: the output image format. Options are TIFF (default), JPEG, PNG, BMP, GIF, EPS, RAW, and PDF.
horizontalResolution / verticalResolution: output DPI, from 12 to 2400. Default is 300 for both. Set both to the same value for square pixels.
colorSpace: output color space. Default is RGB. Other options include Gray, RGBA, CMYK, and LAB. Not all color spaces are valid for all output formats. For example, CMYK is only valid for JPEG or TIFF output.
bitsPerChannel: bits per color channel in the output image. Valid values are 1 or 8. Default is 8.
useColorManagement: whether color management is applied during conversion. Defaults to true. When true, PDF2IMG embeds ICC profiles in TIFF, JPEG, PNG, and BMP output.
enhanceThinLines: matches the default behavior of Adobe Reader and Acrobat for thin line rendering. Defaults to true.
smoothing: anti-aliasing applied to the output image. Default is All. Other options are None, Text, LineArt, and Image. Useful for low-resolution outputs; not recommended for images intended for print.
resamplerUsed: resampling method. Default is Auto. Bicubic resampling is applied when output resolution is below 150 DPI or when the pixelcount is less than half the default input size. Options are Auto, Bicubic, and None.
regionOfInterest: the region of the PDF page to rasterize. Default is CropBox. Other options include MediaBox, ArtBox, TrimBox, BleedBox, BoundingBox, and CustomBox. If CustomBox is selected, provide coordinates in customRegion.
overPrintPreview: set to true to enable Overprint Preview, which shows how the page would look when printed accounting for ink overprinting. Defaults to false.
Step 6: Convert a Single Page
ConvertPageToImage converts a specified page to an image file at the output path you provide:
converter.ConvertPageToImage(1, @"C:\Output\page_1.jpg");
The first argument is the page number, counted from 1. The second is the full output file path including filename and extension. The method uses whichever ImageConversionOptions were set in the previous step.
To convert multiple specific pages, loop over the page numbers:
for (uint i = 1; i <= pageCount; i++)
{
string outputPath = $@"C:\Output\page_{i}.jpg";
converter.ConvertPageToImage(i, outputPath);
}
Step 7: Convert All Pages to a Multipage TIFF
To convert every page in the document to a single multipage TIFF file, use ConvertAllPagesToTIFFImage:
var tiffOptions = new ImageConversionOptions
{
outputType = OutputType.TIFF,
horizontalResolution = 300,
verticalResolution = 300
};
converter.SetImageConversionOptions(tiffOptions);
converter.ConvertAllPagesToTIFFImage(@"C:\Output\all_pages.tif");
Each page in the source PDF is stored in memory and then written to the single TIFF output file. This is the correct method when you need a single multipage TIFF archive rather than individual per-page files.
Step 8: Check for Non-Renderable Content
Before converting, you can check a page range for annotations or form fields that cannot be rendered using CheckForMissingAppearances:
int missing = converter.CheckForMissingAppearances(1, (uint)pageCount);
The method returns the count of appearances that cannot be rendered. An annotation can define multiple appearance streams, for example a form field that changes color when hovered. If any of these cannot be rendered to the output image, they are counted here. A return value of 0 means all content can be rendered.
Step 9: Remove White Margins (Optional)
GetPageBoxWithWhiteSpaceRemoved returns bounding box coordinates that can be used to crop white margins from a page:
var box = converter.GetPageBoxWithWhiteSpaceRemoved();
The method returns top, left, right, and bottom coordinates forming the tightest bounding box around all visible content on the page. Pass these coordinates to the customRegion property of ImageConversionOptions with regionOfInterest set to CustomBox to crop to content.
Step 10: Complete Example
Here is a complete working example that converts all pages of a PDF to individual JPEG files at 150 DPI:
using Datalogics.PDF2IMG;
using System.IO;
byte[] pdfBytes = File.ReadAllBytes(@"C:\Documents\report.pdf");
using var converter = new PDF2IMG();
int pageCount = converter.LoadInput(pdfBytes);
var options = new ImageConversionOptions
{
outputType = OutputType.JPEG,
horizontalResolution = 150,
verticalResolution = 150,
colorSpace = ColorSpace.RGB,
jpegConversionOptions = new JpegConversionOptions { quality = 85 }
};
converter.SetImageConversionOptions(options);
for (uint i = 1; i <= pageCount; i++)
{
converter.ConvertPageToImage(i, $@"C:\Output\report_{i}.jpg");
}
jpegConversionOptions.quality sets the JPEG quality value from 1 to 100. Default is 75. Higher values produce larger, sharper files. Note that lower quality values affect both detail and color precision.
TIFF Compression Options
When outputting TIFF, use tiffConversionOptions to set compression:
var options = new ImageConversionOptions
{
outputType = OutputType.TIFF,
horizontalResolution = 300,
verticalResolution = 300,
tiffConversionOptions = new TiffConversionOptions { compression = Compression.LZW }
};
Compression options are None, LZW (default), G3, G4, and JPEG. G3 and G4 are only valid for 1-bit black and white TIFF output. For G3 or G4, also set colorSpace to Gray and bitsPerChannel to 1.
What to Do Next:
Get the free trial of PDF2IMG
Get the NuGet package
Check out our blog on PDF2IMG and color management