views:

83

answers:

1

Hi Everyone

I'm having issues with over-lapping tables using iTextSharp.

I have multiple tables(from gridviews) that I would like to write to pdf using iTextSharp.

I would like to only have a 10px gap between each table (vertical wise), and the height of the tables always differ.

Does anyone have an article I can read to help me out with this scenario? Or any advice? Absolute positioning isn't working for me.

+1  A: 

You can put each of your tables in an iTextSharp.text.Paragraph and use the Paragraph object's SpacingAfter property to create your gap.

Like this test method:

private static void DemoTableSpacing() {
    using (FileStream fs = new FileStream("SpacingTest.pdf", FileMode.Create)) {

        Document doc = new Document();
        PdfWriter.GetInstance(doc, fs);
        doc.Open();

        Paragraph paragraphTable1 = new Paragraph();
        paragraphTable1.SpacingAfter = 15f;

        PdfPTable table = new PdfPTable(3);
        PdfPCell cell = new PdfPCell(new Phrase("This is table 1"));
        cell.Colspan = 3;
        cell.HorizontalAlignment = 1;
        table.AddCell(cell);
        table.AddCell("Col 1 Row 1");
        table.AddCell("Col 2 Row 1");
        table.AddCell("Col 3 Row 1");
        //table.AddCell("Col 1 Row 2");
        //table.AddCell("Col 2 Row 2");
        //table.AddCell("Col 3 Row 2");
        paragraphTable1.Add(table);
        doc.Add(paragraphTable1);

        Paragraph paragraphTable2 = new Paragraph();
        paragraphTable2.SpacingAfter = 10f;

        table = new PdfPTable(3);
        cell = new PdfPCell(new Phrase("This is table 2"));
        cell.Colspan = 3;
        cell.HorizontalAlignment = 1;
        table.AddCell(cell);
        table.AddCell("Col 1 Row 1");
        table.AddCell("Col 2 Row 1");
        table.AddCell("Col 3 Row 1");
        table.AddCell("Col 1 Row 2");
        table.AddCell("Col 2 Row 2");
        table.AddCell("Col 3 Row 2");
        paragraphTable2.Add(table);
        doc.Add(paragraphTable2);
        doc.Close();
    }
}

This should show what you can do. Try adding and removing rows in that first table; you'll see that the space between the two tables is always there and doesn't change.

Jay Riggs
@Jay Riggs, Holy crap, thank you so much! lol.
Idealflip
@Ideal - No problem. The iTextSharp docs are horrible, as I'm sure you noticed.
Jay Riggs