Tuesday, 07 February 2012 06:44

Draw PDF Image in C#, VB.NET

Written by  support iceblue
Rate this item
(0 votes)

PDF image is an important part of PDF document, especially in some material instructions, a small image can show useful information that even thousands of words cannot explain. While PDF images are also used to make the document more attractive. This section will show you a solution to draw PDF image via a .NET PDF component in C#, VB.NET.

Spire.PDF for .NET, a .NET PDF library for handling PDF file, enables you to draw image in PDF with C#, VB.NET. Using this PDF component, you can not only set your image size in the PDF file but also place the image at a specific position. In my solution, I set the transparency to avoid covering PDF text by PDF image. You can view the target PDF file as below picture:

Draw Image in PDF

Here you can download Spire.PDF for .NET, after installing it on system and adding Spire.Pdf dll, you can start your PDF task by following steps.

Detail Code:

There are mainly three simple steps in my solution. The first step requires you to load an image, which can be achieved by this method: System.Drawing.Image FromFile(string filename). The second step needs you to set image size to match the PDF appropriately through setting image width and height via System.Drawing.Size(int width, int height). The last step is designed to set the PDF image position and draw image in PDF. You can call DrawImage(PdfImage image, PointF point) method in which there are with two parameters passed. The second parameter is mainly responsible for setting image to a specific position. If necessary, you also can set transparency by SetTransparency(float alpha). Please see the whole code here.

using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;

namespace PDFimage
    class Program
        static void Main(string[] args)
            //Create a pdf document.
            PdfDocument doc = new PdfDocument();
            Image image = Image.FromFile(@"..\images.jpg");

            //adjust image size
            int width=image.Width;
            int height=image.Height;
            float schale=1.5f;
            Size size=new Size ((int)(width*schale),(int)(height*schale));
            Bitmap schaleImage = new Bitmap(image, size);

            //insert image into the first PDF page at specific position
            PdfImage pdfImage = PdfImage.FromImage(schaleImage);
            PdfPageBase page0 = doc.Pages[0];
            PointF position = new PointF((page0.Canvas.ClientSize.Width - schaleImage.Width) / 2, 210);
            page0.Canvas.DrawImage(pdfImage, position);

Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics

Namespace PDFimage
    Class Program
        Private Shared Sub Main(ByVal args() As String)
            'Create a pdf document.
            Dim doc As PdfDocument = New PdfDocument
            Dim image As Image = Image.FromFile("..\images.jpg")
            'adjust image size
            Dim width As Integer = image.Width
            Dim height As Integer = image.Height
            Dim schale As Single = 1.5!
            Dim size As Size = New Size(CType((width * schale),Integer), CType((height * schale),Integer))
            Dim schaleImage As Bitmap = New Bitmap(image, size)
            'insert image into the first PDF page at specific position
            Dim pdfImage As PdfImage = PdfImage.FromImage(schaleImage)
            Dim page0 As PdfPageBase = doc.Pages(0)
            Dim position As PointF = New PointF(((page0.Canvas.ClientSize.Width - schaleImage.Width)  _
                            / 2), 210)
            page0.Canvas.DrawImage(pdfImage, position)
        End Sub
    End Class
End Namespace

Spire.PDF for .NET allows developers to perform a wide range of tasks directly without Adobe Acrobat such as create, edit, read and manage PDF documents in C#,VB.NET.

Additional Info

  • tutorial_title: Draw PDF Image
Last modified on Tuesday, 15 July 2014 07:38