Free Support Forum -

Create PDF from Response string

Using XP, VS.NET, C#, Aspose.Pdf demo


User clicks on a link to go to a page.
Page_Load gets data and loads appropriate labels.
Would then like the page to automatically display as PDF.

Is it possible to do this? If so, where does the code go to generate the PDF. Must I save the PDF to a file to display it in the browser?

Would also like to do the same thing with Aspose.Word and Aspose.Excel.

Dear scooper,

Thanks for your consideration.

It’s possible. You can add your code to the Page_Load method and save the resulting document to Response. Please refer to the features demo of Aspose.Pdf, it works just like what you want.

You can do the same thing with Aspose.Word and Aspose.Excel.

That’s good to hear but can you tell me which cs file to look in? As I look through the code examples, I’m not finding it.


I’m looking through the code files.

From the Northwind.aspx.cs page it appears all the links go through a switch statement. The switch statement then calls the appropriate .cs file. Each .cs file includes code similar to the following:

string xmlFile = path + “\Xml\EmployeeSales.xml”;
pdf.BindXML(xmlFile, null);

Must I have an existing xml file? I thought the response string would be the xml file.

Hopefully your response will bring to light what I am missing. Thanks

Dear scooper,

Thanks for your consideration.

Please refer to Creating First Document in the programmer’s guide. You can use XML to define static content and format. You can use pure API to implement your application too.

Here is my code based on the programmer’s guide:

Pdf pdf1 = new Pdf();
Section sec1 = pdf1.Sections.Add();
sec1.Paragraphs.Add(new Text((Response.OutputStream.ToString())));
pdf1.Save("", SaveType.OpenInBrowser, Response);

(I can look at the Response.OutputStream through an XmlTextWriter and it is well-formatted xml.)

When I run this code, I get a “File Download” dialog box.
-If I chose “Open”, I get the following message: “The file referenced by the shortcut file ‘C:\Documents and Settings\scooper\Local Settings\Temporary Internat Files\Content.IE5\MH0J696H\WebForm1[2].aspx’ cannot be opened.”
-If I chose “Save”, I am prompted to save as type Adobe Acrobat Document. I do. A Download Complete dialog box appears. I try to open the saved file and get the following message: "There was an error opening this document. The file is damaged and could not be repaired."

What am I doing wrong?

Post Script

I added Response.End(); after the save. Now I get the following message: "Adobe Reader could not open ‘pa039721.fdf’ because it is either not a supported file type or because the file has been corrupted (for example, it was sent as an email attachment and wasn’t correctly decoded)."

Thanks for your help.

Dear scooper14,

Thanks for your consideration.

You should add Response.End(). Please do a test to find if the generated PDF is valid. You can use a simple string instead of Response.OutputStream.ToString() to see if it can display in browser. And you can save the file to disk to see if it is valid. If the file is not valid, please send your code which can reproduce the error to me.

Dear Laurie,

Thanks for your consideration.

I have to say that changing the HTML response to Excel,Word, or PDF format output with just a few lines of code is not possible. You have to write applications to generate Excel,Word or Pdf format output just like you generate the HTML response.