簡體   English   中英

在創建pdf新頁面時使用itextsharp c#獲取第一頁數據

[英]while creating pdf new page get first page data using itextsharp c#

第二頁包含第一頁名稱 我正在嘗試通過使用 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;
}

所有新頁面連續包含第一頁的第一行。 以上是我試過的代碼。 有什么幫助嗎? 謝謝!

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

刪除第一行

table.DeleteRow(0);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM