el_Sharpo
el_Sharpo

Reputation: 67

ITextSharp table over several Pages

I create a pdf file with a table. This table runs over several pages. I want to insert the HeaderRow once on each page. In the forum I have already found the command pdfPTable. setHeaderRows(1). But this leads to an error. PdfPTable contains no definition for 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();


    }

Upvotes: 1

Views: 616

Answers (1)

mkl
mkl

Reputation: 95908

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

pdfPTable.setHeaderRows(1) looks like iText/Java. Your code on the other hand looks like c#. Thus, you need the iText/.Net way to set the header rows:

pdfPTable.HeaderRows = 1

Upvotes: 2

Related Questions