ItextSharp错误:文档在asp.net中没有页面 [英] ItextSharp Error : Document has no pages, in asp.net
本文介绍了ItextSharp错误:文档在asp.net中没有页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
i尝试使用c#将转发器数据导出到asp.net中的pdf,
在转发器中我也有网络用户控件。
导出这个,我使用的是itextshare.dll
Hi guys,
i was trying to export repeater data to pdf in asp.net using c#,
where in a repeater i have web user controls also.
to export this, i'm using itextshare.dll
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Text;
protected void btnExport_Click(object sender, ImageClickEventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
DailyReportRepeater.DataBind();
DailyReportRepeater.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();//the doc has no pages... Error
Response.Write(pdfDoc);
Response.End();
}
尝试:
trying:
i have already bind the repeater in the pageload, if(!ispostback).
....
in btnexport code:
i have commented // DailyReportRepeater.DataBind();
n then checked the repeater count n it was greater than zero
int c = DailyReportRepeater.Items.Count;
,
but if i uncommented this line ' DailyReportRepeater.DataBind();'
then the count is getting zero.
the problem is in binding of a repeater.
任何人都可以帮我解决这个问题。
谢谢
can anyone please help me to solve this issue.
Thanks
推荐答案
Hi
试试这个,
Response.ContentType =application / pdf;
Response.AddHeader(content-disposition,attachment; filename = FileName.pdf);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
DailyReportRepeater.DataBind();
DailyReportRepeater.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
文件pdfDoc =新文件(PageSize.A4,10f,10f ,10f,0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc,Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close(); //文档没有页面...错误< br $>
Response.Write(pdfDoc);
这是我用来写出表格的代码它有帮助
iTextSharp.text.Document doc = new iTextSharp.text.Document();
iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(doc,new FileStream(System.Web.HttpContext.Current.Server.MapPath(\\)+file.pdf, FileMode.Create));
doc.Open();
iTextSharp.text.pdf。 PdfPTable Table = new iTextSharp.text.pdf.PdfPTable(2);
Table.DefaultCell.Border = 0;
doc.Add (表);
doc.Close();
Hi
Try this,
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
DailyReportRepeater.DataBind();
DailyReportRepeater.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();//the doc has no pages... Error
Response.Write(pdfDoc);
Here is code I have used to write out a table if it helps
iTextSharp.text.Document doc = new iTextSharp.text.Document();
iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, new FileStream(System.Web.HttpContext.Current.Server.MapPath("\\") + "file.pdf", FileMode.Create));
doc.Open();
iTextSharp.text.pdf.PdfPTable Table = new iTextSharp.text.pdf.PdfPTable(2);
Table.DefaultCell.Border = 0;
doc.Add(Table);
doc.Close();
这篇关于ItextSharp错误:文档在asp.net中没有页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文