Insert Image and Text at Specified Position of PDF Document in C#

Image and text are basic but also essential elements of PDF documents. Sometimes, we want to insert an image or some text at the specified position which is apart from body content, for example, you may need to place your company logo at the bottom of a page. In this post, you will learn how to insert image and text at a location with specified X, Y coordinate in a PDF document in C#.

Tools we need

Using following namespaces

using Spire.Pdf;

using Spire.Pdf.Graphics;

using System.Drawing;

Using the code

Step 1: Initialize a new instance of PdfDocument class, add a new page to PDF document.

PdfDocument doc = new PdfDocument();

PdfPageBase page = doc.Pages.Add();

Step 2: Insert a resized image at the specified position in PDF document.

PdfImage image = PdfImage.FromFile(“Dragon.bmp”);

float width = image.Width * 0.9f;

float height = image.Height * 0.9f;

float x1 = 400;

float y1 = 650;

page.Canvas.DrawImage(image, x1, y1, width, height);

Step 3: Add text at the specified location with the specified Brush and Font objects.

String s = “DRAGON MEDIA”;

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

PdfBrush brush=new PdfSolidBrush(Color.Red);

float x2 = 400;

float y2 = 730;

page.Canvas.DrawString(s, font, brush, x2, y2);

Step 4: Save the changes to file.

doc.SaveToFile(“result.pdf”);

Output

blog2016-1-4

Advertisements

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