Persits Software, Inc. Knowledge Base Articles

HOWTO: Convert multi-image TIFF to multi-page PDF

Problem Description

Starting with version 1.1, AspPDF supports images in Tagged Image File Format (TIFF), including multi-image TIFF files.

This article shows how to convert a multi-image TIFF to multi-page PDF.

Solution

The OpenImage and OpenImageBinary methods of the PdfDocument object accept an optional Index parameter which specifies the 1-based index of an image inside the TIFF document being opened (the parameter is ignored for all other types of images). If the Index value is out of range, the OpenImage methods return Nothing.

The following code snippet opens an arbitrary image file (not necessarily a TIFF) and converts it to PDF. If the file being opened happens to be a multi-image TIFF, this code snippet will convert it into a multi-page PDF.

Set Doc = Pdf.CreateDocument
Set Image = Doc.OpenImage(Path)
Set param = Pdf.CreateParam("index=1")

While Not (Image Is Nothing)
   Width = Image.Width * 72 / Image.ResolutionX
   Height = Image.Height * 72 / Image.ResolutionY
   Set Page = Doc.Pages.Add(Width, Height)
   Page.Canvas.DrawImage Image, "x=0; y=0"

   param("Index") = param("Index") + 1 ' reference next image

   If Image.Format <> "TIF" Then
      Set Image = Nothing
' Stop
   Else
      Set Image = Doc.OpenImage(Path, param)
   End If
Wend