itextsharp PDF表格标题重复,并带有额外的第一行

时间:2013-07-24 06:32:41

标签: c# itextsharp

我正在使用iTextSharp库来生成PDF表格。

要在每个页面上重复标题(2行标题),我使用的是PDF表格的HeaderRows属性。它在每个页面上重复标题,但在标题下方,它会插入一个带有默认格式的新空白行。此额外行不会影响列和行的Rowspan值。

如何从表格中删除这一额外的行?

IList<Common.rowspan> Row = new List<Common.rowspan>();

for (int i = 0; i < RowsCount; i = i + 5)
{
    Row.Add(new Common.rowspan()
    {
        row = i,
        col = 0,
        span = 5
    });
}

float[] Width = new float[] { 250, 450, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200 };

public PdfPTable GetPdfTable(string Module, DataTable dt, IList<Common.rowspan> row, float[] width, int HeaderRow = 1)
{
    PdfPTable table = new PdfPTable(width.Length);
    PdfPCell cell;      
    foreach (DataColumn dc in dt.Columns)
    {
        cell = new PdfPCell(new Paragraph(dc.Caption));              
        table.AddCell(cell);
    }   
    int rowindex = 0; int colindex = 0; bool rowskip = false;
    if (dt.Rows.Count > 0)
    {
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (row[rowindex].row == i)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    cell = new PdfPCell(new Phrase(PageController.DecodeText(dt.Rows[i][j].ToString())));
                    if (row[rowindex].col == j && !rowskip)
                    {
                        cell.Rowspan = row[rowindex].span;
                        rowskip = true;
                        colindex = row[rowindex].col;
                    }                        
                    table.AddCell(cell);
                }
                if (rowindex < row.Count - 1)
                    rowindex = rowindex + 1;
                rowskip = false;
            }
            else
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (colindex != j)
                    {
                        cell = new PdfPCell(new Phrase(PageController.DecodeText(dt.Rows[i][j].ToString())));                            
                        table.AddCell(cell);
                    }
                }
            }
        }
    }
    else
    {
        cell = new PdfPCell(new Phrase("No Record Found.", NoRecordFoundBlackBold_Calibri_11));
        cell.Colspan = dt.Columns.Count;
        table.AddCell(cell);
    }
    table.HeaderRows = HeaderRow;
    return table;
}

1 个答案:

答案 0 :(得分:1)

刚遇到同样的问题。在我的情况下,它发生在最后一行不适合页面时,Itextsharp将其拆分。

页面开头的额外行只是上一页分割的第二部分。

通过设置表属性SplitLate = true和SplitRows = false

来解决
    private static PdfPTable CreateTable(PdfTableArgument tableArg)
    {

        PdfPTable result = CreateTableWithColumns(tableArg);
        // skip initialization code

        result.SplitLate = true;
        result.SplitRows = false;

        return result;
    }
相关问题