首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >生成PDF文件后立即打开该文件

生成PDF文件后立即打开该文件
EN

Stack Overflow用户
提问于 2018-07-31 17:41:13
回答 1查看 49关注 0票数 1

我想在程序生成PDF后打开它。我知道这个问题已经在网上得到了回答,但我找不到一个令人满意的答案来解决我的问题。以下是我在生成PDF时的代码。

代码语言:javascript
复制
 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,但仍然没有任何效果。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-31 17:44:18

确保关闭Pdf,然后执行Process.Start(outputCertificate)

为此,您需要using System.Diagnostics

此外,我也不明白您使用document.Open();的原因,因为使用Process.Start();肯定需要关闭它

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51610292

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档