Adding Columns With iTextSharp

The iTextSharp library makes it very easy to create a PDF document with text. The text can be styled in various ways, aligned in various ways, indented and spaced in various ways, etc. When the PDF document must be printed out, the document’s readability can be improved by breaking the text into columns. The iTextSharp library provides support for columns, and in this article, we’re going to take a look at them.

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();


{mospagebreak title=An example}

So, here’s a short example of a program that creates a single column two inches wide, on the left side of a letter-sized page, with one inch to the left, top and bottom of the column:


using System;

using System.IO;


using iTextSharp.text;

using iTextSharp.text.pdf;


class Columns

{

 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 writer = PdfWriter.GetInstance(doc,

 new FileStream("1.pdf",FileMode.Create));

doc.Open();

 PdfContentByte cb = writer.DirectContent;

 ColumnText column1 = new ColumnText(cb);

column1.SetSimpleColumn(72, 72, 72*3, 72*10);

column1.AddElement(new Paragraph(text));

column1.Go();

doc.Close();

}

}


The above example creates a PDF file called 1.pdf. If you open it, you’ll notice that the text doesn’t occupy the whole column (it falls far short, actually). So, how would it look if more text were to be added? Place the two lines that add text in a loop with five iterations:


for (int i = 0; i < 5; i++)

{

column1.AddElement(new Paragraph(text));

column1.Go();

}


If you run the example now and look at the result, you’ll see that the column is indeed filled up with text, but not all of the text actually made it to the column. Once the column filled with text, the rest of the text got cut off. So, we’ve added more text than allowed for by the bounds of the column.

It’s actually possible to check if all of the text has been successfully written. The Go method returns an Integer value that indicates whether the write ended with no text left (success) or no space in the column left (failure). You can check this value with the bit flags ColumnText.NO_MORE_TEXT and ColumnText.NO_MORE_COLUMN:


int result = column1.Go();

if ((result & ColumnText.NO_MORE_TEXT) != 0)

{

 // All the text has been written

}

if ((result & ColumnText.NO_MORE_COLUMN) != 0)

{

 // The column is full

}


Of course, this raises the following question: what do we do if we run out of space within a column? Right now, the extra text is essentially discarded, but this is unacceptable. Common sense tells us that we need yet another column to contain the overflow text.

{mospagebreak title=Creating multiple columns}

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.5”—one 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.

{mospagebreak title=Manually adding columns}

The AddRegularColumns method automatically sets up columns. However, there are some cases where more control is needed. In these cases, it’s possible to manually add columns to a MultiColumnText object, and it’s not very difficult to do, thankfully.

Simple columns can be added using the AddSimpleColumn method. This method takes two arguments: the distance of the left side of the column from the left side of the page, and the distance of the right side of the column from the left side of the page. Using this method, it would be possible to create two unequal columns on a page:


columns.AddSimpleColumn(72, 72 * 3);

columns.AddSimpleColumn(72 * 4, 72 * 7.5f);


In the example above, one column is created that is two inches wide, and another column is created that is three and a half inches wide. Once the columns are created, though, the basic behavior is the same. Text will automatically flow from one column to another on as many pages as needed.

Now you should be familiar with the basics of column creation in iTextSharp. Try creating a few examples of your own with columns.

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye