C# – How to Draw Image in PDF Document

Introduction

Image is often inserted in an article or a blog post. It can give readers an intuitive impression on some contents. For example, we may insert a chart image to show data information which is presented in articles or posts. Also, with a necessary and great image, the appearance will be more appealed to readers.

In this post, I will share my method about how to draw image in PDF document with C#. As is known, many electronic magazines and books are created by PDF. In these magazines and books, image plays an important role. Therefore, it is necessary to learn how to draw image in PDF if we want to create one own e-magazine or book.

Preparation

Before coding, we need to prepare an image. In my example, I just want to show a simple method so that I find an image from internet. Also, you can prepare some specified image according to the contents which you will write on document.

Then, I need to prepare another thing. I use one component, Spire.PDF in my example to help me realize this function. Therefore, after I create project, I need to add its dll file as reference.

Now, let’s start with Steps.

Steps

Firstly, create a PDF document and add page to draw contents.

Secondly, draw a title for this image to make readers know what image shows.

    a.Set format for this title, including font style, color and alignment.

    

  b.Set location for this title by confirming x and y axes value. In coding, x value is set as page.Canvas.ClientSize.Width/2 which means that the title will be put in center of page.

    

Thirdly, draw image in this page.

    a.Load image and set image size, including its width and height and location.

    

    b.Draw image according to images size and location.

    

Fourthly, save and launch file.

Main Coding:

using System;

using System.Drawing;

using Spire.Pdf;

using Spire.Pdf.Graphics;

namespace DrawImage

{

    class image

    {

        static void Main(string[] args)

        {

            //Create PDF and Add Page

            PdfDocument doc = new PdfDocument();

            PdfPageBase page = doc.Pages.Add();

            //Draw Text     

            PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 12f);

            PdfSolidBrush brush1 = new PdfSolidBrush(Color.DarkCyan);

            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center);

            float y = 20;

            page.Canvas.DrawString(“Washington, D.C. Metrorail”, font, brush1, page.Canvas.ClientSize.Width / 2, y, format);

            y = y + font.MeasureString(“Washington, D.C. Metrorail”, format).Height;

            y = y + 5;

            //Draw Image

            PdfImage image = PdfImage.FromFile(@”D:\work\My Documents\Metrorail.png”);

            float width = image.Width * 0.55f;

            float height = image.Height * 0.55f;

            float x = (page.Canvas.ClientSize.Width – width) / 2;

            page.Canvas.DrawImage(image, x, 40, width, height);

            //Save and Launch File

            doc.SaveToFile(“DrawImage.pdf”);

            doc.Close();

            System.Diagnostics.Process.Start(“DrawImage.pdf”);

        }

    }

}

Result Shown by Following:

__________________________________________________________________________

Click Here to Learn more about Spire.PDF

Click Here to Download Spire.PDF

Advertisements