0

单击时我有一个按钮,将文本插入 pdf 的表单字段并将填充的 pdf 保存到目录中。当我编辑完 pdf 后,我想在浏览器中打开 pdf,但 Process.Start() 不起作用。有没有更好的方法在生成 pdf 后立即显示它?这是按钮的代码:

protected void btnGenerateQuote_Click(object sender, EventArgs e)
{
    string address = txtAddress.Text;
    string company = txtCompany.Text;
    string outputFilePath = @"C:\Quotes\Quote-" + company + "-.pdf";
    PdfReader reader = null;
    try
    {
        reader = new PdfReader(@"C:\Quotes\Quote_Template.pdf");
        using (FileStream pdfOutputFile = new FileStream
                                          (outputFilePath, FileMode.Create))
        {
            PdfStamper formFiller = null;
            try
            {
                formFiller = new PdfStamper(reader, pdfOutputFile);
                AcroFields quote_template = formFiller.AcroFields;
                //Fill the form
                quote_template.SetField("OldAddress", address);
                //Flatten - make the text go directly onto the pdf 
                //          and close the form.
                //formFiller.FormFlattening = true;
            }
            finally
            {
                if (formFiller != null)
                {
                     formFiller.Close();
                }
            }
        }
    }
    finally
    {
        reader.Close();
    }
    //Process.Start(outputFilePath); // does not work
}
4

1 回答 1

4

由于根据标签这是关于 ASP.NET 的,因此您不应该使用 Process.Start() 而是例如这样的代码:

private void respondWithFile(string filePath, string remoteFileName) 
{
    if (!File.Exists(filePath))
        throw new FileNotFoundException(
              string.Format("Final PDF file '{0}' was not found on disk.", 
                             filePath));
    var fi = new FileInfo(filePath);
    Response.Clear();
    Response.AddHeader("Content-Disposition", 
                  String.Format("attachment; filename=\"{0}\"", 
                                 remoteFileName));
    Response.AddHeader("Content-Length", fi.Length.ToString());
    Response.ContentType = "application/octet-stream";
    Response.WriteFile(fi.FullName);
    Response.End();
}

这将使浏览器给出保存/打开对话框。

于 2009-05-29T06:49:21.180 回答