Kᴀτᴢ
Kᴀτᴢ

Reputation: 2176

Formatting itextsharp table

I have a given table with 2 columns which I try to export to pdf.

This is my code:

PdfPTable tableUebersicht = new PdfPTable(dtUebersicht100.Columns.Count);
tableUebersicht.SetWidths(new float[] { 250, 420 });
tableUebersicht.LockedWidth = true;
tableUebersicht.TotalWidth = 500f;

foreach (DataColumn c in dtUebersicht100.Columns)
{
    PdfPCell Spalte = new PdfPCell(new Phrase(c.ColumnName, VerdanaFont));
    Spalte.HorizontalAlignment = Element.ALIGN_CENTER;
    Spalte.VerticalAlignment = Element.ALIGN_MIDDLE;
    table.AddCell(Spalte);
}
foreach (DataRow dr in dtUebersicht100.Rows)
{
    PdfPCell Spalte0 = new PdfPCell(new Phrase(dr[0].ToString(), VerdanaFont));
    Spalte0.HorizontalAlignment = Element.ALIGN_CENTER;
    Spalte0.VerticalAlignment = Element.ALIGN_MIDDLE;

    double Double1 = Convert.ToDouble(dr[1].ToString());
    PdfPCell Spalte1 = new PdfPCell(new Phrase(string.Format("{0:C2}", Double1), VerdanaFont));
    Spalte1.HorizontalAlignment = Element.ALIGN_RIGHT;
    Spalte1.VerticalAlignment = Element.ALIGN_MIDDLE;

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

table.WriteSelectedRows(0, -1, 35, 757, cb);

The output looks as followed: enter image description here

As you can see the table direction is from left to right and not from up to down.

I would like to have a table like this where the direction is up to down, on the bottom of the page get up and continue left beside, every second row colored:

enter image description here

Upvotes: 0

Views: 1744

Answers (1)

Joris Schellekens
Joris Schellekens

Reputation: 9012

overview of the solution

  • look at the table header, there are 4 columns
  • generate an iText table object, with 4 columns
  • add cells in the order left to right, top to bottom
    in your case that would be:
    Verkaufernummer, Betrag, Verkaufernummer, Betrag
    1, 55.04, 50, 3.5

  • keep track of the row you are currently rendering on, depending on the row, set the background color of the cell (in your logic even vs odd)

Upvotes: 3

Related Questions