2012-03-26 117 views
1

我有在转换HTML到PDF一个prblem使用iTextSharp的转换HTML到PDF在asp.net

我用于转换的代码如下

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(); 
     } 
} 

我传递的HTML文本其中包含图像,表格,内嵌样式,标识。 虽然转换我收到一个错误“对象引用未设置为实例”在

parser.Parse(reader);

怎么办?

,我要转换为PDF格式的HTML文本如下

<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;"> 
       &nbsp; 
      </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> 

回答

1

尝试根据自己的源代码,这个代码和 添加一些引用

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(); 
    }