C# tutorial: draw graphics in PDF document


Draw graphics in PDF document

iTextSharp allows you to place and style graphical elements in a PDF document. These graphical elements can be text, geometric shapes, and images. They can be placed in any locations on the PDF page. You can apply font name, size, style, color, and orientation to the text elements.



1. Draw text

To draw a text on the PDF page, you can use the showTextAligned(PdfContentByte canvas, int alignment, Phrase phrase, float x, float y, float rotation)
method of the ColumnText class. By using this method, it is easy to align the text, display the text at a location, and rotate the text. You can create a font object to be applied to the text by using the Font class.

Example:

//get DirectContent object for the document
PdfContentByte content = writer.DirectContent;

//create a font object
Font font=new Font( Font.FontFamily.HELVETICA, 18, Font.BOLDITALIC, new BaseColor(0, 0, 255) );
//create a phrase object containing the text to show in the specified font
Phrase phrase=new Phrase(0.0f, "Light House",font);

//draw the text

ColumnText.ShowTextAligned(content, Element.ALIGN_CENTER, phrase, PageSize.A4.Width / 4, PageSize.A4.Height/3 , 45.0f);

2. Draw geometric shapes and images

You can draw different geometric shapes such as lines, rectanges, and ellipses, and images on the PDF page by using the PdfContentByte class. The table below shows you the methods of the PdfContentByte class that can be used to draw the graphical elements.

Shape Methods
Line MoveTo(float x,float y) and LineTo(float x,float y)
Rectangle Rectangle(float x, float y, float w, float h)
Ellipse Ellipse(float x1, float y1, float x2, float y2)
Image AddImage(Image image, float a, float b, float c, float d, float e, float f)

Example:

//get DirectContentUnder object under this document
PdfContentByte contentunder = writer.DirectContentUnder;
//draw a line
contentunder.SetColorStroke(BaseColor.BLUE);
contentunder.MoveTo(72.0f, PageSize.A4.Height - 10);
contentunder.LineTo(300.0f, PageSize.A4.Height - 10);
contentunder.Stroke();

//draw an ellipse filled in pink color
contentunder.SetColorFill(BaseColor.PINK);
contentunder.Ellipse(PageSize.A4.Width / 7, PageSize.A4.Height-20, PageSize.A4.Width / 2, PageSize.A4.Height*3/4);
contentunder.Fill();


//draw a rectangle in pink color
contentunder.SetColorStroke(BaseColor.RED);
contentunder.Rectangle(PageSize.A4.Width / 7, PageSize.A4.Height*3/4, PageSize.A4.Width / 2, PageSize.A4.Height / 5);
contentunder.Stroke();


//draw image from a file
iTextSharp.text.Image img=new Jpeg(imageToByteArray(System.Drawing.Image.FromFile("d://Lighthouse.jpg")));
contentunder.AddImage(img, img.Width / 2, 0, 0, img.Height/2, 36.0f, 100.0f);

draw graphics on PDF pages

You can download the complete example code of this tutorial from here.



Comments

arun comment

 arun

good


2016-06-02
arun comment

 arun

good


2016-06-02
javeed  comment

 javeed

Can you please add on how to retrieve the values from the database and create a pdf using it.


2016-03-25
MOHIT PATEL comment

 MOHIT PATEL

IT IS VERY NICE WEBSITE FOR GAVE C# FREE CODING TOOLS......,


2014-11-07
CAPTCHA image



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.