Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
following code is not loading the pdf in browser when i click on the button why?. I created a folder.pdf by right clicking in the project.And gave this path to asp.et server as you can see in the code below. may be the path is wrong? or something else?

C#
public partial class _Default : System.Web.UI.Page
    {
        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 using different class object to our pdf doc
            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 using different class object to our pdf document  
         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 6-Sep-11 8:08am
v2

1 solution

Have a look please

http://geekswithblogs.net/AzamSharp/archive/2005/09/18/54294.aspx[^]

I have taken the code from above link and it is working for me.

C#
protected void Button1_Click(object sender, EventArgs e)
       {
           string path = @"C:\Users\RASHIM\Desktop\isorc2002.pdf";
           WebClient client = new WebClient();
           Byte[] buffer = client.DownloadData(path);

           if (buffer != null)
           {
               Response.ContentType = "application/pdf";
               Response.AddHeader("content-length", buffer.Length.ToString());
               Response.BinaryWrite(buffer);
           }
       }


And if you want to do it in chunk chunk way please follow this

C#
protected void Button1_Click(object sender, EventArgs e)
        {
            string path = @"C:\Users\RASHIM\Desktop\isorc2002.pdf";
            WebClient client = new WebClient();
            byte[] bytes = client.DownloadData(path);            

            Stream oStream = new MemoryStream(bytes);
            Response.ContentType = "application/pdf";

            byte[] buffer = new byte[1024];
            long i = oStream.Length;

            while (i >= 0)
            {
                int readCount = oStream.Read(buffer, 0, 1024);
                Response.OutputStream.Write(buffer, 0, readCount);
                if (i <= 0)
                    break;

                i = i - 1024;
            }          
        }
 
Share this answer
 
v3
Comments
rajh7 6-Sep-11 13:56pm    
Does not help me ...can have more details in c# if u ca
Md. Rashim Uddin 6-Sep-11 14:01pm    
It is working man..Please see the above code portion
Md. Rashim Uddin 8-Sep-11 2:11am    
Did u try with the above code???

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