ITextSharp 4.1.6. PDF Table - how to remove whitespace on top of each cell? [padding and leading already set to 0]
Asked Answered
M

1

10

I'm having a problem with ITextSharp's tables. I'd like to have cells without top & bottom padding, so that they are placed closer to each other.

Although I have set the padding and the leading of the cell to 0, the white-space still remains.

See the screen

Does anyone please know how to remove the whitespace ?

EDIT:

Thanx to prompt answer from Dylan, I've managed to resolve my issue. Here's the source snippet if someone gets across similar issue

        Document document = new Document(PageSize.A4, 5, 5, 10, 10);
        using (FileStream fs = new FileStream("C:\\Users\\brum\\Desktop\\untitled.pdf", FileMode.Create))
        {
            iTextSharp.text.pdf.PdfWriter.GetInstance(document, fs);
            document.Open();
            PdfPTable table = new PdfPTable(2);
            PdfPCell cell = new PdfPCell(new Phrase("Spanning 2 cols"));

            cell.Colspan = 2;
            cell.HorizontalAlignment = 1;
            cell.Padding = 0f;
            cell.UseAscender = true;
            table.AddCell(cell);

            table.AddCell("Next row 1");
            table.AddCell("Next row 2");

            document.Add(table);
            document.Close();
        }

cell.UseAscender = true; // This is the line that did the trick for me

Majorette answered 12/3, 2012 at 17:41 Comment(2)
Oh my God!... :D I was going crazy with this top white space in each cell. The vertical alignment was not working correctly due to this.Christoffer
For future reference, this is also stated in the iTextSharp docs, eg: afterlogic.com/mailbee-net/docs-itextsharp/html/…Refugia
C
22

Set the top padding to something small or even negative. Another option is PdfPCell.setUseAscender().

ex:

cell.setPaddingTop(0f);  // No padding on top cell

or

cell.UseAscender = true;

Please paste the code you have.

Caesium answered 12/3, 2012 at 17:54 Comment(3)
Worked for me too, been trying to work this out for hours - good question, great answer!Unmade
@Unmade - I was in the same boat... but as always our heavenly father brought me to the right spot. Great answer Dylan.Christoffer
UseAscender works. This library makes me want to jump into the sun.Segovia

© 2022 - 2024 — McMap. All rights reserved.