HTML转换为PDF使用iTextSharp的在asp.net [英] Convert HTML To pdf using itextsharp in asp.net
本文介绍了HTML转换为PDF使用iTextSharp的在asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有在将HTML转换为PDF prblem
在code这是我用来转换如下:
使用iTextSharp.text;
使用iTextSharp.text.html.simpleparser;
使用iTextSharp.text.pdf;
使用系统;
使用System.Collections.Generic;
使用System.Configuration;
使用System.Data这;
使用System.IO;
使用System.Text.RegularEx pressions;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;
保护无效ConvertToPDFNow()
{
StringWriter的SW =新的StringWriter();
HtmlTextWriter的W =新的HtmlTextWriter(SW);
print.RenderControl(重量);
。字符串htmWrite = sw.GetStringBuilder()的ToString();Response.ContentType =应用程序/ PDF
Response.AddHeader(内容处置,附件;文件名= FileName.pdf);
Response.Cache.SetCacheability(HttpCacheability.NoCache); htmWrite = Regex.Replace(htmWrite,< /(A | A)*>?。?,);
htmWrite = htmWrite.Replace(\\ r \\ n,);
StringReader读卡器=新StringReader(htmWrite); 文档DOC =新的文件(PageSize.A4);
HTMLWorker分析器=新HTMLWorker(DOC);
PdfWriter.GetInstance(DOC,Response.OutputStream);
doc.Open();
尝试
{
parser.Parse(读卡器);
}
赶上(异常前)
{}
最后
{
doc.Close();
}
}
我传递有图像,表格,内联样式,ID的HTML文本。
虽然转换我得到一个错误对象引用未设置为实例在
parser.Parse(读卡器);
该怎么办?
我要转换为PDF格式HTML文本如下:
< DIV ID =打印>
< DIV的风格=WIDTH:870px; ID =打印>
< DIV ID =dvSoftCopy的风格=显示:无;>
< IMG SRC =HTTP://本地主机:56814 /报价/图片/ head.jpg的风格=宽度:100%/>
< / DIV>
< DIV ID =dvHardCopy的风格=高度:197.29px;>
&安培; NBSP;
< / DIV>
< DIV的风格=显示:无;>
<表格的宽度=100%>
&所述; TR>
< TD风格=宽度:100%;高度:的3px;>
<小时风格=高度:的3px;颜色:蓝色; />
< / TD>
< / TR>
< /表>
< / DIV>
< DIV CLASS =divBorder>
<表样式=颜色:蓝色;字体大小:11像素;填充左:14px的;>
&所述; TR>
< TD合并单元格=2的风格=宽度:50%;>
至
< / TD>
&所述; TD>
报价否
< / TD>
< TD风格=填充左:为5px;>
<跨度ID =lblQuoteNo的风格=字体重量:大胆;> 00005< / SPAN>
< / TD>
< / TR>
&所述; TR>
&所述; TD列跨度=2>
该
&所述;跨度的id =lblDesignation> NJ&下; /跨度>
< / TD>
&所述; TD>
报价日期
< / TD>
< TD风格=填充左:为5px;>
26/04/2012
< / TD>
< / TR>
&所述; TR>
&所述; TD列跨度=2>
&所述;跨度的id =lblPartyName> PK&下; /跨度>
< / TD>
&所述; TD>
询盘日期
< / TD>
< TD风格=填充左:为5px;>
<跨度ID =lblEnquiryDate> 09/09/2010< / SPAN>
< / TD>
< / TR>
&所述; TR>
&所述; TD列跨度=2>
&所述;跨度的id =lblAddress>巴特&下; /跨度>
< / TD>
&所述; TD>
那么殷勤恳切
< / TD>
< TD风格=填充左:为5px;>
&所述;跨度的id =lblContactPerson&GT 123下; /跨度>
< / TD>
< / TR>
&所述; TR>
&所述; TD>
&所述;跨度的id =lblPCity> AHMEDABAD&下; /跨度>
< / TD>
&所述; TD>
<跨度ID =lblPPostal code> 333333< / SPAN>
< / TD>
&所述; TD>
电话
< / TD>
< TD风格=填充左:为5px;>
&所述;跨度的id =lblPPhone> 787878787&下; /跨度>
< / TD>
< / TR>
&所述; TR>
&所述; TD>
&所述;跨度的id =lblState> GUJARAT&下; /跨度>
< / TD>
&所述; TD列跨度=3>
&所述;跨度的id =原产国> PK&下; /跨度>
< / TD>
< / TR>
< /表>
< / DIV>
< DIV CLASS =divBorder>
<表样式=FONT-SIZE:11像素;填充左:14px的;>
&所述; TR>
&所述; TD>
尊敬的先生/女士
< BR />
感谢您的查询,我们想引用我们的最低下列项目。
< / TD>
< / TR>
< /表>
< / DIV>
< DIV的风格=填充左:14px的;填充右:14px的;类=divBorder>
< DIV>
<表类=DataList控件CELLSPACING =0规则=所有边界=1ID =grdItemList的风格=宽度:100%;边境崩溃:崩溃;>
&所述; TR>
百分位范围=山口>项目< /第i个百分位范围=山口>价格/单位< /第i个百分位范围=山口>说明< /第i个百分位ALIGN =中心范围=山口>形象和LT; /第i
< / TR>< TR风格=高度:100像素;>
< TD风格=宽度:45%;>
< DIV的风格=的位置是:相对;高度:150像素;>
< DIV的风格=的位置是:绝对的;>
< DIV的风格=文本转换:大写; FONT-SIZE:13px的;>
< B>
1。 使
PN061549845
SAFTY
< / B>
< / DIV>
带
< BR />
合规性:NA
< / DIV>
< / DIV>
< / TD>< TD VALIGN =顶的风格=高度:100像素;宽度:20%;>
卢比。
&所述;跨度的id =grdItemList_ctl02_lblPrice> 381&下; /跨度>
/
号。
< / TD>< TD风格=高度:100像素;宽度:20%;> Markand< / TD>< TD VALIGN =中间的风格=高度:100像素;宽度:25%;&GT ;
< IMG ID =grdItemList_ctl02_imgItemSRC =HTTP://本地主机:56814 /报价/图片/ default.jpg的风格=高度:70像素;宽度:70像素,边框宽度:0像素; />
< / TD>
< / TR>
< /表>
< / DIV>
< / DIV>
< DIV的风格=填充左:14px的;填充右:12px的;类=divBorder>
< BR />
<表类=tableclass>
&所述; TR>
&所述; TD宽度=20%>
< B>付款方式和LT; / B>
< / TD>
&所述; TD宽度=20%>
< B>有效性和LT; / B>
< / TD>
&所述; TD宽度=20%>
< B>销售税< / B>
< / TD>
&所述; TD宽度=20%>
< B>交付进度和LT; / B>
< / TD>
< TD WIDTH =17%>
< B>货运及LT; / B>
< / TD>
< / TR>
&所述; TR>
&所述; TD>
&所述;跨度的id =lblPaymentTerms大于7天&下; /跨度>
< / TD>
&所述; TD>
&所述;跨度的id =lblSalesTax>增值税15%下; /跨度>
< / TD>
&所述; TD>
&所述;跨度的id =lblValidity>30天&下; /跨度>
< / TD>
&所述; TD>
&所述;跨度的id =lblDelivery>&下; /跨度>
天
< / TD>
&所述; TD>
<跨度ID =lblFreight>付费搜索和LT; / SPAN>
< / TD>
< / TR>
< /表>
< / DIV>
< DIV的风格=填充左:14px的;类=divBorder>
< BR />
在任何进一步的疑问时,请随时通过E-mail或电话与我们联系。
感谢你并期待有机会服务于贵组织。
< / DIV>
< DIV的风格=填充左:14px的;下边框:1px的固体#858585;类=divBorder>
<表格的宽度=100%>
&所述; TR>
< TD align =left>
< / TD>
&所述; TD> < / TD>
< TD ALIGN =右>
< IMG SRC =HTTP://本地主机:56814 /报价/图片/ logo.jpg/>
< / TD>
< / TR>
&所述; TR>
&所述; TD列跨度=2>
< / TD>
< / TR>
< /表>
< / DIV>
< / DIV>
< / DIV>
解决方案
尝试根据源$ C $ C此code和
添加一些引用
使用System.Configuration;
使用iTextSharp.text;
使用iTextSharp.text.pdf;
使用System.IO;
使用System.Windows.Forms的;
使用System.Drawing.Printing; 保护无效ButtonPDF_Click(对象发件人,EventArgs的发送)
{
字符串标题=PDF表;
GETPDF(标题,Chartpdf); //传递参数来获得图 }
公共无效GETPDF(字符串标题,图表chart1)
{
文档PDOC =新的文档(PageSize.A4,72,72,82,72);
MemoryStream的毫秒=新的MemoryStream();
PdfWriter作家= PdfWriter.GetInstance(PDOC,MS);
pdoc.Open();
大块C =新的块(标题,FontFactory.GetFont(VERDANA,25,5,颜色:Color.GREEN));
VAR IMG = Image.GetInstance(图(chart1));
//块C =新的块(divchart);
第P =新的第();
p.Alignment = Element.ALIGN_CENTER;
p.Add(C);
pdoc.Add(P);
pdoc.Add(IMG);
pdoc.Close();
Response.AddHeader(内容类型,应用程序/ PDF格式);
Response.AddHeader(内容处置,附件;文件名= Chart.pdf);
Response.OutputStream.Write(ms.GetBuffer(),0,ms.GetBuffer()长。);
到Response.End();
}
I am having a prblem in converting html to pdf
The code which i used for conversion is as follows
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
protected void ConvertToPDFNow()
{
StringWriter sw = new StringWriter();
HtmlTextWriter w = new HtmlTextWriter(sw);
print.RenderControl(w);
string htmWrite = sw.GetStringBuilder().ToString();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
htmWrite = Regex.Replace(htmWrite, "</?(a|A).*?>", "");
htmWrite = htmWrite.Replace("\r\n", "");
StringReader reader = new StringReader(htmWrite);
Document doc = new Document(PageSize.A4);
HTMLWorker parser = new HTMLWorker(doc);
PdfWriter.GetInstance(doc, Response.OutputStream);
doc.Open();
try
{
parser.Parse(reader);
}
catch (Exception ex)
{ }
finally
{
doc.Close();
}
}
I am passing an html text which has image, table, inline-styles, id. While converting i am getting an error "Object referenced not set to an instance" at
parser.Parse(reader);
What to do?
The html text which i want to convert to pdf is as follows
<div id="print">
<div style="width: 870px;" id="printable">
<div id="dvSoftCopy" style="display: none;">
<img src="http://localhost:56814/Quotation/images/head.jpg" style="width: 100%" />
</div>
<div id="dvHardCopy" style="height: 197.29px;">
</div>
<div style="display: none;">
<table width="100%">
<tr>
<td style="width: 100%; height: 3px;">
<hr style="height: 3px; color: Blue;" />
</td>
</tr>
</table>
</div>
<div class="divBorder">
<table style="color: Blue; font-size: 11px; padding-left: 14px;">
<tr>
<td colspan="2" style="width: 50%;">
To
</td>
<td>
Quote No
</td>
<td style="padding-left: 5px;">
<span id="lblQuoteNo" style="font-weight:bold;">00005</span>
</td>
</tr>
<tr>
<td colspan="2">
The
<span id="lblDesignation">nj</span>
</td>
<td>
Quote Date
</td>
<td style="padding-left: 5px;">
26/04/2012
</td>
</tr>
<tr>
<td colspan="2">
<span id="lblPartyName">PK</span>
</td>
<td>
Inquiry Date
</td>
<td style="padding-left: 5px;">
<span id="lblEnquiryDate">09/09/2010</span>
</td>
</tr>
<tr>
<td colspan="2">
<span id="lblAddress">bhatt</span>
</td>
<td>
Kind Attentions
</td>
<td style="padding-left: 5px;">
<span id="lblContactPerson">123</span>
</td>
</tr>
<tr>
<td>
<span id="lblPCity">AHMEDABAD</span>
</td>
<td>
<span id="lblPPostalCode">333333</span>
</td>
<td>
Phone
</td>
<td style="padding-left: 5px;">
<span id="lblPPhone">787878787</span>
</td>
</tr>
<tr>
<td>
<span id="lblState">GUJARAT</span>
</td>
<td colspan="3">
<span id="lblCountry">PK</span>
</td>
</tr>
</table>
</div>
<div class="divBorder">
<table style="font-size: 11px; padding-left: 14px;">
<tr>
<td>
Dear Sir/Madam
<br />
Thank you for your inquiry, we wish to quote our lowest for the following items.
</td>
</tr>
</table>
</div>
<div style="padding-left: 14px; padding-right: 14px;" class="divBorder">
<div>
<table class="datalist" cellspacing="0" rules="all" border="1" id="grdItemList" style="width:100%;border-collapse:collapse;">
<tr>
<th scope="col">Item</th><th scope="col">Price/Unit</th><th scope="col">Remarks</th><th align="center" scope="col">Image</th>
</tr><tr style="height:100px;">
<td style="width:45%;">
<div style="position: relative; height: 150px;">
<div style="position: absolute;">
<div style="text-transform: uppercase; font-size: 13px;">
<b>
1.
make
PN061549845
SAFTY
</b>
</div>
Belt
<br />
Compliance:NA
</div>
</div>
</td><td valign="top" style="height:100px;width:20%;">
Rs.
<span id="grdItemList_ctl02_lblPrice">381</span>
/
Nos.
</td><td style="height:100px;width:20%;">Markand</td><td valign="middle" style="height:100px;width:25%;">
<img id="grdItemList_ctl02_imgItem" src="http://localhost:56814/Quotation/images/default.jpg" style="height:70px;width:70px;border-width:0px;" />
</td>
</tr>
</table>
</div>
</div>
<div style="padding-left: 14px; padding-right: 12px;" class="divBorder">
<br />
<table class="tableclass">
<tr>
<td width="20%">
<b>Payment Terms</b>
</td>
<td width="20%">
<b>Validity</b>
</td>
<td width="20%">
<b>Sales Tax</b>
</td>
<td width="20%">
<b>Delivery Schedule</b>
</td>
<td width="17%">
<b>Freight</b>
</td>
</tr>
<tr>
<td>
<span id="lblPaymentTerms">7 Days</span>
</td>
<td>
<span id="lblSalesTax">VAT 15%</span>
</td>
<td>
<span id="lblValidity">30 days</span>
</td>
<td>
<span id="lblDelivery"></span>
Days
</td>
<td>
<span id="lblFreight">Paid</span>
</td>
</tr>
</table>
</div>
<div style="padding-left: 14px;" class="divBorder">
<br />
In case of any futher queries please feel free to contact us by E-mail or Phone.
Thanking you and looking forward to the opportunity to serve your esteemed Organisation.
</div>
<div style="padding-left: 14px; border-bottom: solid 1px #858585;" class="divBorder">
<table width="100%">
<tr>
<td align="left">
</td>
<td>
</td>
<td align="right">
<img src="http://localhost:56814/Quotation/images/logo.jpg" />
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
</table>
</div>
</div>
</div>
解决方案
try this code according to your source code and add some references
using System.Configuration;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Windows.Forms;
using System.Drawing.Printing;
protected void ButtonPDF_Click(object sender, EventArgs e)
{
string title = "pdf chart";
GETPDF(title, Chartpdf); //pass the arguments to get chart
}
public void GETPDF(string title, Chart chart1)
{
Document pdoc = new Document(PageSize.A4, 72, 72, 82, 72);
MemoryStream ms = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(pdoc, ms);
pdoc.Open();
Chunk c = new Chunk(title, FontFactory.GetFont("VERDANA", 25, 5, color: Color.GREEN));
var img = Image.GetInstance(Chart(chart1));
//Chunk c = new Chunk(divchart);
Paragraph p = new Paragraph();
p.Alignment = Element.ALIGN_CENTER;
p.Add(c);
pdoc.Add(p);
pdoc.Add(img);
pdoc.Close();
Response.AddHeader("Content-Type", "application/pdf");
Response.AddHeader("Content-Disposition", "attachment;filename=Chart.pdf");
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
Response.End();
}
这篇关于HTML转换为PDF使用iTextSharp的在asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文