C# 循环itextsharp内容以基于sql项在同一pdf中创建新页面

C# 循环itextsharp内容以基于sql项在同一pdf中创建新页面,c#,asp.net,sql,pdf,itextsharp,C#,Asp.net,Sql,Pdf,Itextsharp,我正在使用iTextsharp从sql数据库创建一个包含字段的pdf文档,当客户机输入信息时,每个页面都保存在新行中,其中page01、page02等作为该数据库中的一列。我希望iTextsharp循环并基于此页码创建一个新页面 protected void GenerateReport(object sender, EventArgs e) { DataRow dr = GetData("SELECT * FROM OnSiteWorkTx where DocI

我正在使用iTextsharp从sql数据库创建一个包含字段的pdf文档,当客户机输入信息时,每个页面都保存在新行中,其中page01、page02等作为该数据库中的一列。我希望iTextsharp循环并基于此页码创建一个新页面

    protected void GenerateReport(object sender, EventArgs e)
    {
        DataRow dr = GetData("SELECT * FROM OnSiteWorkTx where DocID = " + DropDownListPdf.SelectedItem.Value).Rows[0]; ;
        Document document = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
        Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK);

        using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
        {
            PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
            Phrase phrase = null;
            PdfPCell cell = null;
            PdfPTable table = null;
            BaseColor color = null;

            document.Open();

    table = new PdfPTable(2);
            table.SetWidths(new float[] { 2f, 10f });
            table.TotalWidth = 480f;
            table.LockedWidth = true;
            table.SpacingBefore = 15f;
            table.HorizontalAlignment = Element.ALIGN_RIGHT;


            phrase = new Phrase(new Chunk("doc 018/2\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
            phrase.Add(new Chunk("On Site Work                        " + "Visit " + dr["VisitNumber"] + "   " + dr["PageNumber"], FontFactory.GetFont("Arial", 15, Font.NORMAL, BaseColor.BLACK)));
            // table.AddCell(PhraseCell(new Phrase("On Site Work", FontFactory.GetFont("Arial", 20, Font.UNDERLINE, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
            table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
            cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_RIGHT);
            cell.Colspan = 2;
            cell.PaddingBottom = 13f;
            table.AddCell(cell);

            table.AddCell(PhraseCell(new Phrase("Company: ", FontFactory.GetFont("Arial", 10, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
            phrase = new Phrase(new Chunk(dr["Company"] + "          " + "           " + "          " + "           " + "           " + "           " + "           " + "Email: " + dr["Email"], FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
            table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
            cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
            cell.Colspan = 7;
            cell.PaddingBottom = 10f;
            table.AddCell(cell);

            table.AddCell(PhraseCell(new Phrase("Plant: ", FontFactory.GetFont("Arial", 10, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
            phrase = new Phrase(new Chunk(dr["Plant"] + "          " + "           " + "          " + "           " + "           " + "           " + "          " + "Contact Tel: " + dr["ContactTel"], FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
            table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
            cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
            cell.Colspan = 2;
            cell.PaddingBottom = 10f;
            table.AddCell(cell);

            table.AddCell(PhraseCell(new Phrase("Contact Person: ", FontFactory.GetFont("Arial", 10, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
            phrase = new Phrase(new Chunk(dr["ContactPerson"] + "          " + "           " + "          " + "           " + "           " + "           " + "Fax No: " + dr["FaxNo"], FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
            table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
            cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
            cell.Colspan = 2;
            cell.PaddingBottom = 10f;
            table.AddCell(cell);

            DrawLine(writer, 25f, document.Top - 160f, document.PageSize.Width - 30f, document.Top - 160f, BaseColor.BLACK);
            document.Add(table);

            table = new PdfPTable(2);
            table.SetWidths(new float[] { 2f, 10f });
            table.TotalWidth = 480f;
            table.LockedWidth = true;
            table.SpacingBefore = 15f;
            table.HorizontalAlignment = Element.ALIGN_RIGHT;


            table.AddCell(PhraseCell(new Phrase("Actuator Type: ", FontFactory.GetFont("Arial", 10, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
            phrase = new Phrase(new Chunk(dr["ActuatorType"] + "     " + "Drive: " + dr["Drive"] + "           " + "           " + "Matic Type: " + dr["MATICTYPE"] + "     " + "Control: " + dr["CONTROL"], FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
            table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
            cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_LEFT);
            cell.Colspan = 2;
            cell.PaddingBottom = 10f;
            table.AddCell(cell);

            table.AddCell(PhraseCell(new Phrase("Comm No: ", FontFactory.GetFont("Arial", 10, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
            phrase = new Phrase(new Chunk(dr["CommNo"] + "         " + "          " + "          " + "           " + "           " + "Matic Comm No: " + dr["MATICCOMMNO"], FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
            table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
            cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
            cell.Colspan = 2;
            cell.PaddingBottom = 10f;
            table.AddCell(cell);

    document.Add(table);

                document.Close();

                byte[] bytes = memoryStream.ToArray();
                memoryStream.Close();
                Response.Clear();

                MailMessage mm = new MailMessage("email", txt_To.Text);
                mm.CC.Add(txt_Cc.Text);
                mm.Subject = txt_Subject.Text;
                mm.Body = txt_Message.Text;
                mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "OnSiteWork.pdf"));
                mm.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
                NetworkCred.UserName = "email";
                NetworkCred.Password = "password";
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 587;
                smtp.Send(mm);

你必须重新措辞你的问题,因为现在它没有意义。创建一个表并将其添加到文档中。如果表格适合页面,则文档仅占用一页。如果该表不适合,则该表将被拆分并分布在多个页面上。如果希望在一个单独的页面上有两个表,可以在两个
doc.add(table)
语句之间添加
doc.newPage()
。您的要求“我希望iTextsharp根据此页码循环并创建一个新页面”假设您的页面上的人都知道您在说什么。那句话的页码是多少?每一页是什么?页码是一行中的一列,我添加了一列访问号,如果用户按下三次,该访问号将在三行中相同。我想我需要一个查询来逐行浏览数据库,如果访问号相同,它必须从下一行获取数据并插入到pdf so doc.NewPage();应该在查询之前,这样它会自动将信息放在新页面上,如果没有,则doc.close会打印pdf。我希望我能理解“页码是行中的一列”:哪一行?数据库记录中的字段?列中的单元格?“我添加了专栏访问编号”:什么是专栏访问编号?“如果用户按下下三次”:用户按下的是什么?我很抱歉,但你真的应该集中精力,用一些不了解你的项目的人容易理解的术语来解释你的需求:请重写你的问题,否则它将被关闭。