Java & J2EE Page 3 - Adding Columns With iTextSharp |
In situations where a lot of text must be written to a document, there obviously needs to be multiple columns for the text to span across. While it's possible to do this manually using the ColumnText class, it's extra work. For situations where multiple columns are needed, it's best to use the MultiColumnText class, which supports multiple columns and will take care of much of the work involved for you. It's not even created and added to the page in the complicated manner that a ColumnText object is. Rather, it can be added to a document through the document's Add method. Creating an instance of MultiColumnText is very simple: MultiColumnText columns = new MultiColumnText(); Once an instance is created, columns can be added. A number of columns can be automatically set up using the AddRegularColumns method. This method will create evenly spaced columns within an area, with a fixed space in between each column. For example, suppose we had a letter-sized page and needed to divide it into two columns with an inch on either side of the page and a half inch in between the two columns. The columns would be set up like this: columns.AddRegularColumns(72, 72*7.5f, 36, 2); The first argument is the left position of the first column (1); the second argument is the right position of the last column (7.5one inch from the end of the page); the third argument is the amount of space in between each column (.5); and the fourth argument is the number of columns to be created. Text can be added to the columns very easily using the AddElement method: columns.AddElement(new Paragraph("A paragraph.")); Notice how there's no Go method to be called. AddElement does the job by itself. If there's not enough room in the column for the text, then the text will flow into the next column. If there's not enough room on the page for another column, then a new column will be created on a new page. As you can see, much of the work is done for us. The MultiColumnText object can be added to the document using the Add method of the Document object: doc.Add(columns); We can modify the last example to fit with MultiColumnText. However, let's add even more text to see how MultiColumnText handles multiple pages: using System; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; class Columns2 { public static void Main() { string text = "This is a paragraph. It is represented" + " by a Paragraph object in the iTextSharp " + "library. Here, we're creating paragraphs with " + "various styles in order to test out iTextSharp." + " This paragraph will take up multiple lines " + "and allow for a more complete example."; Document doc = new Document(PageSize.LETTER); PdfWriter.GetInstance(doc, new FileStream("2.pdf", FileMode.Create)); doc.Open(); MultiColumnText columns = new MultiColumnText(); columns.AddRegularColumns(72, 72*7.5f, 36, 2); for (int i = 0; i < 20; i++) { columns.AddElement(new Paragraph(text)); } doc.Add(columns); doc.Close(); } } Everything should work as expected: overflow text will simply go into new columns. As you can see, this example is actually much simpler than the last one. All of the hard work is handled by the library itself, rather than by code written by us.
blog comments powered by Disqus |
|
|
|
|
|
|
|