Multipage TIFF to PDF

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

Step 1: Get the number of 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 section and image paragraph per frame

For each frame we add a section that has a page size that equals the size of the frame. The page margins are zero. To each section, add a single image paragraph that spans the entire page.

Document document = new Document(); 
// for each frame add a section 
for (int index = 0; index < count; index++) 
{ 
   // create a section and add it to the document. 
   Section section = document.Sections.Add(); 
   section.StartOnNewPage = true; 
   // set margins to zero; the image will span the entire page 
   section.Margin.Left = 0; 
   section.Margin.Right = 0; 
   section.Margin.Top = 0; 
   section.Margin.Bottom = 0; 
   // load the frame into an Image paragraph 
   Image frame = new Image(path, index); 
   // set PageSize from the Image dimensions 
   section.PageSize.Width = frame.Width; 
   section.PageSize.Height = frame.Height; 
   // add the image to the section 
   section.Paragraphs.Add( frame ); 
}
Dim document As New Document() 
' for each frame add a section 
For index = 0 To count - 1 
   ' create a section and add it to the document. 
   Dim section As Section = document.Sections.Add() 
   section.StartOnNewPage = True 
   ' set margins to zero; the image will span the entire page 
   section.Margin.Left = 0 
   section.Margin.Right = 0 
   section.Margin.Top = 0 
   section.Margin.Bottom = 0 
   ' load the frame into an Image paragraph 
   Dim frame As New Image(path, index) 
   ' set PageSize from the Image dimensions 
   section.PageSize.Width = frame.Width 
   section.PageSize.Height = frame.Height 
   ' add the image to the section 
   section.Paragraphs.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

In this article we have used images based on files because TallPDF.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 specificaly designed to not modify the image data itself. So whenever possible, construct images from files or streams and avoid System.Drawing.Bitmap.