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

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

6 thoughts on “C# – How to Draw Image in PDF Document”

  1. You actually make it seem so easy with your presentation but I find this topic to be really something that I think I would never understand. It seems too complex and extremely broad for me. I’m looking forward for your next post, I’ll try to get the hang of it!

  2. Hmm it appears like your blog ate my first comment (it was extremely long) so I guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying your blog.

    I as well am an aspiring blog writer but I’m still new to everything. Do you have any tips and hints for first-time blog writers? I’d
    certainly appreciate it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s