Convert multipage TIFF to PDF using PDFKit.NET 3.0

  • Reference KB000064
  • Type Code sample
  • Product PDFKit.NET
  • Categories Generate PDF, Images
  • Created 10/03/2010

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;
1 // count the number of TIFF frames/pages 
2 string path = @"in.tif"; 
3 ImageShape image = new ImageShape(path); 
4 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
1 ' count the number of TIFF frames/pages 
2 Dim path As String = "in.tif" 
3 Dim image As ImageShape = New ImageShape(path) 
4 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); 
}
 1 Document document = new Document(); 
 2 // for each frame add a section 
 3 for (int index = 0; index < count; index++) 
 4 {
 5    // load the frame into an image shape 
 6    ImageShape frame = new ImageShape(path, index); 
 7    // create a page and add it to the document 
 8    Page page = new Page(frame.Width, frame.Height); 
 9    document.Pages.Add(page); 
10    // add the image to the page overlay 
11    page.Overlay.Add(frame); 
12 }

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
 1 Dim document As New Document() 
 2 ' for each frame add a section 
 3 For index = 0 To count - 1 
 4    ' load the frame into an image shape 
 5    Dim frame As New ImageShape(path, index) 
 6    ' create a page and add it to the document. 
 7    Dim page As New Page(frame.Width, frame.Height) 
 8    document.Pages.Add(page) 
 9    ' add the image to the page 
10    page.Overlay.Add(frame) 
11 Next

Step 3: Save the document


// save the PDF to a file 
using (FileStream file = new FileStream( 
   @"out.pdf", FileMode.Create, FileAccess.Write)) 
{ 
   document.Write(file); 
}
1 // save the PDF to a file 
2 using (FileStream file = new FileStream( 
3    @"out.pdf", FileMode.Create, FileAccess.Write)) 
4 { 
5    document.Write(file); 
6 }

' save the PDF to a file 
Using file = New FileStream( _ 
   "out.pdf", FileMode.Create, FileAccess.Write) 
   document.Write(file) 
End Using
1 ' save the PDF to a file 
2 Using file = New FileStream( _ 
3    "out.pdf", FileMode.Create, FileAccess.Write) 
4    document.Write(file) 
5 End Using

Performance considerations

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