Convert a Multipage TIFF to PDF in .NET
Introduction
This article shows you how to create a PDF document from a multipage TIFF document in both C# and VB.NET using TallPDF.NET 2.0.
Sample Files
Download Code
How it works
Basically, there three steps involved in the conversion:
- Get the number of frames.
- Add each frame to a document.
- Write the document.
1. Get the number of frames
A TIFF image can have multiple frames (pages). The input TIFF document in this sample has 4 frames. To retrieve the number of frames, use ImageShape.GetFrameCount(). This method has three overloads, allowing you to specify the image as a path, a stream or a System.Drawing.Bitmap object.
2. Add each frame to a document.
The next step is to create a new document and add a new page for each frame. We already know the number of frames so we can use a for-next loop to iterate over the frames.
3. Write the document.
Call Document.Write to write the PDF document to disk.
TallPDF.NET Sample Code
The easiest way to implement this using TallPDF is to create a new section for each frame. This ensures the original size of each image is retained and no scaling or clipping will occur.
[C# Sample Code]
[VB.NET]
//1. Get number of pages/images used in TIFF file
int frameCount = ImageShape.GetFrameCount( tiffPath );
//2. Add a section for each frame
// Create new (Pdf)document
Document doc = new Document();
// Iterate over frames
for ( int frameIndex=0; frameIndex < frameCount; frameIndex++ )
{
//Create a section and add it to the document.
Section section = doc.Sections.Add();
section.StartOnNewPage = true;
//Set Margins to zero; the image will span the entire page
section.LeftMargin = 0;
section.RightMargin = 0;
section.TopMargin = 0;
section.BottomMargin = 0;
// Load the frame into an Image paragraph
Image frame = new Image( tiffPath, frameIndex );
//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 );
}
// 3. Stream the PDF to a file
using( FileStream fs = new FileStream( "out.pdf", FileMode.Create ) )
{
//Write the file
doc.Write( fs );
}
'1. Get number of pages/images used in TIFF file
Dim frameCount As Integer = ImageShape.GetFrameCount(tiffPath)
' 2. Add a section for each frame
' Create a new TallPDF document
Dim doc As Document = New Document
' Iterate over the frames
Dim frameIndex As Integer
For frameIndex = 0 To frameCount - 1 Step 1
'Create a section and add it to the document.
Dim section As Section = doc.Sections.Add()
section.StartOnNewPage = True
'Set Margins to zero; the image will span the entire page
section.LeftMargin = 0
section.RightMargin = 0
section.TopMargin = 0
section.BottomMargin = 0
' Load the frame into an Image paragraph
Dim frame As New Image(tiffPath, frameIndex)
' 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 frameIndex
'3. Stream the PDF to a file
Dim fs As FileStream = New FileStream("out.pdf", FileMode.Create)
Try
'Write the file
doc.Write(fs)
Finally
fs.Close()
End Try
Using PDFKit.NET
To implement this with PDFKit.NET we need a slightly different approach. PDFKit.NET has a page-based object model as opposed to TallPDF.NET's flow model. So we'll add a new page for each frame and use an ImageShape to place the frame on that page.
[C#]
//1. Get number of pages/images used in TIFF file
int frameCount = ImageShape.GetFrameCount( tiffPath );
//2. Add a page for each frame
// Create new (Pdf)document
Document doc = new Document();
// Iterate over frames
for ( int frameIndex=0; frameIndex < frameCount; frameIndex++ )
{
// Load the frame into an Image paragraph.
ImageShape frame = new ImageShape( tiffPath, frameIndex );
// Create a new page with the same size as the image.
Page page = new Page( frame.Width, frame.Height );
doc.Pages.Append( page );
//Add the image to the page
page.Underlay.Add( frame );
}
// 3. Stream the PDF to a file
using( FileStream fs = new FileStream( "out.pdf", FileMode.Create ) )
{
//Write the file
doc.Write( new BinaryWriter( fs ) );
}
[VB.NET]
'1. Get number of pages/images used in TIFF file
Dim frameCount As Integer = ImageShape.GetFrameCount(tiffPath)
' 2. Add a section for each frame
' Create a new TallPDF document
Dim doc As Document = New Document
' Iterate over the frames
Dim frameIndex As Integer
For frameIndex = 0 To frameCount - 1 Step 1
' Load the frame into an Image paragraph.
Dim frame As New ImageShape(tiffPath, frameIndex)
' Create a new page with the same size as the image.
Dim page As New Page(frame.Width, frame.Height)
doc.Pages.Append(page)
'Add the image to the page
page.Underlay.Add(frame)
Next frameIndex
'3. Stream the PDF to a file
Dim fs As FileStream = New FileStream("out.pdf", FileMode.Create)
Try
'Write the file
doc.Write(New BinaryWriter(fs))
Finally
fs.Close()
End Try
Imaging considerations
In this article we've used images based on files because TallPDF.NET and PDFKit.NET are highly optimized to work with files and streams. Older versions of both components (before TallPDF.NET 2.0.24 and PDFKit.NET 1.0.24) were based on GDI+ Bitmaps (System.Drawing.Bitmap). While quite flexible, GDI+ also introduces performace and quality issues. GDI+ uses a lot of resources to hold entire bitmaps in memory, often uncompressed (!). In addition to this, GDI+ has a nasty habit of converting 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 for efficient seeking and caching, resulting in greater performance. In addition to this, the image processing subsystem is specificaly designed not to modify the image data itself. So, when ever possible, use images from files or streams.
About TallComponents
TallComponents specializes in .NET PDF components. You can visit TallComponents at http://www.tallcomponents.com/. Here you can download evaluations for all our PDF-related .NET components and get support.
Copyright © 2001-2005 TallComponents BV





