How To Use VB.NET To Insert Image In PDF
Sign in

How to Use VB.NET to Insert Image in PDF

After blog is released, people would like to write things happened in their daily life, professional research, travelling experience and so on. Also, people may insert images in blog posts for making it be more appealed or showing contents intuitively, especially in journey dairy. Besides, images are often used in electronic books and magazines.

Sometimes, we want to create own blog posts or electronic books. At that time, we may need to learn how to insert images. It is simple to insert in posts because each blog offers the tools for inserting image. But how should we do to insert in electronic books?

As is known, the format of most electronic books is PDF. Therefore, we need to learn how to insert image in PDF firstly.

Generally speaking, Adobe Acrobat can be used to meet our requirements. Download and install the tool and follow the guide which is written by some senior users and shown online.

But next, the method I want to introduce is helpful for programmers. The method can be used to insert image in many PDF documents at one time.

What’s more, in this method, one component, Spire.PDF is used. You need to download and install it. Then add DLL file as reference before using the following code.

The following code shows how to insert image in PDF with VB.NET.

Imports System.Drawing

Imports Spire.Pdf

Imports Spire.Pdf.Graphics

 

Namespace Image

    Friend Class Program

        Shared Sub Main(ByVal args() As String)

            'Create a pdf document.

            Dim doc As New PdfDocument()

 

            ' Create one page

            Dim page As PdfPageBase = doc.Pages.Add()

 

            'Draw the text

            page.Canvas.DrawString("Hello, World!", _

                                   New PdfFont(PdfFontFamily.Helvetica, 30.0F), _

                                   New PdfSolidBrush(Color.Black), 10, 10)

            'Draw the image

            Dim image As PdfImage = PdfImage.FromFile("SalesReportChart.png")

            Dim width As Single = image.Width * 0.75F

            Dim height As Single = image.Height * 0.75F

            Dim x As Single = (page.Canvas.ClientSize.Width - width) / 2

 

            page.Canvas.DrawImage(image, x, 60, width, height)

 

            'Save pdf file.

            doc.SaveToFile("Image.pdf")

            doc.Close()

 

            'Launching the Pdf file.

            Process.Start("Image.pdf")

        End Sub

    End Class

End Namespace

 

start_blog_img