在asp.net中使用itextsharp将HTML转换为pdf
我在将html转换为pdf时遇到问题 我用于转换的代码如下在asp.net中使用itextsharp将HTML转换为pdf,asp.net,Asp.net,我在将html转换为pdf时遇到问题 我用于转换的代码如下 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
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();
}
}
<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>
使用iTextSharp.text;
使用iTextSharp.text.html.simpleparser;
使用iTextSharp.text.pdf;
使用制度;
使用System.Collections.Generic;
使用系统配置;
使用系统数据;
使用System.IO;
使用System.Text.RegularExpressions;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
受保护的void ConvertToPDFNow()
{
StringWriter sw=新的StringWriter();
HtmlTextWriter w=新的HtmlTextWriter(sw);
打印。渲染控制(w);
字符串htmWrite=sw.GetStringBuilder().ToString();
Response.ContentType=“application/pdf”;
AddHeader(“内容处置”、“附件;文件名=filename.pdf”);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
htmWrite=Regex.Replace(htmWrite,“,”);
HTMLWrite=HTMLWrite.Replace(“\r\n”,”);
StringReader=新的StringReader(HTMLWrite);
文档文档=新文档(PageSize.A4);
HTMLWorker解析器=新的HTMLWorker(doc);
GetInstance(doc,Response.OutputStream);
doc.Open();
尝试
{
parser.Parse(reader);
}
捕获(例外情况除外)
{ }
最后
{
doc.Close();
}
}
我正在传递一个包含图像、表格、内联样式和id的html文本。
转换时,我在以下位置收到一个错误:“引用的对象未设置为实例”
parser.Parse(reader)
怎么办
我想转换成pdf的html文本如下
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();
}
}
<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>
到
报价编号
00005
这个
新泽西州
报价日期
26/04/2012
主键
查询日期
09/09/2010
巴特
殷勤
123
艾哈迈达巴德
333333
电话
787878787
古吉拉特邦
主键
亲爱的先生/女士:
谢谢您的询价,我们希望对以下产品报最低价格。
ItemPrice/UnitRemarksImage
1.
制作
PN061549845
安全
皮带
合规性:NA
卢比。
381
/
网络操作系统。
马坎德
付款条件
有效性
销售税
交货时间表
运费
7天