如何在每页上重复PDF表的标题?

时间:2019-06-24 16:03:30

标签: c# asp.net pdf itext

我将PDF表中的数据表数据绑定在一起,并且有100多个行,因此数据由许多页面组成,所以想在每页上重复表头。

void ExportDataTableToPdf(DataTable dtblTable, String strPdfPath, string strHeader)
{
    System.IO.FileStream fs = new FileStream(strPdfPath, FileMode.Create, FileAccess.Write, FileShare.None);
    Document document = new Document();
    document.SetPageSize(iTextSharp.text.PageSize.A4);
    PdfWriter writer = PdfWriter.GetInstance(document, fs);
    document.Open();

    //Report Header
    BaseFont bfntHead = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    Font fntHead = new Font(bfntHead, 16, 1, Color.GRAY);
    Paragraph prgHeading = new Paragraph();
    prgHeading.Alignment = Element.ALIGN_CENTER;
    prgHeading.Add(new Chunk(strHeader.ToUpper(), fntHead));
    document.Add(prgHeading);

    //Author
    Paragraph prgAuthor = new Paragraph();
    BaseFont btnAuthor = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    Font fntAuthor = new Font(btnAuthor, 8, 2, Color.GRAY);
    prgAuthor.Alignment = Element.ALIGN_RIGHT;
    // prgAuthor.Add(new Chunk("Author : Dotnet Mob", fntAuthor));
    prgAuthor.Add(new Chunk("\nPrint Date : " + DateTime.Now.ToShortDateString(), fntAuthor));
    document.Add(prgAuthor);

    //Add a line seperation
    Paragraph p = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, Color.BLACK, Element.ALIGN_LEFT, 1)));
    document.Add(p);

    //Add line break
    document.Add(new Chunk("\n", fntHead));

    //Write the table
    PdfPTable table = new PdfPTable(dtblTable.Columns.Count);
    //Table header
    BaseFont btnColumnHeader = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    Font fntColumnHeader = new Font(btnColumnHeader, 10, 1, Color.WHITE);
    for (int i = 0; i < dtblTable.Columns.Count; i++)
    {
        PdfPCell cell = new PdfPCell();
        cell.BackgroundColor = Color.GRAY;
        cell.AddElement(new Chunk(dtblTable.Columns[i].ColumnName.ToUpper(), fntColumnHeader));
        table.AddCell(cell);
    }
    //table Data
    for (int i = 0; i < dtblTable.Rows.Count; i++)
    {
        for (int j = 0; j < dtblTable.Columns.Count; j++)
        {
            table.AddCell(dtblTable.Rows[i][j].ToString());
        }
    }

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

1 个答案:

答案 0 :(得分:0)

我找不到文档,但是我认为您应该添加一个OnEndPage事件,在这种情况下,您可以使用标题创建一个新表。

我认为,如果要在每个页面上重复标题,则需要为每个页面创建一个单独的表。