TallPDF.NET 4.0
Generate PDF Manipulate PDF

This article shows how to use the EndPage event to draw vertical lines between columns.

The following graphic shows a typical article layout consisting of three columns. (Source: CNN website "The human cost of chocolate")

First we set the number of columns in the section to three. Then we add all the paragraphs to the section (these will flow in the 3 columns). Then we subscribe to the EndPage event (fired at the end of each page). Inside the EndPage event handler we will draw the lines.

Here is the code:

static void Main(string[] args) { Document doc = new Document();

// define the section layout as 3 columns Section section = new Section(); section.Columns.Count = 3; doc.Sections.Add(section); section.Columns[0].Width = section.Columns[1].Width = section.Columns[2].Width = 155; section.Columns.Spacing = 4;

// add content for (int i = 0; i < 10; i++) { TextParagraph text = new TextParagraph(); text.Fragments.Add(new Fragment("It may be unthinkable that the chocolate we enjoy could come" +
"from the hands of children working as slaves. In Ivory Coast and other cocoa-producing " + "countries, there are an estimated 100,000 children working the fields, many against their" + "will, to create the chocolate delicacies enjoyed by Western countries.")); text.SpacingAfter = 3.0; section.Paragraphs.Add(text); }

// setup the event handler for the EndPage event // we are going to draw the lines at the end of the page section.EndPage += new EndPageEventHandler(drawColumnBorders); }

//We draw the column lines in the EndPage event handler static void drawColumnBorders(Section section, EndPageEventArgs e) { Pen pen = new Pen(System.Drawing.Color.Black, 0.5);

// draw a line between each column pair double bottom = section.Margin.Bottom; double top = section.PageSize.Height - section.Margin.Top; double x = section.Margin.Left + section.Columns[0].Width + section.Columns.Spacing / 2; for (int i = 0; i < section.Columns.Count - 1; i++) { // draw a vertical line at the right side of the i-th column e.Foreground.Add(new LineShape(x, top, x, bottom, pen)); x += section.Columns[i].Width + section.Columns[i].Spacing; } }