0

I am creating dynamically a PDF file. After creating I want to open the pdf file. For that I am using this code:

    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;
    }

When executing this code, nothing happens and I don;t get any errors. What am I doing wrong?

Martijn
  • 24,441
  • 60
  • 174
  • 261

3 Answers3

2

Asp.net? What I would do is to take the memory stream and write it to the response stream like follows:

Response.ContentType = "application/pdf"; 
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", file.FileName)); 
Response.BinaryWrite(file.FileBytes); 
Response.Flush(); 
Response.End();

For windows forms I'd take a look at using Foxit Reader instead. I have a blog post about printing directly from foxit. You can open similarly.

EDIT: To create an attachment you add a reference to System.Net.Mail and do something like:

var stream = GetTheFileAsStream();
var attachment = new Attachment(stream);
mhenrixon
  • 6,179
  • 4
  • 40
  • 64
2

UPDATE:

Since this is an ASP.NET app, this code will not work. It cannot interact with the desktop of the server hosting ASP.NET.

If the intention is to display the PDF for the users accessing from a browser, then the code for that is completely different.

Jose Basilio
  • 50,714
  • 13
  • 121
  • 117
  • CreatePDF() returns the full path to my project including the filename: C:\\path\to\\file.pdf. It is a ASP.NET app. – Martijn Apr 21 '09 at 09:43
  • This would not work in an ASP.NET app because it needs to interact with the desktop of the server in order to display the PDF. – Jose Basilio Apr 21 '09 at 09:46
  • So, what do you suggest? When can I use Process in order to open my PDF file? – Martijn Apr 21 '09 at 09:48
  • To send the pdf in an email, there's no need to display it. – Jose Basilio Apr 21 '09 at 09:50
  • That's right, but there is also the posibility to view the pdf file. So I want to show the PDF and to send the PDF as an attchent – Martijn Apr 21 '09 at 09:52
  • The the code provided by @Catz would work for displaying it. For emailing check out this post: http://stackoverflow.com/questions/279953/how-to-send-an-email-with-attachments-using-smtpclient-sendasync – Jose Basilio Apr 21 '09 at 09:54
  • Marked as answer because of the link. (it shows how to create an attachment from bytes) – Martijn Apr 21 '09 at 09:59
0

It's not clear to me whether this is an ASP.NET app or Winforms. If Winforms then...

using (Process p = new Process())
{
    p.StartInfo.RedirectStandardOutput = false;
    p.StartInfo.FileName = @"C:\foo.pdf";
    p.StartInfo.UseShellExecute = true;
    p.Start();
    p.WaitForExit();             
}

... will work fine.

If this is ASP.NET MVC then you should look at the FileResult type and the File method of Controller...

public ActionResult GetFile()
{
    return File("foo.pdf", "application/pdf");
}

... as this is exactly what this is for.

Martin Peck
  • 11,440
  • 2
  • 42
  • 69
  • ok. cool. so it might be worth updating the question to state the stack you are using. i'm not sure the answer you chose is the best either. I'd have thought Catz's one better answers you as it shows you how to do this in ASP.NET. – Martin Peck Apr 21 '09 at 11:36