Skip to content

Convert PDF files to multi-page color and B/W TIFF images

The conversion of a PDF file to a multi-page TIFF is implemented through the PDFDocumentRenderer class. Once a PDF file is loaded in a PDFFixedDocument it can be converted to multi-page TIFF with a few lines of code.

C#
// Load the PDF file
PDFFixedDocument document = new PDFFixedDocument("sample.pdf");
// Create a document renderer.
PDFDocumentRenderer documentRenderer = new PDFDocumentRenderer(document);
// Open the destination stream that will store the TIFF image.
FileStream tiffStream = File.OpenWrite("sample.tif");
// Setup the rendering settings and convert the PDF to multi-page TIFF
PDFRendererSettings settings = new PDFRendererSettings(96, 96);
documentRenderer.ConvertToMultipageImage(settings, tiffStream);
// Close the destination stream
tiffStream.Flush();
tiffStream.Close();

By default the PDF file is converted to RGBA TIFF but grayscale and B/W TIFFs are supported.

In order to create a 1bpp B/W TIFF a black and white rendering surface must be provided as model in the rendering settings.

C#
// Load the PDF file
PDFFixedDocument document = new PDFFixedDocument("sample.pdf");
// Create a document renderer.
PDFDocumentRenderer documentRenderer = new PDFDocumentRenderer(document);
// Open the destination stream that will store the TIFF image.
FileStream tiffStream = File.OpenWrite("sample.tif");
// Setup the rendering settings and convert the PDF to multi-page TIFF
PDFRendererSettings settings = new PDFRendererSettings(96, 96);
// Specify a Black and White rendering surface to generate a 1bpp B/W TIFF.
settings.RenderingSurface = new PDFBlackWhiteByteRenderingSurface();
documentRenderer.ConvertToMultipageImage(settings, tiffStream);
// Close the destination stream
tiffStream.Flush();
tiffStream.Close();

When converting a PDF file to B/W TIFF each PDF page is initially rendered in grayscale and then the grayscale image is converted to B/W. The process of converting the grayscale image to B/W is called binarization and we implemented this process through binarization filters.
The most simple filter is the threshold filter. This filter converts the values above the threshold to white and the values below the threshold to black.

We'll use this PDF page for testing the conversion to B/W TIFF image.

When using the threshold filter the converted image looks like below:

As you can see a lot of details are lost during the conversion. In order to avoid this we support also several error diffusion dithering filters including the well-known Floyd-Steinberg filter.

The binarization filter is specified as an attribute of the black and white rendering surface.

C#
// Load the PDF file
PDFFixedDocument document = new PDFFixedDocument("sample.pdf");
// Create a document renderer.
PDFDocumentRenderer documentRenderer = new PDFDocumentRenderer(document);
// Open the destination stream that will store the TIFF image.
FileStream tiffStream = File.OpenWrite("sample.tif");
// Setup the rendering settings and convert the PDF to multi-page TIFF
PDFRendererSettings settings = new PDFRendererSettings(96, 96);
// Specify a Black and White rendering surface with an explicit binarization filter to generate a 1bpp B/W TIFF.
PDFBlackWhiteByteRenderingSurface bwRenderingSurface = new PDFBlackWhiteByteRenderingSurface();
bwRenderingSurface.BinarizationFilter = new PDFFloydSteinbergDitheringFilter();
settings.RenderingSurface = bwRenderingSurface;
documentRenderer.ConvertToMultipageImage(settings, tiffStream);
// Close the destination stream
tiffStream.Flush();
tiffStream.Close();

Using the same source PDF page the result of converting it to B/W TIFF looks like this when using the Floyd-Steinberg filter:

We support 10 binarization filters: 1 simple threshold filter and 9 dithering based filters, Bayer which uses ordered dithering and
Atkinson, Burkes, Floyd-Steinberg, Jarvis Judice Ninke, Sierra, Sierra Lite, Sierra 2 Row and Stucki that use error diffusion dithering).

The 1bpp B/W images are compressed using CCITT G4 compression.