0

I'm trying to make pdf from div using c#. To complete this object i m using iTextShap. I found solution on Internet to create pdf from given html code.

<asp:Panel ID="Panel1" runat="server">
    <table style="border:1px solid">

        <tr style="background-color:green;">
            <td style="width:10%; color:white; font-weight:bold">Sr.</td> 
            <td style="width:60%; color:white; font-weight:bold">Particular</td> 
            <td style="width:25%; color:white; font-weight:bold">Price</td>
        </tr>

        <tr style="background-color:#ffffff">
            <td style="width:10%">01.</td> 
            <td style="width:60%">3 keywords and printers</td> 
            <td style="width:25%">2000 INR</td>
        </tr>

        <tr style="background-color:#e1c5ed">
            <td style="width:10%">02.</td> 
            <td style="width:60%">3 acer laptops</td> 
            <td style="width:25%">90000 INR</td>
        </tr>

        <tr style="background-color:#ffffff">
            <td style="width:10%">03.</td> 
            <td style="width:60%">2 dell laptops</td> 
            <td style="width:25%">60000 INR</td>
        </tr>

    </table>
</asp:Panel>

my browser's output look like this html output image

my button click event's code given below

Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=pdffile.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    Panel1.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();

Above code created pdf without any css style. I want to ask how to apply css and create pdf as like as webpage? When i create pdf my table look like this PDF output Can anybody help me. Thanks in advance.

Narender Godara
  • 105
  • 2
  • 10
  • You are using the wrong class. Don't use `HTMLWorker`, use XML Worker instead! If you had read the [official documentation](http://developers.itextpdf.com/faq/category/parsing-xml-and-xhtml) before asking a question, you would have known that `HTMLWorker` is obsolete, unsupported, and should no longer be used. I will close your question. – Bruno Lowagie Aug 17 '16 at 16:33
  • You will notice that `bgcolor="lightblue"` is used in the example I marked as duplicate. CSS works too, see for instance the answer to [this question](http://stackoverflow.com/questions/33795772) where `style="background:rgb(230,230,230)` is used. The key to your problem is your use of `HTMLWorker` instead of XML Worker. – Bruno Lowagie Aug 17 '16 at 16:51
  • #Bruno Lowagie Thank your for your reply, Now i got it that problem is HTMLWorker and i should use xmlworker. But i have no idea how can i do this. Will you please solve my code. I'm trying this to solve back 3 days. But nothing found on Internet and i'm very beginner in .net. – Narender Godara Aug 17 '16 at 17:03

0 Answers0