Java & J2EE Adding Columns With iTextSharp |
Creating simple columns of text Creating a simple column of text is more complex than creating a paragraph—you can't just pass a line of text to a constructor and then simply add the result to the document. There are more steps involved in the process. One of the first steps is to create a reference to the document's PdfWriter, rather than just passively creating it: PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("file.pdf",FileMode.Create)); With a document that just involves simple paragraphs, iTextSharp will automatically position things on the page. However, with more complex content, such as columns, positioning will have to be done manually. All of the user-positioned content in a page is stored in a PdfContentByte object. So, to add a column, we're going to need access to this. Access is provided through the DirectContent attribute of the PdfWriter: PdfContentByte cb = writer.DirectContent; Using PdfContentByte, we're able to create a column of text. A column of text is represented using the ColumnText class. So, to create a new column of text, we need to create a ColumnText object. The constructor accepts the PdfContentByte that we just retrieved: ColumnText column1 = new ColumnText(cb); The column isn't ready yet. It still needs to be placed on the page. To place it on the page, we need to give it a rectangle to occupy. The text will be placed within this rectangle. The rectangle is specified by passing two sets of coordinates. These coordinates correspond to two positions on the map. The rectangle will be drawn with the lower left corner at the location represented by the first set of coordinates and the upper right corner at the location represented by the second set of coordinates. Note that when specifying coordinates, the lower left corner of the page is the origin, just as in the first quadrant of a graph. To create a column two inches wide and three inches tall at the bottom left corner of the page, we'd do something like this: column1.SetSimpleColumn(72, 72, 72*3, 72*4); There are two ways to add text to the column. The first way will accept a Chunk or Phrase of text and add it to the column: column1.AddText(new Chunk("A chunk.")); If you're using this method, then the column has some control over the text. Through the column, you can, for example, justify the text, creating the classic newspaper column look: column1.Alignment = Element.ALIGN_JUSTIFIED; The second way accepts an IElement. Using this method, we can add a paragraph to the column (which is probably what you'll want to do in most cases): column1.AddElement(new Paragraph("A paragraph.")); In order to actually make the addition, however, the Go method must be called: column1.Go(); This actually writes the text to the document. Before, we had the column justify the text. Now, however, the paragraph must justify the text itself: Paragraph p = new Paragraph("The text in this paragraph" + " is justified."); p.Alignment = Element.ALIGN_JUSTIFIED; column1.AddElement(p); column1.Go();
blog comments powered by Disqus |
|
|
|
|
|
|
|