ASP.NET,c#,使用itextsharp导出为pdf [英] ASP.NET, c# , export to pdf using itextsharp
问题描述
嗨
我使用itextsharp导出为PDF
当我在本地机器上运行应用程序时,它是相关的打开pdf文件。
但是当我试图从本地机器运行应用程序并导出到pdf功能文件时会生成但是打开该文件它会说错误: -
打开此文件时出错。此文件已损坏且无法修复
以下是我的代码我是用于导出为pdf: -
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 :-
//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;
当我在LOCAL中运行我的应用程序时,上面的代码工作正常,但是当我在服务器上使用相同的代码并从本地运行应用程序URL并执行导出到PDF操作时出现错误,PLease建议需要什么执行
需要做些什么来打开导出的PDF文件。
PLease help
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
推荐答案
下面的代码对我来说很好。
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();
这篇关于ASP.NET,c#,使用itextsharp导出为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!