【问题标题】:while creating pdf new page get first page data using itextsharp c#在创建pdf新页面时使用itextsharp c#获取第一页数据
【发布时间】:2019-08-27 11:38:13
【问题描述】:

我正在尝试通过使用 c# 进行分组来创建附加格式的 pdf。第一页很好,但下一页仍然存在第一页第一列名称。在创建 pdf 新页面时获取第一页数据 我用过

DeleteBodyRows()

用于删除表格行但仍然相同。

foreach (DataRow row in dataTable.Rows)
{//write in new row
    var datatable = dataTable;

    var departments = from r in datatable.Rows.OfType<DataRow>()
                      group r by r["emp_reader_id"] into g
                      select new { emp_reader_id = g.Key, Data = g };
    foreach (var department in departments)
    {
        foreach (var roww in department.Data)
        {
            iii++;

            if (ii == 0)
            {


                PdfPCell cell1333 = new PdfPCell(new iTextSharp.text.Phrase("Name " + ":" + roww.ItemArray[1].ToString(), new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 19, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));

                cell1333.Colspan = 8;
                cell1333.Border = 0;
                cell1333.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
                table.AddCell(cell1333);


                PdfPCell cell1 = new PdfPCell(new iTextSharp.text.Phrase("E.Code " + ":" + roww.ItemArray[2].ToString(), new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 19, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
                cell1.Colspan = 8;
                cell1.Border = 0;
                cell1.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
                table.AddCell(cell1);

                PdfPCell cell144 = new PdfPCell(new iTextSharp.text.Phrase("Department " + ":" + roww.ItemArray[4].ToString(), new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 19, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
                cell144.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
                cell144.Colspan = 8;
                cell144.Border = 0;
                cell144.Border = iTextSharp.text.Rectangle.NO_BORDER;
                table.AddCell(cell144);


                PdfPCell cell155 = new PdfPCell(new iTextSharp.text.Phrase("Designation " + ":" + roww.ItemArray[5].ToString(), new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 19, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
                cell155.Colspan = 8;
                cell155.Border = 0;
                cell155.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
                cell155.Border = iTextSharp.text.Rectangle.NO_BORDER;
                table.AddCell(cell155);

                PdfPCell cell166 = new PdfPCell(new iTextSharp.text.Phrase("Name Of Facility  " + ":" + roww.ItemArray[6].ToString(), new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 19, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
                cell166.Colspan = 8;
                cell166.Border = 0;
                cell166.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
                cell166.Border = iTextSharp.text.Rectangle.NO_BORDER;
                table.AddCell(cell166);

                DataTable dtp1 = new DataTable();
                dtp1.Columns.Add("Date", typeof(string));

                dtp1.Columns.Add("F1", typeof(string));
                dtp1.Columns.Add("F2", typeof(string));
                dtp1.Columns.Add("F3", typeof(string));

                dtp1.Columns.Add("F4", typeof(string));
                dtp1.Columns.Add("Hours", typeof(string));

                dtp1.Columns.Add("Remarks", typeof(string));



                for (int i = 0; i < 7; i++)
                {
                    PdfPCell cell = new PdfPCell(new iTextSharp.text.Phrase(dtp.Columns[i].ColumnName, new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 19, iTextSharp.text.Font.BOLD | iTextSharp.text.Font.ITALIC, iTextSharp.text.Color.WHITE)));
                    cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#2980b9"));
                    cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
                    table.AddCell(cell);
                }
            }



            ii++;
            DateTime date = DateTime.Parse(roww.ItemArray[3].ToString(), System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat);
            string strDate = String.Format("{0:dd/MM/yyyy}", date);

            PdfPCell cell19 = new PdfPCell(new iTextSharp.text.Phrase(strDate, new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 12, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK)));
            cell19.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
            table.AddCell(cell19);

            table.AddCell(string.Empty);
            table.AddCell(string.Empty);
            table.AddCell(string.Empty);
            table.AddCell(string.Empty);
            table.AddCell(string.Empty);
            table.AddCell(string.Empty);


        }


        ii = 0;
        pdfDoc.Add(table);
        table.DeleteBodyRows();
        pdfDoc.NewPage();

    }

    break;
}

所有新页面连续包含第一页第一行。以上是我尝试过的代码。 有什么帮助吗?

【问题讨论】:

  • 表的创建在哪里完成?看起来该表被设置了一次并被反复使用而不更新值。

标签: c# asp.net itext


【解决方案1】:

在 table.DeleteBodyRows() 旁边添加以下行

删除第一行

table.DeleteRow(0);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 2014-05-13
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多