我想在程序生成后打开PDF。我知道它已经在整个互联网上得到解答,但是我找不到能够解决我的问题的令人满意的答案。这是生成PDF时的代码。
protected void btnPdf_Click(object sender, EventArgs e)
{
GridViewRow rows = TraineeGrid.SelectedRow;
Label varName = (Label)rows.FindControl("lbName");
string name = varName.Text;
Label varCourse = (Label)rows.FindControl("lbCourse");
string course = varCourse.Text;
Label varCertNum = (Label)rows.FindControl("lbCertNum");
string certNumber = varCertNum.Text;
Label varEndDate = (Label)rows.FindControl("lbEndDate");
string endDate = varEndDate.Text;
DateTime date = DateTime.Parse(endDate);
string finalEDate = date.ToString("MMMM dd, yyyy");
Label varStartDate = (Label)rows.FindControl("lbStartDate");
string startDate = varStartDate.Text;
DateTime date1 = DateTime.Parse(startDate);
string finalSDate = date1.ToString("MMMM dd, yyyy");
try
{
string inputCertificate = "D:\\Intern\\BASSWeb\\Certificates\\InitialCertificate.pdf";
string outputCertificate = "D:\\Intern\\BASSWeb\\Certificates\\" + course + "_" + name + ".pdf";
string newFileName = getNextFileName(outputCertificate);
// 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);
Font font = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 45, iTextSharp.text.Color.BLACK);
ct.SetSimpleColumn(28f, 100f, 600f, 305f);
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(28f, 100f, 600f, 370f);
certText = new Paragraph(new Phrase("This is to certify that ", font2));
ct.AddElement(certText);
ct.Go();
ct.SetSimpleColumn(28f, 100f, 500f, 277f);
certText = new Paragraph(new Phrase(" has successfully completed the " + course + " at BASS Philippines RHQ", font2));
ct.AddElement(certText);
ct.Go();
ct.SetSimpleColumn(28f, 100f, 500f, 210f);
certText = new Paragraph(new Phrase("From: " + finalSDate + "", font2));
ct.AddElement(certText);
ct.Go();
ct.SetSimpleColumn(28f, 100f, 500f, 186f);
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(0f, 100f, 500f, 620f);
certText = new Paragraph(new Phrase("Certificate Number: " + certNumber + "", font3));
ct.AddElement(certText);
ct.Go();
stamp.Close();
reader.Close();
fs.Close();
document.Open();
string script = "alert(\" A Certificate has been generated \");";
ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true);
Response.Clear();
Response.ContentType = "application/pdf";
Response.WriteFile(outputCertificate);
Response.End(); }
我也尝试过Response.Redirect
,但仍然无济于事。
答案 0 :(得分:0)
确保关闭Pdf,然后执行Process.Start(outputCertificate)
。
为此,您需要using System.Diagnostics
此外,我看不到您执行document.Open();
的原因,因此必须关闭才能使用Process.Start();