删除表格单元格之间的空白区域

时间:2016-11-09 15:19:42

标签: itext

我正在使用ITextSharp 5.5.7。我有以下代码,一切正常。但是,我需要删除每个单元格之间的空白区域。我已经尝试了所有建议,但仍然没有删除空格。这是代码:

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;

    }

更新#1 蓝线是我需要删除间距的区域:

enter image description here

更新#2 ,已修复。以下是Bruno提供的代码:

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

1 个答案:

答案 0 :(得分:1)

我会这样解决这个问题:

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