循环itextsharp内容以基于sql项目在相同的pdf中创建新页面

时间:2014-06-05 07:12:33

标签: 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 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);

0 个答案:

没有答案