1

I have used the following code to fill my PdfPTable:

  public PdfPTable GetTable(List<hsp_Narudzbe_IzdavanjeFakture4_Result> proizvodi)
    {
        PdfPTable table = new PdfPTable(5);
        table.WidthPercentage = 100F;
        table.DefaultCell.UseAscender = true;
        table.DefaultCell.UseDescender = true;
        Font f = new Font(Font.FontFamily.HELVETICA, 13);
        f.Color = BaseColor.WHITE;
        PdfPCell cell = new PdfPCell(new Phrase("Stavke narudžbe: ", f));
        cell.BackgroundColor = BaseColor.BLACK;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        cell.Colspan = 5;
        table.AddCell(cell);
        for (int i = 0; i < 2; i++)
        {
            table.AddCell("Redni broj");
            table.AddCell("Proizvod");
            table.AddCell("Cijena");
            table.AddCell("Šifra proizvoda");
            table.AddCell("Kolicina");
        }
        table.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY;
        table.HeaderRows = 3;
        table.FooterRows = 1;
        int broj = 0;
        table.DeleteLastRow();


        foreach (hsp_Narudzbe_IzdavanjeFakture4_Result p in proizvodi)
        {
            broj++;
            table.AddCell(broj.ToString());
            table.AddCell(p.Naziv);
            table.AddCell(p.Cijena);
            table.AddCell(p.Sifra);
            table.AddCell(p.Kolicina.ToString());
        }
        float[] widths = { 15.00F, 15.00F,15.00F,15.00F};
        table.GetRow(1).SetWidths(widths);


        return table;
    }

Little explanation of code:

broj++; represents the row number which gets incremented each time a new row is added. That way I get the number for each row.

Now my question is: how can I manipulate width of the first column (which represents the row number). I have tried the following code:

float[] widths = { 15.00F, 15.00F,15.00F,15.00F};
table.GetRow(1).SetWidths(widths);

Can someone help me out with this ?

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
perkes456
  • 1,163
  • 4
  • 25
  • 49
  • Column widths are defined for the *whole table*. You *can not* and **should not** change the width of a column at the level of a single row. Please rephrase your question so that we get a better understanding of the nature of your question. What do you want to achieve? Maybe you want to nest a table in a cell that spans all columns... – Bruno Lowagie Oct 29 '14 at 11:08
  • Well first I would like the text in the cells to be in the center of the cells, not on the left side as it is right now. Second I would like that the first row has lowest width, because each cells in it contains just a number (1,2,3,4...), other cells in 2,3,4 and 5th row contain text (and they should have longer width). I hope I rephrased the question so that you can understand it now. P.S. I can't thank you enough for your answer yesterday on my question, you sir are a genius :)! – perkes456 Oct 29 '14 at 11:15
  • OK, give me some time and I'll see if I can make a simple example. – Bruno Lowagie Oct 29 '14 at 11:21
  • Okay sir. Thanks a lot once again! – perkes456 Oct 29 '14 at 11:26
  • It is custom to *accept* the answer that was most useful. If you do not accept an answer, it is as if the question didn't get a correct answer. – Bruno Lowagie Oct 29 '14 at 11:46

2 Answers2

5

Combining your original post and your comment, I see two questions:

  1. You want to create a table where the first column isn't as wide as the other columns.
  2. You want the content in those columns to be center-aligned.

I have written a small sample named ColumnWidthExample that should result in such a PDF:

enter image description here

This is the Java code that produces this PDF:

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document(PageSize.A4.rotate());
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    float[] columnWidths = {1, 5, 5};
    PdfPTable table = new PdfPTable(columnWidths);
    table.setWidthPercentage(100);
    table.getDefaultCell().setUseAscender(true);
    table.getDefaultCell().setUseDescender(true);
    Font f = new Font(FontFamily.HELVETICA, 13, Font.NORMAL, GrayColor.GRAYWHITE);
    PdfPCell cell = new PdfPCell(new Phrase("This is a header", f));
    cell.setBackgroundColor(GrayColor.GRAYBLACK);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setColspan(3);
    table.addCell(cell);
    table.getDefaultCell().setBackgroundColor(new GrayColor(0.75f));
    for (int i = 0; i < 2; i++) {
        table.addCell("#");
        table.addCell("Key");
        table.addCell("Value");
    }
    table.setHeaderRows(3);
    table.setFooterRows(1);
    table.getDefaultCell().setBackgroundColor(GrayColor.GRAYWHITE);
    table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
    for (int counter = 1; counter < 101; counter++) {
        table.addCell(String.valueOf(counter));
        table.addCell("key " + counter);
        table.addCell("value " + counter);
    }
    document.add(table);
    document.close();
}

