Kᴀτᴢ
Kᴀτᴢ

Reputation: 2176

Continue PDFtable on next page with headers

this is my code:

PdfPTable tableSumme = new PdfPTable(dtUebersicht.Columns.Count-1);
widthsSumme = new float[] { 4.2f, 5f, 5f, 5f, 5f };
tableSumme.SetWidths(widthsSumme);
tableSumme.WidthPercentage = 100;
tableSumme.TotalWidth = 500f;


foreach (DataColumn c in dtUebersicht.Columns)
    {
PdfPCell Spalte = new PdfPCell(new Phrase(c.ColumnName, VerdanaFont));
                            Spalte.HorizontalAlignment = Element.ALIGN_CENTER;
                            Spalte.VerticalAlignment = Element.ALIGN_MIDDLE;
                            tableSumme.AddCell(Spalte);
    }

PdfContentByte cbSumme = writerSumme.DirectContent;


foreach (DataRow dr in dtUebersicht.Rows)
{
PdfPCell Spalte0 = new PdfPCell(new Phrase(dr[0].ToString(), VerdanaFont));
                                    Spalte0.HorizontalAlignment = Element.ALIGN_CENTER;
                                    Spalte0.VerticalAlignment = Element.ALIGN_MIDDLE;

PdfPCell Spalte1 = new PdfPCell(new Phrase(dr[1].ToString(), VerdanaFont));
                                    Spalte1.HorizontalAlignment = Element.ALIGN_CENTER;
                                    Spalte1.VerticalAlignment = Element.ALIGN_MIDDLE;

tableSumme.AddCell(Spalte0);
tableSumme.AddCell(Spalte1);
}

tableSumme.WriteSelectedRows(0, -1, 35, 757, cbSumme);

This gives me a PDF with one page and the data on it. The data is longer than the page so I want to insert every 50th row a new page. How can I solve this?

A simple

 if (Rowindex % 50 == 0)
   { documentSumme.NewPage(); }

doesn't work. Thanks

Upvotes: 0

Views: 1525

Answers (1)

Ben
Ben

Reputation: 763

Here is a small example following the comment by @Bruno Lowagie. When the end of the page is reached before all rows are added, they will be added on the next page.

Document doc = new Document();
FileStream fs = new FileStream(@"your path", FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.Open();

List<string> columns = new List<string> {"col1", "col2", "col3", "col4", "col5"};

PdfPTable table = new PdfPTable(columns.Count);
table.SetWidths(new[] { 5f, 5f, 5f, 5f, 5f });
table.WidthPercentage = 100;
table.TotalWidth = 500f;
table.HeaderRows = 1;

foreach (string col in columns)
{
    PdfPCell cell = new PdfPCell(new Phrase(col));
    table.AddCell(cell);
}

for (int i = 0; i < 100; i++)
{
    for (int j = 0; j < columns.Count; j++)
    {
        PdfPCell cell = new PdfPCell(new Phrase($"{i},{j}"));
        table.AddCell(cell);
    }
}

doc.Add(table);
doc.Close();

Upvotes: 2

Related Questions