Recursive AI

Convert a PDF to TIFF in C#

All oneimage recipes

Register OneImage’s PDF format, load the PDF and save it with a .tif extension: every page becomes a page of one multipage TIFF. Drawing, encoding and compression are managed .NET code, with no Ghostscript to deploy. The program below converts the IRS Form W-9, a public six-page PDF, at 300 dpi in color, gray and black and white.

Terminal
dotnet new console -o PdfToTiff && cd PdfToTiff
dotnet add package RecursiveAI.OneImage.Pdf --version 1.10.4
curl -LO https://www.irs.gov/pub/irs-pdf/fw9.pdf

OneImage needs the .NET 10 SDK. Put this in Program.cs and run dotnet run:

C#
using OneImage;
using OneImage.Pdf;
 
var options = new LoadOptions
{
    Configuration = new ImageConfiguration()
        .Add(new PdfFormat(new PdfRenderOptions { Dpi = 300 })),
};
 
// Pages are drawn on opaque white, so the alpha channel can go.
Image pdf = Image.Load("fw9.pdf", options).RemoveAlpha();
 
Image gray = pdf.Grayscale(toGrayFormat: true);
 
pdf.Save("fw9.tif");                          // color, LZW
gray.Save("fw9-gray.tif");                    // 8-bit gray
gray.Threshold(0.5).Save("fw9-bw.tif");       // black and white
 
foreach (string file in new[] { "fw9.tif", "fw9-gray.tif", "fw9-bw.tif" })
{
    ImageInfo info = Image.Identify(file);
    long kb = new FileInfo(file).Length / 1024;
    string size = $"{info.Width}x{info.Height}";
    Console.WriteLine($"{file}: {info.Frames.Count} pages, {size}, "
                    + $"{info.Frames[0].Format}, {kb:N0} KB");
}
Text
fw9.tif: 6 pages, 2550x3300, Rgb/U8/None, 10,905 KB
fw9-gray.tif: 6 pages, 2550x3300, Gray/U8/None, 6,962 KB
fw9-bw.tif: 6 pages, 2550x3300, Gray/U8/None, 1,401 KB

At 300 dpi a US Letter page is 2550×3300 pixels. Saving the whole document writes every page, because TIFF holds several. RemoveAlpha() keeps the alpha channel PDF pages are drawn with out of the file. Threshold(0.5) turns every pixel white or black; raise it to keep fainter lines, or use AdaptiveThreshold() for unevenly lit scans (grayscale and black and white).

A range of pages, or one file per page

C#
using OneImage;
using OneImage.Pdf;
 
// Pages 2 to 4: FirstPage counts from 0.
var range = new LoadOptions
{
    Configuration = new ImageConfiguration().Add(new PdfFormat(
        new PdfRenderOptions { Dpi = 200, FirstPage = 1, PageCount = 3 })),
};
Image pages = Image.Load("fw9.pdf", range).RemoveAlpha();
pages.Save("fw9-pages-2-4.tif");
int count = Image.Identify("fw9-pages-2-4.tif").Frames.Count;
Console.WriteLine($"fw9-pages-2-4.tif: {count} pages");
 
// One single-page TIFF per page.
for (int i = 0; i < pages.FrameCount; i++)
{
    pages.Frames[i].ToImage().Save($"page-{i + 2}.tif");
    Console.WriteLine($"page-{i + 2}.tif");
}
Text
fw9-pages-2-4.tif: 3 pages
page-2.tif
page-3.tif
page-4.tif

Pages outside the range aren’t drawn at all.

Compression

TIFF pages are LZW-compressed by default. Pass new SaveOptions { EncoderOptions = new TiffEncoderOptions { Compression = TiffCompression.Deflate } } for Deflate, or None or PackBits.

Limits

  • No 1-bit or CCITT Group 4 output. Black and white pages are stored as 8-bit gray with only two values, compressed with LZW, Deflate or PackBits. OneImage reads fax (CCITT G3 and G4) TIFFs but doesn’t write them; if a fax system requires G4, convert with another tool.
  • Large documents are built in one go. Saving to a MemoryStream holds the whole TIFF in memory; for long documents write to a file or convert a range at a time. At most 1,000 pages load by default.
  • It renders pixels only: text in the TIFF isn’t searchable.

To render pages as PNG or JPG instead, see Convert a PDF to an image; to split a TIFF again, Split a multipage TIFF.