It should be fairly simple to adapt this code to C#.

The first problem is solved by defining the column widths at the level of the table. For instance like this (there are other ways to get the same result):

float[] columnWidths = {1, 5, 5};
PdfPTable table = new PdfPTable(columnWidths);

The values 1, 5, and 5 aren't absolute widths. As we define the width of the table as 100% of the available width within the margins of the page, iText will divide the available width by 11 (1 + 5 + 5) and the first column will measure 1 time this value, whereas column two and three will measure 5 times this value.

Your second problem can be solved by changing the horizontal alignment. If you work with an explicit PdfPCell object, you need to change the value at the cell level, as is done in the following line:

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

If you leave it to iText to create the PdfPCell object, you need to change the alignment at the level of the default cell:

table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);

Note that the default cell properties are not applied when you add a PdfPCell object to the table. In that case, the properties of that PdfPCell are used.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
0

Okay so I managed to center the cell's text in every column, now I just can't figure out how to make first row to have smallest width...

Here is the code

public PdfPTable GetTable(List<hsp_Narudzbe_IzdavanjeFakture4_Result> proizvodi)
    {
        PdfPTable table = new PdfPTable(5);
        table.WidthPercentage = 100F;
        table.DefaultCell.UseAscender = true;
        table.DefaultCell.UseDescender = true;
        Font f = new Font(Font.FontFamily.HELVETICA, 13);
        f.Color = BaseColor.WHITE;
        PdfPCell cell = new PdfPCell(new Phrase("Stavke narudžbe: ", f));
        cell.BackgroundColor = BaseColor.BLACK;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        cell.Colspan = 5;
        table.AddCell(cell);
        for (int i = 0; i < 2; i++)
        {
            table.AddCell(new PdfPCell(new Phrase("Redni broj"))
                {
                    VerticalAlignment = Element.ALIGN_MIDDLE,
                    HorizontalAlignment = Element.ALIGN_CENTER,
                    BackgroundColor = BaseColor.LIGHT_GRAY
                });
            table.AddCell(new PdfPCell(new Phrase("Proizvod"))
            {
                VerticalAlignment = Element.ALIGN_MIDDLE,
                HorizontalAlignment = Element.ALIGN_CENTER,
                BackgroundColor = BaseColor.LIGHT_GRAY
            });
            table.AddCell(new PdfPCell(new Phrase("Cijena"))
            {
                VerticalAlignment = Element.ALIGN_MIDDLE,
                HorizontalAlignment = Element.ALIGN_CENTER,
                BackgroundColor = BaseColor.LIGHT_GRAY
            });
            table.AddCell(new PdfPCell(new Phrase("Šifra"))
            {
                VerticalAlignment = Element.ALIGN_MIDDLE,
                HorizontalAlignment = Element.ALIGN_CENTER,
                BackgroundColor = BaseColor.LIGHT_GRAY
            });
            table.AddCell(new PdfPCell(new Phrase("Kolicina"))
            {
                VerticalAlignment = Element.ALIGN_MIDDLE,
                HorizontalAlignment = Element.ALIGN_CENTER,
                BackgroundColor = BaseColor.LIGHT_GRAY
            });
        }
        table.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY;
        table.HeaderRows = 3;
        table.FooterRows = 1;
        int broj = 0;
        table.DeleteLastRow();
        foreach (hsp_Narudzbe_IzdavanjeFakture4_Result p in proizvodi)
        {
            broj++;
            table.AddCell(new PdfPCell(new Phrase(broj.ToString()))
            {
                VerticalAlignment = Element.ALIGN_MIDDLE,
                HorizontalAlignment = Element.ALIGN_CENTER

            });
            table.AddCell(new PdfPCell(new Phrase(p.Naziv))
            {
                VerticalAlignment = Element.ALIGN_MIDDLE,
                HorizontalAlignment = Element.ALIGN_CENTER
            });
            table.AddCell(new PdfPCell(new Phrase(p.Cijena))
            {
                VerticalAlignment = Element.ALIGN_MIDDLE,
                HorizontalAlignment = Element.ALIGN_CENTER
            });
            table.AddCell(new PdfPCell(new Phrase(p.Sifra))
            {
                VerticalAlignment = Element.ALIGN_MIDDLE,
                HorizontalAlignment = Element.ALIGN_CENTER
            });
            table.AddCell(new PdfPCell(new Phrase(p.Kolicina.ToString()))
            {
                VerticalAlignment = Element.ALIGN_MIDDLE,
                HorizontalAlignment = Element.ALIGN_CENTER
            });
        }



        return table;
    }

Can someone help me figure out how to set the width of each row to a custom size?

Thanks !

perkes456
  • 1,163
  • 4
  • 25
  • 49