Components for Developers
© 2000-2012, Two Pilots

PDF Library Download Features Manual Tutorials FAQ Pricing HTML2PDF-X Pilot History In the Lab

PDF Creator Pilot documentation

Download CHM version of this manual.
PDF Creator Pilot 4 Knowledge Base
How to Convert an Image to a PDF Using VB.NET

See Also Example
Collapse All

Add a reference to PDF Creator Pilot in your VB.NET project. (See how to do this).

Use AddImageFromHandle, AddImageFromBLOB or AddImageFromFile to load an image to the library. Then use ShowImage or DrawImage to add an image to the PDF document's page.

You can find a full example below. You may also download a MS Visual Studio project with this example download a MS Visual Studio Project containing this example..

Example

Convert an Image to a PDF

VB.NET
[copy to clipboard]
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents AddImageFromBitmapHandle As System.Windows.Forms.Button
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents AddImageFromClipboard As System.Windows.Forms.Button
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents Label7 As System.Windows.Forms.Label
    Friend WithEvents AddImageFromBufferOrMemory As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.AddImageFromBitmapHandle = New System.Windows.Forms.Button
        Me.Label3 = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.Label5 = New System.Windows.Forms.Label
        Me.AddImageFromClipboard = New System.Windows.Forms.Button
        Me.Label6 = New System.Windows.Forms.Label
        Me.Label7 = New System.Windows.Forms.Label
        Me.AddImageFromBufferOrMemory = New System.Windows.Forms.Button
        CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Image)
        Me.PictureBox1.Location = New System.Drawing.Point(17, 136)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(150, 131)
        Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.PictureBox1.TabIndex = 0
        Me.PictureBox1.TabStop = False
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
        Me.Label1.Location = New System.Drawing.Point(84, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(408, 24)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "How to add images using different methods"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Label2
        '
        Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
        Me.Label2.Location = New System.Drawing.Point(20, 80)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(144, 40)
        Me.Label2.TabIndex = 2
        Me.Label2.Text = "This image will be added from bitmap handle"
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'AddImageFromBitmapHandle
        '
        Me.AddImageFromBitmapHandle.BackColor = System.Drawing.SystemColors.Control
        Me.AddImageFromBitmapHandle.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
        Me.AddImageFromBitmapHandle.Location = New System.Drawing.Point(40, 280)
        Me.AddImageFromBitmapHandle.Name = "AddImageFromBitmapHandle"
        Me.AddImageFromBitmapHandle.Size = New System.Drawing.Size(104, 23)
        Me.AddImageFromBitmapHandle.TabIndex = 3
        Me.AddImageFromBitmapHandle.Text = "From handle"
        Me.AddImageFromBitmapHandle.UseVisualStyleBackColor = False
        '
        'Label3
        '
        Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
        Me.Label3.Location = New System.Drawing.Point(184, 128)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(192, 88)
        Me.Label3.TabIndex = 4
        Me.Label3.Text = "Please copy some image to clipboard, then click ""From clipboard"""
        Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Label4
        '
        Me.Label4.Font = New System.Drawing.Font("Microsoft Sans Serif", 22.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
        Me.Label4.ForeColor = System.Drawing.SystemColors.ActiveCaption
        Me.Label4.Location = New System.Drawing.Point(68, 37)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(48, 40)
        Me.Label4.TabIndex = 5
        Me.Label4.Text = "1"
        Me.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Label5
        '
        Me.Label5.Font = New System.Drawing.Font("Microsoft Sans Serif", 22.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
        Me.Label5.ForeColor = System.Drawing.SystemColors.ActiveCaption
        Me.Label5.Location = New System.Drawing.Point(248, 37)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(48, 40)
        Me.Label5.TabIndex = 6
        Me.Label5.Text = "2"
        Me.Label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'AddImageFromClipboard
        '
        Me.AddImageFromClipboard.BackColor = System.Drawing.SystemColors.Control
        Me.AddImageFromClipboard.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
        Me.AddImageFromClipboard.Location = New System.Drawing.Point(224, 280)
        Me.AddImageFromClipboard.Name = "AddImageFromClipboard"
        Me.AddImageFromClipboard.Size = New System.Drawing.Size(104, 23)
        Me.AddImageFromClipboard.TabIndex = 7
        Me.AddImageFromClipboard.Text = "From clipboard"
        Me.AddImageFromClipboard.UseVisualStyleBackColor = False
        '
        'Label6
        '
        Me.Label6.Font = New System.Drawing.Font("Microsoft Sans Serif", 22.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
        Me.Label6.ForeColor = System.Drawing.SystemColors.ActiveCaption
        Me.Label6.Location = New System.Drawing.Point(440, 37)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(48, 40)
        Me.Label6.TabIndex = 8
        Me.Label6.Text = "3"
        Me.Label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Label7
        '
        Me.Label7.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
        Me.Label7.Location = New System.Drawing.Point(376, 128)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(192, 88)
        Me.Label7.TabIndex = 9
        Me.Label7.Text = "Read image file into memory, then convert"
        Me.Label7.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'AddImageFromBufferOrMemory
        '
        Me.AddImageFromBufferOrMemory.BackColor = System.Drawing.SystemColors.Control
        Me.AddImageFromBufferOrMemory.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
        Me.AddImageFromBufferOrMemory.Location = New System.Drawing.Point(424, 280)
        Me.AddImageFromBufferOrMemory.Name = "AddImageFromBufferOrMemory"
        Me.AddImageFromBufferOrMemory.Size = New System.Drawing.Size(104, 23)
        Me.AddImageFromBufferOrMemory.TabIndex = 10
        Me.AddImageFromBufferOrMemory.Text = "Convert"
        Me.AddImageFromBufferOrMemory.UseVisualStyleBackColor = False
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.Color.White
        Me.ClientSize = New System.Drawing.Size(576, 310)
        Me.Controls.Add(Me.AddImageFromBufferOrMemory)
        Me.Controls.Add(Me.Label7)
        Me.Controls.Add(Me.Label6)
        Me.Controls.Add(Me.AddImageFromClipboard)
        Me.Controls.Add(Me.Label5)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.AddImageFromBitmapHandle)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.PictureBox1)
        Me.Name = "Form1"
        Me.Text = "Working with images using VB.NET"
        CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

#End Region

    Private Function StartDoc() As PDFCreatorPilotLib.PDFDocument4
        Dim PDF As PDFCreatorPilotLib.PDFDocument4
        PDF = New PDFCreatorPilotLib.PDFDocument4
        PDF.SetLicenseData("demo@demo", "demo")
        Return PDF
    End Function

    Private Sub SavePDFDoc(ByVal PDF As PDFCreatorPilotLib.PDFDocument4, ByVal FileName As String)
        PDF.SaveToFile(FileName, True)
        PDF = Nothing
    End Sub

    Private Sub ConvertBitmapToPDF(ByVal bm As Bitmap, ByVal FileName As String)
        Dim PDF As PDFCreatorPilotLib.PDFDocument4
        Dim ImageIndex As Integer
        Dim BitmapHandle As IntPtr

        PDF = StartDoc()
        BitmapHandle = bm.GetHbitmap()
        ImageIndex = PDF.AddImageFromHandle(BitmapHandle.ToInt32())
        PDF.DrawImage(ImageIndex, 100, 100, bm.Width, bm.Height, 0)

        SavePDFDoc(PDF, FileName)

    End Sub

    Private Sub ConvertImageBytesToPDF(ByVal ImageBytes As Byte(), ByVal FileName As String)
        Dim PDF As PDFCreatorPilotLib.PDFDocument4
        Dim ImageIndex As Integer
        Dim bm As Bitmap

        PDF = StartDoc()

        ImageIndex = PDF.AddImageFromBLOB(CType(ImageBytes, Object))
        bm = New Bitmap(New System.IO.MemoryStream(ImageBytes))
        PDF.DrawImage(ImageIndex, 100, 100, bm.Width, bm.Height, 0)

        SavePDFDoc(PDF, FileName)

    End Sub

    Private Sub AddImageFromBitmapHandle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddImageFromBitmapHandle.Click

        Dim bm As Bitmap
        bm = PictureBox1.Image
        ConvertBitmapToPDF(bm, "AddImageFromBitmapHandle.pdf")

    End Sub

    Private Sub AddImageFromClipboard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddImageFromClipboard.Click
        Dim bm As Bitmap
        ' Declares an IDataObject to hold the data returned from the clipboard.
        ' Retrieves the data from the clipboard.
        Dim iData As IDataObject = Clipboard.GetDataObject()
        ' We need a Bitmap
        If iData.GetDataPresent(DataFormats.Bitmap) Then
            bm = CType(iData.GetData(DataFormats.Bitmap), Bitmap)
            ConvertBitmapToPDF(bm, "AddImageFromClipboard.pdf")
        Else
            MessageBox.Show("Sorry, but no image in clipboard", "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.CenterToScreen()
    End Sub

    Private Function GetImageBytes(ByVal FileName As String) As Byte()
        Dim fs As System.IO.FileStream
        Dim br As System.IO.BinaryReader
        ' You can use here any stream - for example, memory stream
        ' where image data generated from some another function
        fs = New System.IO.FileStream(FileName, IO.FileMode.Open, IO.FileAccess.Read)
        br = New System.IO.BinaryReader(fs)
        Dim bytes As Byte() = br.ReadBytes(fs.Length)
        Return bytes
    End Function

    Private Sub AddImageFromBuffer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddImageFromBufferOrMemory.Click
        Dim ImageFileName As String = New String("../images/laptop1.jpg")
        ConvertImageBytesToPDF(GetImageBytes(ImageFileName), "AddImageFromBuffer.pdf")
    End Sub

End Class

See Also

Reference

PDF Library Download Features Manual Tutorials FAQ Pricing HTML2PDF-X Pilot History In the Lab

 

 

PDF Library | Virtual Printer | Converters to PDF

Support | Blog | Forum | Contacts

© 2000-2012, Two Pilots