TIFF to PDF C#

TIFF to PDF C#

This article shows you how to convert a multipage TIFF to PDF using PDFKit.NET.

Convert Tiff To Pdf

Code samples to convert TIFF to PDF in C# and VB.NET

Step 1: Get the number of TIFF frames

// count the number of TIFF frames/pages 
string path = @"in.tif"; 
ImageShape image = new ImageShape(path); 
int count = image.FrameCount;
' count the number of TIFF frames/pages 
Dim path As String = "in.tif" 
Dim image As ImageShape = New ImageShape(path) 
Dim count As Integer = image.FrameCount

Step 2: Add a new page and image shape per frame

For each frame we add a page that has a size that equals the size of the frame. To the overlay of each page we add a single image shape that spans the entire page

Document document = new Document(); 
// for each frame add a section 
for (int index = 0; index < count; index++) 
{
   // load the frame into an image shape 
   ImageShape frame = new ImageShape(path, index); 
   // create a page and add it to the document 
   Page page = new Page(frame.Width, frame.Height); 
   document.Pages.Add(page); 
   // add the image to the page overlay 
   page.Overlay.Add(frame); 
}
Dim document As New Document() 
' for each frame add a section 
For index = 0 To count - 1 
   ' load the frame into an image shape 
   Dim frame As New ImageShape(path, index) 
   ' create a page and add it to the document. 
   Dim page As New Page(frame.Width, frame.Height) 
   document.Pages.Add(page) 
   ' add the image to the page 
   page.Overlay.Add(frame) 
Next

Step 3: Save the PDF document

// save the PDF to a file 
using (FileStream file = new FileStream( 
   @"out.pdf", FileMode.Create, FileAccess.Write)) 
{ 
   document.Write(file); 
}
' save the PDF to a file 
Using file = New FileStream( _ 
   "out.pdf", FileMode.Create, FileAccess.Write) 
   document.Write(file) 
End Using

Performance considerations when converting TIFF to PDF in C# / VB.NET

In this TIFF to PDF article we have used images based on files because PDFKit.NET is optimized to work with files and streams. Older versions used System.Drawing.Bitmap internally. While quite flexible, GDI+ also introduces performance and quality issues. GDI+ uses a lot of resources to hold entire bitmaps in memory, often uncompressed (!). In addition to this, GDI+ converts image data to whatever format is best suited for on-screen display, meaning that the actual color values in the image may change (!). Using files and streams allows efficient seeking and caching which results in better performance. In addition to this, the image processing is specifically designed to not modify the image data itself. So whenever possible, construct images from files or streams and avoid System.Drawing.Bitmap.