单击时有一个按钮,将文本插入到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
}
答案 0 :(得分: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();
}
这将使浏览器提供保存/打开对话框。