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
                                doc.NewPage()
                                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)
                                doc.Add(img)
                            Next
                        End If
                    Else
                        doc.NewPage()
                        gif.SetAbsolutePosition(10, 40)
                        gif.ScaleToFit(pageWidth, pageHeight)
                        doc.Add(gif)

                    End If
                End If
            End If
Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s