Basic PDF Creation Using iTextSharp - Part II

Part II - Writing text, images and simple graphics in the document.

This is the second part of this articles series about creating simple PDFs using iTextSharp. In the first part we looked at how to create the file and add meta information. We also added a simple line of text in the document. As there are several ways of placing elements in the document, I will describe two in this article; The flow layout and exact positioning.

Flow layout

The flow layout mode just puts all of the added elements in a left to right, line by line manner. This was how we put Hello World to the document in part I. When you write text like this you don't need to keep track of the page breaks, this is handled by the document itself.
Exact positioning

Here you set the exact coordinates (Y & X) to place elements in the document and this is the way we will continue to place text in this tutorial. Here you need to control the page bounds yourself, keeping track of when it's time to switch page. This gives us better control over how we create documents.

To enable exact positioning of text and other elements as described above you need to use the PdfContentByte class which enables you to place it where you want to. You get hold of a reference with this simple line of code:
  1. PdfContentByte cb = writer.DirectContent;  
Before you can write anything in the document using the direct content you need to do two things; use the BeginText method and then set the font and the font size, like this:
  1. cb.BeginText();  
  2. cb.SetFontAndSize(f_cn, 9);  

You can create fonts several ways, here shown how to by using the BaseFont class.

  1. BaseFont f_cb = BaseFont.CreateFont("c:\\windows\\fonts\\calibrib.ttf", BaseFont.CP1252, BaseFont.NOT_EMBEDDED);  
  3. BaseFont f_cn = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.CP1252, BaseFont.NOT_EMBEDDED);  
Note that the document will be corrupt if you use fonts that aren't installed on your machine. There are several other ways of doing this, but I don't cover that here. To be sure that the target machine can open the file, you could change BaseFont.NOT_EMBEDDED to BaseFont.EMBEDDED which includes the font in the file, but be careful, if you do then the size of the file increases very much!

Okay, now you can place text where you want to with the following code:

  1. cb.SetTextMatrix(100, 100); // Left, Top  
  2. cb.ShowText("Hello World");  First
First we use the SetTextMatrix method to place the position and then we use the ShowText method to write the actual text. Before we can end the document we must use the method end text, like this:
  1. cb.EndText();  
Document coordinates

One tricky thing to keep track of is that the y coordinate in the SetTextMatrix are a bit different from normal use of graphic coordinates, it works from the top bottom of the document, is backward. To visualize this, let's do two simple for loops to draw the positions in the document.
  1. int row = 1;  
  2. for (int y = 0; y != 70; y++)  
  3. {  
  4.    cb.SetTextMatrix(10, row);  
  5.    cb.ShowText("Y: " + row.ToString());  
  6.    row += 12; // The spacing between the rows is set to 12 "points"  
  7. }  
  8. int col = 35;  
  9. for (int x = 0; x != 22; x++)  
  10. {  
  11.    cb.SetTextMatrix(col, 829);  
  12.    cb.ShowText("X: " + col.ToString());  
  13.    col += 25; // The spacing between the columns is set to 25 "points"  
  14. }  
The first one starts at Y (vertical) position 1.


and works its way up to the top with the line spacing of 12 points. The last position is here the Y: 829.


The second loop creates the left to right "column" illustration, and that one goes from right to left as normal positioning of coordinates are used. This is normally something I keep in printing on my desktop when I design PDF's using iTextSharp, it can otherwise be many test documents created before everything falls into the right place!

Text alignment

You can align the text by using the ShowTextAligned method which takes an alignment parameter, like this:
  1. cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "This text is left aligned", 200, 800, 0);  
  2. cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, "This text is right aligned", 200, 788, 0);


Drawing lines

To draw a line with exact positioning you need to first set the line width, like this: cb.SetLineWidth(0f). Then you set the starting point, like this: cb.MoveTo(30, 650) and the ending point, like this: cb.LineTo(570, 650).

To draw the line in the document, use the Stroke method like this cb.Stroke().

Drawing lines

To draw a rectangle you can use the Rectangle method, like this:

cb.Rectangle(400, 700, 100, 100);

NOTE: You need to call the end text method before you write lines or add images in the document when you use exact positioning, if you are doing it while writing text. One way to keep track of this is to start out with the graphics before you start placing text!


There are several ways of placing images, but I will just show the simplest one, using exact positioning.  First, create a instance of the image class, like this:
  1. iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("");  
Or you could reference a file on disk like this.
  1. iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(Server.MapPath("img.png"));
To place the image you first set the position and then add the image to the content byte:


  1. img.SetAbsolutePosition(50, 647);  
  2. cb.AddImage(img);  
You can scale the size using the ScaleAbsolute or ScalePercent methods like this:
  1. img.ScaleAbsolute(216, 70);  
  2. img.ScalePercent(50);  
Okay, that's it!

Now you have the basic knowledge of how to place text, graphics and images in a PDF document using iTextSharp. Please, check out Part III where I go thru a simple process creating a PDF invoice from a XML file.

Part I, Part III

Similar Articles