iTextSharp – HTML to PDF – Positioning Text

The next series of things I’m going to introduce about using iTextSharp are all going to lead toward taking HTML text and placing it on the PDF document.

There are several items we need to cover before we even get to the part about converting the text from HTML to PDF text.  The first is placing the text on the document where it is supposed to be.

Once again, we are building on previous articles about using iTextSharp.  So if you are just jumping in, you might want to go take a look at the other articles.  You can find a list at the bottom of this post.

To place a block of text on the screen that is going to have multiple formats in it (bold, underline, etc) I use the ColumnText class.  This allows me to specify the rectangle or, if I want, some irregular shape, to place the text in.  I handle determining where this rectangle is on the page in the same way that I determine where an image should go.  I have the designer place a form field on the screen and then I use that to get my coordinates.

float[] fieldPosition = null;
fieldPosition = 
    fields.GetFieldPositions("fieldNameInThePDF");
left = fieldPosition[1];
right = fieldPosition[3];
top = fieldPosition[4];
bottom = fieldPosition[2];
if (rotation == 90)
{
    left = fieldPosition[2];
    right = fieldPosition[4];
    top = pageSize.Right - fieldPosition[1];
    bottom = pageSize.Right - fieldPosition[3];
}

Once I have the position, the next thing I need to do is to create my ColumnText object.  This requires the same ContentByte object that we used for the images.

PdfContentByte over = stamp.GetOverContent(1);
ColumnText ct = new ColumnText(over);

And now I can set the rectangle to print into.

ct.SetSimpleColumn(left, bottom, right, top,
    15, Element.ALIGN_LEFT);

The 15 represents the leading you want (space between characters vertically). You may need to adjust that number.

Once you have your rectangle, you can add paragraphs to it.  Paragraphs are composed of smaller units called chunks that can be formatted.  If you want a paragraph that is all formatted the same you can make a call that looks like this.

Paragraph p = new Paragraph(
    new Chunk("Some Text here", 
        FontFactory.GetFont(
          "Arial", 14, Font.BOLD, Color.RED)));

and then add the paragraph to your rectangle

ct.AddElement(p);

 

 

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • iTextSharp – HTML to PDF – PrerequisitesiTextSharp – HTML to PDF – Prerequisites Before we get into the nitty gritty of parsing the HTML so that we can create PDF code from it, it is important that we develop the concept of how text layout works in iTextSharp.  […]
  • iTextSharp – HTML to PDF – Parsing HTMLiTextSharp – HTML to PDF – Parsing HTML Now that we have the HTML cleaned up, the next thing we will want to do is to parse the HTML. In my actual code for this, I parse the HTML and create the PDF at the same time, but for […]
  • iTextSharp – HTML to PDF – Writing the PDFiTextSharp – HTML to PDF – Writing the PDF Last week we parsed the HTML and created code that keeps track of the various attributes we are going to need when we create the PDF.  Today we will finish the code and create the […]
  • iTextSharp – Adding ImagesiTextSharp – Adding Images Last week I showed how to use form fields to control placement of dynamic data. But what if you want to dynamically place images in your PDF?  You can stuff them into a form field […]
  • iTextSharp – HTML to PDF – Cleaning HTMLiTextSharp – HTML to PDF – Cleaning HTML The last prerequisite step prior to actually converting our HTML into PDF code is to clean up the HTML.The method I use takes advantage of the XML parser in .NET but in order to […]

About Dave Bush

Dave Bush is a .NET programmer and Certified ScrumMaster who is passionate about managing risk as it relates to developing software. When he is not writing or speaking about topics related to Application Lifecycle Risk Management (ALRM), he is an example to his peers as he develops web sites in the ASP.NET environment using industry best practices.

2 Pingbacks/Trackbacks