Search code examples
c#pdfvisual-studio-2012itextpdfptable

How to append a table in PDF using iTextsharp without knowing the exact number of pages the table will produce


I'm using iTextSharp to create a PDF and I'm creating multiple tables that run inline in my code. I won't know how long the table is going to be when I fill it with values from my collection. And I don't want one table to run into or through the next. This is what I have so far, but if the table runs into the next page, it overlaps the table that I placed in the next line of code with a page break -NewPage()-

// Page 1 Searches 
            doc.NewPage();

            cb.BeginText();
            centerText(cb, "HeaderText for Searches", 300, 760, _fontbold, 18);
            cb.EndText();

            PdfPTable tableSearches = new PdfPTable(4);
            PdfPCell cellSearches = new PdfPCell();

            cellSearches.BackgroundColor = BaseColor.WHITE;

            cellSearches.Phrase = new Phrase("Company");
            tableSearches.AddCell(cellLKQ);
            cellSearches.Phrase = new Phrase("Contact");
            tableSearches.AddCell(cellLKQ);
            cellSearches.Phrase = new Phrase("Phone Number");
            tableSearches.AddCell(cellLKQ);
            cellSearches.Phrase = new Phrase("Amount");
            tableSearches.AddCell(cellLKQ);

            //loop through the records in facilities collection and add row
            foreach (var m in facilities)
            {
                cellSearches.BackgroundColor = BaseColor.LIGHT_GRAY;

                cellSearches.Phrase = new Phrase(m.Facility);
                tableSearches.AddCell(cellSearches);

                cellSearches.Phrase = new Phrase(m.FacilityContact);
                tableSearches.AddCell(cellSearches);

                cellSearches.Phrase = new Phrase(m.Phone);
                tableSearches.AddCell(cellSearches);

                cellSearches.Phrase = new Phrase(m.SalvageQuote.ToString());
                tableSearches.AddCell(cellSearches);
            }

            doc.Add(tableSearches);

            //Page 2? AM Searches

            doc.NewPage();

            cb.BeginText();
            centerText(cb, "HeaderText AM Searches", 300, 760, _fontbold, 18);
            cb.EndText();

            PdfPTable tableAM = new PdfPTable(4);
            PdfPCell cellAM = new PdfPCell();

            cellAM.BackgroundColor = BaseColor.WHITE;

            cellAM.Phrase = new Phrase("Company");
            tableAM.AddCell(cellAM);
            cellAM.Phrase = new Phrase("Contact");
            tableAM.AddCell(cellAM);
            cellAM.Phrase = new Phrase("Phone Number");
            tableAM.AddCell(cellAM);
            cellAM.Phrase = new Phrase("Amount");
            tableAM.AddCell(cellAM);

            //loop through the records and add row
            foreach (var m in amfacilities)
            {
                cellAM.BackgroundColor = BaseColor.CYAN;
                cellAM.Phrase = new Phrase(m.Facility);
                tableAM.AddCell(cellAM);

                cellAM.Phrase = new Phrase(m.FacilityContact);
                tableAM.AddCell(cellAM);

                cellAM.Phrase = new Phrase(m.Phone);
                tableAM.AddCell(cellAM);

                cellLKQ.Phrase = new Phrase(m.SalvageQuote.ToString());
                tableAM.AddCell(cellAM);
            }

            doc.Add(tableAM);

            //Page 3? Another Table
              doc.NewPage();
           // Code for next table

Solution

  • Just removed contentbyte and added a new paragraph to the document. Quite simply this solved what I needed to achive: A table with a Title header, new table starting on a new page.

            doc.Open();
    
            doc.Add(new Paragraph(new Chunk("Header for Searches" + Chunk.NEWLINE + Chunk.NEWLINE, fb)));
    
            PdfPTable tableSearches = new PdfPTable(3);
            PdfPCell cellSearches = new PdfPCell();
    
            cellSearches.BackgroundColor = BaseColor.WHITE;
    
            cellSearches.Phrase = new Phrase("Facility ID");
            tableSearches.AddCell(cellSearches);
            cellSearches.Phrase = new Phrase("Facility");
            tableSearches.AddCell(cellSearches);
            cellSearches.Phrase = new Phrase("Phone Number");
            tableSearches.AddCell(cellSearches);
    
            //loop through the records in facilities collection and add row
            foreach (var m in facilityList)
            {
                cellSearches.BackgroundColor = BaseColor.LIGHT_GRAY;
    
                cellSearches.Phrase = new Phrase(m.Id.ToString());
                tableSearches.AddCell(cellSearches);
    
                cellSearches.Phrase = new Phrase(m.Facility);
                tableSearches.AddCell(cellSearches);
    
                cellSearches.Phrase = new Phrase(m.Phone);
                tableSearches.AddCell(cellSearches);
            }
    
            doc.Add(tableSearches);
    
            //Page 2? AM Searches
    
            doc.NewPage();
    
            doc.Add(new Paragraph(new Chunk("Header for AM Searches" + Chunk.NEWLINE + Chunk.NEWLINE, fb)));
    
            PdfPTable tableAM = new PdfPTable(3);
            PdfPCell cellAM = new PdfPCell();
    
            cellAM.BackgroundColor = BaseColor.WHITE;
    
            cellAM.Phrase = new Phrase("Facility ID");
            tableAM.AddCell(cellAM);
            cellAM.Phrase = new Phrase("Facility");
            tableAM.AddCell(cellAM);
            cellAM.Phrase = new Phrase("Phone Number");
            tableAM.AddCell(cellAM);
    
            //loop through the records and add row
            foreach (var m in facilityList)
            {
                cellAM.BackgroundColor = BaseColor.CYAN;
                cellAM.Phrase = new Phrase(m.Id.ToString());
                tableAM.AddCell(cellAM);
    
                cellAM.Phrase = new Phrase(m.Facility);
                tableAM.AddCell(cellAM);
    
                cellAM.Phrase = new Phrase(m.Phone);
                tableAM.AddCell(cellAM);
            }
    
            doc.Add(tableAM);
    
            doc.Close();
    

    enter image description here