tags:

views:

1635

answers:

3

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?

+2  A: 

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
I also tried this and this works indeed. But I also want to send an e-mail with the pdf as an attachment
Martijn
Then you create an attachment from the file bytes.
mhenrixon
+2  A: 

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
CreatePDF() returns the full path to my project including the filename: C:\\path\to\\file.pdf. It is a ASP.NET app.
Martijn
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
So, what do you suggest? When can I use Process in order to open my PDF file?
Martijn
To send the pdf in an email, there's no need to display it.
Jose Basilio
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
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
Marked as answer because of the link. (it shows how to create an attachment from bytes)
Martijn
A: 

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
It's a asp.net webform app.
Martijn
edit: and i'm not using the mvc pattern
Martijn
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