C#过程对象无法打开PDF
问题描述:
我正在动态创建一个PDF文件。创建后我想打开PDF文件。对于我使用这个代码:C#过程对象无法打开PDF
System.Diagnostics.Process p = new System.Diagnostics.Process();
p = new System.Diagnostics.Process();
p.StartInfo.FileName = CreatePDF(); // method that creats my pdf and returns the full path
try
{
if (!p.Start())
Controller.Error = "Opening acrobat failed..";
}
catch(Exception ex)
{
Controller.Error = "Create PDF::" + ex.Message;
}
执行此代码时,什么也没有发生,我不;吨得到任何错误。我究竟做错了什么?
答
UPDATE:
由于这是一个ASP.NET应用程序,这段代码将不起作用。它无法与托管ASP.NET的服务器的桌面进行交互。
如果打算为从浏览器访问的用户显示PDF,那么代码完全不同。
答
Asp.net?我会做的是把内存流,它像写出如下响应流:
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", file.FileName));
Response.BinaryWrite(file.FileBytes);
Response.Flush();
Response.End();
Windows窗体我想看看使用福昕阅读器来代替。我有一个关于直接从福克斯打印的blog post。你可以打开类似的。
编辑:要创建一个引用到System.Net.Mail一个附件,这样做:
var stream = GetTheFileAsStream();
var attachment = new Attachment(stream);
答
我不清楚这是ASP.NET应用程序还是Winforms。如果Winforms然后...
using (Process p = new Process())
{
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.FileName = @"C:\foo.pdf";
p.StartInfo.UseShellExecute = true;
p.Start();
p.WaitForExit();
}
...将工作正常。
如果这是ASP.NET MVC,那么你应该看看FileResult类型和控制器的文件方法...
public ActionResult GetFile()
{
return File("foo.pdf", "application/pdf");
}
...因为这正是这是。
CreatePDF()返回我的项目的完整路径,包括文件名:C:\\ path \ to \\ file.pdf。这是一个ASP.NET应用程序。 – Martijn 2009-04-21 09:43:38