Click here to Skip to main content
15,063,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Im using itextsharp to export to PDF

when im run the application in local machine it is correclty open the pdf file.
but when im trying to run the application from local machine and do the export to pdf functionality file get generated but whle opening that file its is saying an error:-

"There was an error opening this document.This file is damaged and colud not be repaire"

Below is my code which i am using to export to pdf :-

C#
 //set the cotent type to PDF  
Response.ContentType = "application/pdf";

Response.AddHeader("content-disposition", "attachment;filename=Fullreport.pdf");

Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);

pnlPerson.RenderControl(hw);

//load the html content to the string reader  
StringReader sr = new StringReader(sw.ToString());

//HTMLDocument  Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom)  

Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 5f, 0f);

//iText class that allows you to convert HTML to PDF  
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

//When this PdfWriter is added to a certain PdfDocument,   
//the PDF representation of every Element added to this Document will be written to the outputstream.  
            
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

            
//open the document 
pdfDoc.Open();
  
htmlparser.Parse(sr);

//close the document stream  
pdfDoc.Close();

//write the content to the response stream  
Response.Write(pdfDoc);

//Response.End();

return true;


Above code is working properly when im running my application in LOCAL but when i diploy same code on server and run the application url from local and do the Export To PDF operation im geeting an error,PLease suggest what needs to do

What need to be done for this to open that exported PDF file.

PLease help
Posted
Updated 16-Jan-14 1:17am
v5

1 solution

The following code working fine for me.

iTextSharp.text.Document document = new iTextSharp.text.Document(new Rectangle(288f, 144f), 10, 10, 10, 10);
                document.SetPageSize(iTextSharp.text.PageSize.A4);

                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create));
                iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 10);
                document.Open();
                PdfPTable table = new PdfPTable(MainTable.Columns.Count);

                PdfPRow row = null;
                float[] widths = new float[] { 5f, 4f };
                table.SetWidths(widths);

                table.WidthPercentage = 100;

                // int iCol = 0;
                // string colname = "";
                iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(Server.MapPath("IMAGE URL"));
                PdfPCell cell1 = new PdfPCell(logo);
                cell1.Colspan = MainTable.Columns.Count;
                cell1.Padding = 5;
                table.AddCell(cell1);
                document.Add(table);
 Paragraph footer = new Paragraph("Your Company Name", FontFactory.GetFont(FontFactory.TIMES, 10, iTextSharp.text.Font.NORMAL));
                footer.Alignment = Element.ALIGN_RIGHT;
                PdfPTable footerTbl = new PdfPTable(1);
                footerTbl.TotalWidth = 500;
                footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;
                PdfPCell cell2 = new PdfPCell(footer);
                cell2.Border = 0;
                cell2.PaddingLeft = 0;
                footerTbl.AddCell(cell2);
                footerTbl.WriteSelectedRows(0, -1, 550, 30, writer.DirectContent);
                document.Close();
   

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