How to add a multi-page TIFF to a PDF using iTextSharp (VB.NET)

If you are ever stuck with a multi-page tiff, you will know how frustrating it is to get the images out and then add them to a pdf. This code snippet below shows how it’s done and feel free to leave a comment if you have used it and it worked.

'check if image exists
                Dim fi As New FileInfo(strImagePath)
                If fi.Exists Then

                    'save image in document
                    Dim gif As Image = Image.GetInstance(fi.FullName)
                    Dim pageWidth = doc.PageSize.Width - (10 + 10)
                    Dim pageHeight = doc.PageSize.Height - (40 + 10)

                    If fi.Extension.ToLower.EndsWith("tif") Then
                        Dim bmp As New System.Drawing.Bitmap(fi.FullName)
                        Dim total As Integer = bmp.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page)
                        If total > 1 Then
                            For k As Integer = 0 To total - 1
                                bmp.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, k)
                                Dim img As iTextSharp.text.Image
                                img = iTextSharp.text.Image.GetInstance(bmp, System.Drawing.Imaging.ImageFormat.Bmp)
                                img.SetAbsolutePosition(10, 40)
                                img.ScaleToFit(pageWidth, pageHeight)
                        End If
                        gif.SetAbsolutePosition(10, 40)
                        gif.ScaleToFit(pageWidth, pageHeight)

                    End If
                End If
            End If

One thought on “How to add a multi-page TIFF to a PDF using iTextSharp (VB.NET)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.