views:

18

answers:

1

Hi

I'm trying to set up a simple 2 column page, write to the first column, then the second. However the code below places both paragraphs in the second column. The current column trace appears to be correct (first 0, then 1)

Any ideas what I'm doing wrong?

MultiColumnText columns = new MultiColumnText();

columns.AddSimpleColumn(0, 200);
columns.AddSimpleColumn(200, 400);

Paragraph para1 = new Paragraph("Para1");
columns.AddElement(para1);

Response.Write(columns.CurrentColumn);//traces 0

columns.NextColumn();

Response.Write(columns.CurrentColumn);//traces 1

Paragraph para2 = new Paragraph("Para2");
columns.AddElement(para2);

doc.Add(columns);

Many thanks

Oliver

A: 

I couldn't get NextColumn() to work wih a MultiColumnText object and I couldn't find any samples (in .NET) that do.

A MultiColumnText makes creating columns in a document relatively easy but in exchange you give up a lot control over layout. You can use the ColumnText object which gives you a great deal of control over column layout but requires more code.

Here's a simple but complete example of what you're trying to do using ColumnText:

    private void TestColumnText() {
        using (FileStream fs = new FileStream("ColumnTest.pdf", FileMode.Create)) {
            Document doc = new Document();
            PdfWriter writer = PdfWriter.GetInstance(doc, fs);
            doc.Open();

            PdfContentByte cb = writer.DirectContent;
            ColumnText ct = new ColumnText(cb);

            float columnWidth = 200f;
            float[] left1  = { doc.Left + 90f, doc.Top - 80f, doc.Left + 90f, doc.Top - 170f, doc.Left, doc.Top - 170f, doc.Left, doc.Bottom };
            float[] right1 = { doc.Left + columnWidth, doc.Top - 80f, doc.Left + columnWidth, doc.Bottom };
            float[] left2  = { doc.Right - columnWidth, doc.Top - 80f, doc.Right - columnWidth, doc.Bottom };
            float[] right2 = { doc.Right, doc.Top - 80f, doc.Right, doc.Bottom };

            // Add content for left column.
            ct.SetColumns(left1, right1);
            ct.AddText(new Paragraph("Para 1"));
            ct.Go();

            // Add content for right column.
            ct.SetColumns(left2, right2);
            ct.AddText(new Paragraph("Para 2"));
            ct.Go();

            doc.Close();
        }
    }

Warning: As I mentioned, this is a simple example and won't even serve as a starting point for you in what you're trying to do. The samples on the sites below (especially the first one) will help you:

http://www.mikesdotnetting.com/Article/89/iTextSharp-Page-Layout-with-Columns http://www.devshed.com/c/a/Java/Adding-Columns-With-iTextSharp

Jay Riggs