Konvertieren von HTML in pdf mit itextsharp in asp.net

Ich habe ein prblem bei der Umwandlung von html in pdf" -

Dem code, die ich verwendet für die Konvertierung ist wie folgt

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

Leite ich einen html-text, welches Bild, Tabelle, inline-styles, id.
Während der Konvertierung erhalte ich eine Fehlermeldung "Objekt verwiesen wird, nicht auf eine Instanz" in

parser.Parse(reader);

Was ist zu tun?

Den html-text, die ich in pdf konvertieren ist wie folgt

<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>

InformationsquelleAutor MARKAND Bhatt | 2012-03-26

Schreibe einen Kommentar