Robert Smith
Robert Smith

Reputation: 779

Remove white space between table cells

I am using ITextSharp 5.5.7. I have the following code and everything is working. However, I need to remove the white space between each cell. I have tried all the suggestions found and still the white spaces are not removed. Here is the code:

private PdfPTable createTable()
        {

            // Table (No border, just to hold all objects)
            PdfPTable sheetTable = new PdfPTable(1);
            sheetTable.WidthPercentage = 100;
            sheetTable.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
            sheetTable.SpacingBefore = 0f;
            sheetTable.SpacingAfter = 0f;

            // Add Heading One
            sheetTable.AddCell(createHeadingOne());

            // Add Heading two
            sheetTable.AddCell(createHeadingTwo());

            return sheetTable;

        }


        private PdfPTable createHeadingOne()
    {

        PdfPCell cell = new PdfPCell();

        PdfPTable headingTable = new PdfPTable(2);

        // Set widths of columns
        int[] headingTablewidths = { 50, 50 }; // percentage
        headingTable.SetWidths(headingTablewidths);

        cell = new PdfPCell(new Phrase("col1row1", FontFactory.GetFont(FontFactory.HELVETICA, 8, BaseColor.BLACK)));
        headingTable.AddCell(cell);

        cell = new PdfPCell(new Phrase("col2row1", FontFactory.GetFont(FontFactory.HELVETICA, 8, BaseColor.BLACK)));
        headingTable.AddCell(cell);

        return headingTable;

    }

    private PdfPTable createHeadingTwo()
    {

        PdfPCell cell = new PdfPCell();

        PdfPTable headingTable = new PdfPTable(2);

        // Set widths of columns
        int[] headingTablewidths = { 50, 50 }; // percentage
        headingTable.SetWidths(headingTablewidths);

        cell = new PdfPCell(new Phrase("col1row2", FontFactory.GetFont(FontFactory.HELVETICA, 8, BaseColor.BLACK)));
        headingTable.AddCell(cell);

        cell = new PdfPCell(new Phrase("col2row2", FontFactory.GetFont(FontFactory.HELVETICA, 8, BaseColor.BLACK)));
        headingTable.AddCell(cell);

        return headingTable;

    }

Update #1 The blue lines is the area were I need to remove the spacing:

enter image description here

Update #2, Fixed. Here is the code Bruno provided:

PdfPCell cell;
cell = new PdfPCell(createHeadingOne());
cell.Padding = 0;
sheetTable.AddCell(cell);
cell = new PdfPCell(createHeadingTwo());
cell.Padding = 0;
sheetTable.AddCell(cell);

Upvotes: 0

Views: 5746

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

I would solve this like this:

PdfPCell cell;
cell = new PdfPCell(createHeadingOne());
cell.Padding = 0;
sheetTable.AddCell(cell);
cell = new PdfPCell(createHeadingTwo());
cell.Padding = 0;
sheetTable.AddCell(cell);

Upvotes: 1

Related Questions