0

我正在使用 iTextSharp 创建 PDF,并且正在创建多个在我的代码中内联运行的表。当我用我收藏的值填充表格时,我不知道表格会有多长时间。而且我不希望一张桌子碰到或穿过下一张。这是我到目前为止所拥有的,但是如果表格进入下一页,它会与我放置在下一行代码中的表格重叠,并带有分页符 -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
4

1 回答 1

1

刚刚删除 contentbyte 并在文档中添加了一个新段落。很简单,这解决了我需要实现的目标:带有标题标题的表格,从新页面开始的新表格。

        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();

在此处输入图像描述

于 2014-02-13T20:16:28.567 回答