Link Search Menu Expand Document

Create Multipage Tiff to PDF - VB.NET

PDF SDK sample in VB.NET demonstrating ‘Create Multipage Tiff to PDF’

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Imports Bytescout.PDF

''' <summary>
''' This example demonstrates how to create PDF from multipage TIFF image. 
''' </summary>
Class Program

    Shared Sub Main()

        ' Create new document
        Dim pdfDocument = New Document()
        pdfDocument.RegistrationName = "demo"
        pdfDocument.RegistrationKey = "demo"

        ' If you wish to load an existing document uncomment the line below And comment the Add page section instead
        ' pdfDocument.Load(".\existing_document.pdf")

        ' Get TIFF file in BitMap
        Dim multipageTiffImage As Bitmap = Drawing.Image.FromFile("MultipageTiffFile.tiff")

        ' Tiff images can contain multiple images named as frame
        Dim count = multipageTiffImage.GetFrameCount(FrameDimension.Page)

        For idx = 0 To count - 1

            ' Save each frame to a bytestream
            multipageTiffImage.SelectActiveFrame(FrameDimension.Page, idx)

            Dim byteStream As New MemoryStream()
            multipageTiffImage.Save(byteStream, ImageFormat.Tiff)

            Dim sysImage = FixImageOrientation(byteStream)

            Using sysImage

                Dim image = New Bytescout.PDF.Image(sysImage, ImageCompression.DCT, 75)
                Dim w As Single = sysImage.Width / sysImage.HorizontalResolution * 72.0F
                Dim h As Single = sysImage.Height / sysImage.VerticalResolution * 72.0F

                ' Create PDF page
                Dim page = New Page(w, h)

                Dim canvas = page.Canvas
                canvas.DrawImage(image, 0, 0, w, h)

                ' Add PDF document

            End Using


        ' Save document to file

        ' Cleanup 

        ' Open document in default PDF viewer app
        Dim processStartInfo As New ProcessStartInfo("result.pdf")
        processStartInfo.UseShellExecute = True

    End Sub

    ''' <summary>
    ''' Fix image orientation
    ''' </summary>
    Private Shared Function FixImageOrientation(ByVal fileStream As Stream) As System.Drawing.Image

        Dim sysImage As System.Drawing.Image = System.Drawing.Image.FromStream(fileStream, False, False)

        ' fix orientation by EXIF rotation tag
        Const exifRotationTag As Integer = &H112
        Dim propertyItemIDs As Integer() = sysImage.PropertyIdList

        Dim found As Integer = Array.BinarySearch(propertyItemIDs, 0, propertyItemIDs.Length, exifRotationTag)

        If found > -1 Then
            Dim pi As PropertyItem = sysImage.GetPropertyItem(exifRotationTag)
            Dim orientation As Integer = pi.Value(0)

            Select Case orientation
                Case 2
                Case 3
                Case 4
                Case 5
                Case 6
                Case 7
                Case 8
                Case Else
            End Select

        End If

        Return sysImage
    End Function

End Class

Download Source Code (.zip)

Return to the previous page Explore PDF SDK

Copyright © 2016 - 2021 ByteScout