Convert PDF to multipage TIFF in C# .NET
Convert PDF to multipage TIFF in C# .NET
This article shows how to convert PDF to multipage TIFF in C# using PDFRasterizer.NET 3.0.
Code sample to convert a PDF to multipage TIFF using C# or VB.NET
There are basically two ways to achieve this conversion. The simplest way is to just call Document.ConvertToTiff().
C#
using (FileStream fileIn = new FileStream(
"in.pdf", FileMode.Open, FileAccess.Read))
{
Document document = new Document(fileIn);
ConvertToTiffOptions options = new ConvertToTiffOptions();
options.Compression = TiffCompression.CcittG4;
options.Resolution = 150;
options.PixelFormat = PixelFormat.Bw1Bpp;
using (FileStream fileOut = new FileStream(
"out.tif", FileMode.Create, FileAccess.Write))
{
document.ConvertToTiff(fileOut, options);
}
}
VB.NET
Using fileIn As New FileStream( _
"in.pdf", FileMode.Open, FileAccess.Read)
Dim pdf As New Document(fileIn)
Dim options As New ConvertToTiffOptions()
options.Compression = TiffCompression.CcittG4
options.Resolution = 150
options.PixelFormat = PixelFormat.Bw1Bpp
Using fileOut As New FileStream( _
"out.tif", FileMode.Create, FileAccess.Write)
pdf.ConvertToTiff(fileOut, options)
End Using
End Using
The Resolution and Compression settings of the ConvertToTiffOptions class allow you to control the size of the output. Lowering the resolution will lead to smaller files, at the cost of lower quality output. The effect of changing the compression settings depends on the graphical content of the PDF file.
LZW compression may result in smaller file sizes for PDF file that contain more than just black text on a white background. It may not be usable however if the resulting file needs to be processed further by a component that only supports CCITT G3/G4 compression. Many fax machines for example only support CCITT G3.
Page.Draw
Another way to convert PDF to TIFF using C#, is to use Page.Draw() to create a bitmap for each page, and then use standard .Net methods to write these to a tiff file. There are two issue with this though:
- .NET does not support creating a graphics instance for a monochrome bitmap. So this solution only works for generating color TIFF files. This, in fact, is the main reason why our software offers the ConvertToTiff methods.
- Writing multipage TIFF files is fairly involved using only standard .NET calls. See the sample code below (C# only). A similar effect can be achieved via ConvertToTiff in a much simpler way by just changing the PixelFormat to Rgba32Bpp.
Using standard .Net calls to generate multipage Tiff (C# only)
double scale = dpi / 72;
int n = document.Pages.Count;
if (n > 0)
{
// Convert the first page.
Page page = document.Pages[0];
using (Bitmap tiff = new Bitmap((int)(scale * page.Width), (int)(scale * page.Height)))
{
using (Graphics graphics = Graphics.FromImage(tiff))
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.ScaleTransform((float)scale, (float)scale);
graphics.Clear(Color.White);
page.Draw(graphics);
}
tiff.SetResolution((float)dpi, (float)dpi);
EncoderParameters firstImageParameters = new EncoderParameters();
firstImageParameters.Param = new EncoderParameter[2];
firstImageParameters.Param[1] = new EncoderParameter(
Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
EncoderParameters nextImageParameters = new EncoderParameters();
nextImageParameters.Param = new EncoderParameter[2];
nextImageParameters.Param[1] = new EncoderParameter(
Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage);
EncoderParameters flushImageParameters = new EncoderParameters();
flushImageParameters.Param = new EncoderParameter[1];
flushImageParameters.Param[0] = new EncoderParameter(
Encoder.SaveFlag, (long)EncoderValue.Flush);
firstImageParameters.Param[0] = new EncoderParameter(
Encoder.Compression, (long)EncoderValue.CompressionLZW);
nextImageParameters.Param[0] = new EncoderParameter(
Encoder.Compression, (long)EncoderValue.CompressionLZW);
ImageCodecInfo tiffCodec = null;
foreach (System.Drawing.Imaging.ImageCodecInfo codec in
System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders())
{
if (codec.FormatDescription.Equals("TIFF"))
{
tiffCodec = codec;
}
}
if (tiffCodec == null)
{
throw new ApplicationException("could not find tiff codec");
}
tiff.Save(file, tiffCodec, firstImageParameters);
// Convert the remaining pages.
for (int i = 1; i < n; i++)
{
page = document.Pages[i];
using (Bitmap nextBitmap = new Bitmap((int)(scale * page.Width), (int)(scale * page.Height)))
{
using (Graphics graphics = Graphics.FromImage(nextBitmap))
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.ScaleTransform((float)scale, (float)scale);
graphics.Clear(Color.White);
page.Draw(graphics);
}
nextBitmap.SetResolution((float)dpi, (float)dpi);
tiff.SaveAdd(nextBitmap, nextImageParameters);
}
}
tiff.SaveAdd(flushImageParameters);
}
}