多个页面上的 ITextSharp table

ITextSharp table over several Pages

我用 table 创建了一个 pdf 文件。此 table 跨越数页。我想在每个页面上插入一次 HeaderRow。在论坛中我已经找到命令 pdfPTable。设置标题行(1)。但这会导致错误。 PdfPTable 不包含 setHeaderRows 的定义。

private void Cmd_Protocoll_Click(object sender, EventArgs e)
    {
        Document pProtocoll = new Document(iTextSharp.text.PageSize.A4.Rotate(), 10, 10, 10, 10);// A4 Querformat
        PdfWriter.GetInstance(pProtocoll, new FileStream("TestPDF.pdf", FileMode.Create));
        pProtocoll.Open();
        pProtocoll.SetPageSize(PageSize.A4.Rotate());
        pProtocoll.AddTitle("PDF-Erstellung");
        string author = Txt_PreName.Text + Txt_LastName.Text;
        pProtocoll.AddAuthor(author);
        pProtocoll.AddSubject("Was ist das Subject");

        /*BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);

        iTextSharp.text.Font font = FontFactory.GetFont(iTextSharp.text.Font.FontFamily.TIMES_ROMAN.ToString(), 6,
            iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK);*/


        PdfPTable pdfPTable = new PdfPTable(7);
        pdfPTable.setHeaderRow(1);

        pdfPTable.TotalWidth = 750f;
        pdfPTable.LockedWidth = true;

        float[] widths = new float[] { 3f, 1f, 1f, 5f, 1f, 1f, 0.75f };
        pdfPTable.SetWidths(widths);

        PdfPCell cell = new PdfPCell(new Phrase("Prüfprotokol zum Hardwaredatenpunkttest"));

        cell.Colspan = 3;
        cell.HorizontalAlignment = 1;

        BaseFont btnColumnHeader = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        iTextSharp.text.Font fntColumnHeader = FontFactory.GetFont(iTextSharp.text.Font.FontFamily.TIMES_ROMAN.ToString(), 10,
            iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK);

        for(int i =0; i < Dgv_Data_List.Columns.Count; i++)// Einlesen der Kopfzeile
        {
            PdfPCell headerCell = new PdfPCell();
            headerCell.BackgroundColor = iTextSharp.text.BaseColor.YELLOW;
            headerCell.AddElement(new Chunk(Dgv_Data_List.Columns[i].HeaderText, fntColumnHeader));

            pdfPTable.AddCell(headerCell);
        }

            BaseFont btnColumnData = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            iTextSharp.text.Font fntColumnData = FontFactory.GetFont(iTextSharp.text.Font.FontFamily.TIMES_ROMAN.ToString(), 8,
                iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK);

            for (int i = 0; i < Dgv_Data_List.Rows.Count; i++) // einfügen der Tabelle
            {
                for(int j = 0; j < Dgv_Data_List.Columns.Count; j++)
                {
                    PdfPCell dataCell = new PdfPCell();
                    dataCell.BackgroundColor = iTextSharp.text.BaseColor.WHITE;
                    dataCell.AddElement(new Chunk(Dgv_Data_List.Rows[i].Cells[j].Value.ToString(), fntColumnData));
                    pdfPTable.AddCell(dataCell); 
                }
            }

            pdfPTable.setHeaderRows(1);


        pProtocoll.Add(pdfPTable);

        pProtocoll.Close();


    }

I have already found the command pdfPTable.setHeaderRows(1). But this leads to an error. PdfPTable contains no definition for setHeaderRows.

pdfPTable.setHeaderRows(1) 看起来像 iText/Java。另一方面,您的代码看起来像 c#。因此,您需要 iText/.Net 方式来设置 header 行:

pdfPTable.HeaderRows = 1