使用ITextsharp将带有图像的ASP.Net网页导出为PDF [英] Export ASP.Net Web Page with images to PDF using ITextsharp

查看:111
本文介绍了使用ITextsharp将带有图像的ASP.Net网页导出为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hai all,



我使用下面的代码将asp.net网页转换为pdf。



但我得到这样的错误字典中没有给定的密钥这行htmlparser.Parse(sr);

 Response.ContentType =   application / pdf; 
Response.AddHeader( content-disposition attachment; filename = TestPage.pdf);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this .Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
文件pdfDoc = 文件(PageSize.A4,10f,10f,100f,0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc,Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();



请帮帮我。

解决方案

查看这些links..2nd和3rd link包含转换代码。比较你的代码..

将ASP.NET网页转换为PDF [ ^ ]

http://forums.asp.net/t/1444214.aspx/1 [ ^ ]

http://aspdotnetcodebook.blogspot.com/2009/04/how-to-convert-web-page-to-pdf.html [<一个href =http://aspdotnetcodebook.blogspot.com/2009/04/how-to-convert-web-page-to-pdf.html\"target =_ blanktitle =新窗口> ^ ]

代替htmlparser.Parse(sr);

使用此



StringBuilder strB = new StringBuilder( html);



使用(TextReader sReader = new StringReader(strB.ToString()))
{
列出< IElement > 列表= HTMLWorker.ParseToList(sReader,new StyleSheet());
foreach(列表中的IElement elm)
{
Doc.Add(elm);
}
}







问候



Rajkumar P


Hai all,

I used this below code for converting asp.net web page to pdf.

but i am getting error like this The given key was not present in the dictionary on this line htmlparser.Parse(sr);

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();


please help me out this.

解决方案

See these links..2nd and 3rd link contains code for conversion.Compare your code with those..
Convert ASP.NET web page to PDF[^]
http://forums.asp.net/t/1444214.aspx/1[^]
http://aspdotnetcodebook.blogspot.com/2009/04/how-to-convert-web-page-to-pdf.html[^]


In Place of "htmlparser.Parse(sr);"
Use this

StringBuilder strB = new StringBuilder(html);

using (TextReader sReader = new StringReader(strB.ToString()))
           {
               List<IElement> list = HTMLWorker.ParseToList(sReader, new StyleSheet());
               foreach (IElement elm in list)
               {
                   Doc.Add(elm);
               }
           }




Regards

Rajkumar P


这篇关于使用ITextsharp将带有图像的ASP.Net网页导出为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