合并所有已生成的PDF

时间:2018-08-07 07:59:28

标签: c# asp.net web-applications itext pdf-generation

我正在开发Web应用程序,其功能之一是在PDF文件中生成多个证书。生成pdf没问题,但是生成多个证书时我想发生的事情仅是放在一个pdf文件中。我只是一个初学者,所以我没有太多想法。以下是我用于生成证书的代码。预先感谢!

    protected void btnPdf_Click(object sender, EventArgs e)
{
    string now = DateTime.Now.ToString("MM-dd-yyyy");
    foreach (GridViewRow row in TraineeGrid.Rows)
    {
        CheckBox chk = (CheckBox)row.FindControl("chkSelect");
        if (chk.Checked)
        {
              Label varName = (Label)row.FindControl("lbName");
              string name = varName.Text;
              Label varCourse = (Label)row.FindControl("lbCourse");
              string course = varCourse.Text;
              Label varCertNum = (Label)row.FindControl("lbCertNum");
              string certNumber = varCertNum.Text;

              Label varEndDate = (Label)row.FindControl("lbEndDate");
              string endDate = varEndDate.Text;
              DateTime date = DateTime.Parse(endDate);
              string finalEDate = date.ToString("MMMM dd, yyyy");

              Label varStartDate = (Label)row.FindControl("lbStartDate");
              string startDate = varStartDate.Text;
              DateTime date1 = DateTime.Parse(startDate);
              string dateFileName = date1.ToString("MM-dd-yyyy"); 
              string finalSDate = date1.ToString("MMMM dd, yyyy");
              var checkedBoxes = TraineeGrid.Controls.OfType<CheckBox>().Count(c => chk.Checked);

    try
    {

        Directory.CreateDirectory("D:\\Intern\\BASSWeb\\Certificates\\"+now+"\\");
        string inputCertificate = "D:\\Intern\\BASSWeb\\Certificates\\CertificateLayout.pdf";
        string outputCertificate = "D:\\Intern\\BASSWeb\\Certificates\\"+now+"\\"+name+"_" + course + "_"+dateFileName+".pdf";

        // open the reader
        PdfReader reader = new PdfReader(inputCertificate);
        Rectangle size = reader.GetPageSizeWithRotation(1);
        Document document = new Document();

        // open the writer
        FileStream fs = new FileStream(outputCertificate, FileMode.Create, FileAccess.Write);
        PdfStamper stamp = new PdfStamper(reader, fs);
        PdfContentByte cb = stamp.GetOverContent(1);
        var pageSize = reader.GetPageSize(1);
        ColumnText ct = new ColumnText(cb);
        PdfCopy copy = new PdfCopy(document, fs);
        document.Open();



        Font font = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 45, iTextSharp.text.Color.BLACK);
        ct.SetSimpleColumn(10f, 100f, 600f, 325f);
        Paragraph certText = new Paragraph(new Phrase(20, name, font));
        ct.AddElement(certText);
        ct.Go();

        Font font2 = FontFactory.GetFont(FontFactory.HELVETICA_OBLIQUE, 19, iTextSharp.text.Color.GRAY);
        ct.SetSimpleColumn(10f, 100f, 600f, 390f);
        certText = new Paragraph(new Phrase("This is to certify that ", font2));
        ct.AddElement(certText);
        ct.Go();
        ct.SetSimpleColumn(10f, 100f, 500f, 297f);
        certText = new Paragraph(new Phrase("has successfully completed the " + course + " at BASS Philippines RHQ", font2));
        ct.AddElement(certText);
        ct.Go();
        ct.SetSimpleColumn(10f, 100f, 500f, 240f);
        certText = new Paragraph(new Phrase("From: " + finalSDate + "", font2));
        ct.AddElement(certText);
        ct.Go();
        ct.SetSimpleColumn(10f, 100f, 500f, 216f);
        certText = new Paragraph(new Phrase("To: " + finalEDate + "", font2));
        ct.AddElement(certText);
        ct.Go();

        Font font3 = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10, iTextSharp.text.Color.BLACK);
        ct.SetSimpleColumn(10f, 100f, 500f, 192f);
        certText = new Paragraph(new Phrase("Certificate Number: " + certNumber + "", font2));
        ct.AddElement(certText);
        ct.Go();

        stamp.Close();
        reader.Close();
        fs.Close();


        Process.Start(outputCertificate);
        chk.Checked = false;
    }
    catch (Exception ex)
    {
        string errorMessage = "alert(\"ERROR: "+ ex.Message.ToString() +" \");";
        ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", errorMessage, true);
    }
}
    }
}

0 个答案:

没有答案