Click here to Skip to main content
15,902,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
All I did is wrote the following code and created a folder in the project and put a pdf document in to the folder named "test1.pdf". I dont know if my program hit the test.pdf or not... for some reason my program does not load a pdf in browser when I click the button? What's wrong in my code? Give me a god reason please.



C#
protected void Page_Load(object sender, EventArgs e)
        {
            //Create Document class object and set its size to letter and 
            //give space left, right, Top, Bottom Margin
             Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
            PdfWriter wri = PdfWriter.GetInstance(doc, new System.IO.FileStream("Test1.pdf", FileMode.Create)); doc.Open();//Open Document to write

            //Write some content
            Paragraph paragraph = new Paragraph("This is my first line using Paragraph.");
            Phrase pharse = new Phrase("This is my second line using Pharse.");
            Chunk chunk = new Chunk(" This is my third line using Chunk.");

            // Now add the above created text 
            doc.Add(paragraph); 
            doc.Add(pharse); 
            doc.Add(chunk);
            doc.Close(); //Close document

        }   

        protected void Button1_Click(object sender, EventArgs e)
        {
         //Create Document class obejct and set its size to letter and give space left, right, Top, Bottom Margin  

     Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);  

     try 

     {  

        PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("Test1.pdf", FileMode.Create));  
        //Open Document to write  
          doc.Open();  

        //Write some content  
   Paragraph paragraph = new Paragraph("This is my first line using Paragraph.");  
         Phrase pharse = new Phrase("This is my second line using Pharse.");  
         Chunk chunk = new Chunk(" This is my third line using Chunk."); 
 
         // Now add the above created text  
         doc.Add(paragraph);  
         doc.Add(pharse);  
         doc.Add(chunk);  

     }  

     catch (DocumentException dex)  

     {  

         //Handle document exception  

     }  

     catch (IOException ioex)  

     {  

         //Handle IO exception  

     }  

     catch (Exception ex)  

     {  

         //Handle Other Exception  

     }  

     finally 

     {  

         doc.Close(); //Close document  

     }  



           
        }
Posted
Updated 7-Sep-11 4:50am
v2

1 solution

There's nowhere in your code where you actually try to write the PDF output to the response stream. The code that you have here writes the file to the FileStream, which represents the file storage. You can write as many files as you like in there, but without actually picking one and streaming it to the response stream, you aren't going to see the file being downloaded.

When you write to the response stream, you should really remember to set the ContentType. Here's a sample of what you need to do:
C#
private void WriteToResponseStream(string fileName)
{
  Response.ContentType = "Application/pdf";
  Response.WriteFile(MapPath(fileName));
  Response.End();
}
 
Share this answer
 
Comments
rajh7 7-Sep-11 11:28am    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900