0

I have the following table and I need to downloaded to PDF.

The method downloads to pdf but it does not display the table's border.

Thank you!

protected void Button2_Click(object sender, EventArgs e) {

    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);

    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

    Repeater_AK.RenderControl(htmlTextWriter);
    StringReader stringReader = new StringReader(stringWriter.ToString());
    Document Doc = new Document(PageSize.A4, 160, 160, 20, 20);

    HTMLWorker htmlparser = new HTMLWorker(Doc);
    PdfWriter.GetInstance(Doc, Response.OutputStream);

    Doc.Open();
    htmlparser.Parse(stringReader);
    Doc.Close();
    Response.Write(Doc);
    Response.End();

}

HTML

 <table cellspacing="0" style="border-style:solid; font-family:Calibri; border-width:thin;" >
                                   <tr style="height: 26.4pt;">
                        <td align="center" colspan="6" 
                            style="padding-left:5.4pt; padding-right:5.4pt;">
                            <asp:Label ID="lbl_title" runat="server" 
                                Style="font-weight: bold; font-size:7pt; padding-left:5.4pt; padding-right:5.4pt;">ALASKA INSURANCE IDENTIFICATION CARD</asp:Label>
                        </td>
                    </tr>

                    <tr style="height: 8.4pt;">
                        <td colspan="2" 
                            style="max-width:98.1pt; padding-left:5.4pt; padding-right:5.4pt; padding-bottom:0px;">
                            <asp:Label ID="lbl_Company_NAIC_Number" runat="server" 
                                Style="font-weight:bold; font-size:7pt;" Text="Company NAIC Number"></asp:Label>
                        </td>
                        <td style="width:43.2pt; padding-left:5.4px; padding-right:5.4pt; padding-bottom:0px;">
                            <asp:Label ID="lbl_Commercial" runat="server" 
                                Style="font-weight:bold; font-size:7pt;" Text="Commercial"></asp:Label>
                        </td>
                        <td style="width:20.7pt; padding-left:5.4pt; padding-right:5.4pt; padding-bottom:0px;">

                        </td>
                        <td style="width:38.7pt; padding-left:5.4pt; padding-right:5.4pt; padding-bottom:0px;">
                            <asp:Label ID="lbl_Personal" runat="server" Style="font-weight:bold; font-size:7pt;" Text="Personal"></asp:Label>
                        </td>
                        <td style="width:16.2pt; padding-left:5.4pt; padding-right:5.4pt; padding-bottom:0px;">

                        </td>
                    </tr>

                    <tr valign="top">
                        <td colspan="2" style="width:108.9pt; padding-left:5.4pt; padding-right:5.4pt;">
                            <asp:Label ID="lbl_Company_NAIC_Number_out" runat="server" Style="font-size:5pt;" Text="31089"></asp:Label>

                        </td>
                        <td colspan="4" 
                            style="width:162pt; padding-left:5.4pt; padding-right:5.4pt;">
                            <asp:Label ID="lbl_empty2" runat="server" 
                                Style="font-weight:bold; font-size:7pt;" Text=""></asp:Label>
                        </td>
                    </tr>

                    <tr style="height: 21.6pt;" valign="top">
                        <td colspan="2" 
                            style="width:108.9pt; padding-left:5.4pt; padding-right:5.4pt;">

                            <asp:Label ID="lbl_Company_Name_and_Address" runat="server" 
                                Style="font-weight:bold; font-size:7pt;" Text="Company Name and Address"></asp:Label>
                                <p style="margin:0px;">
                                </p>
                            <asp:Label ID="lbl_Company_Name_and_Address_out" runat="server" Style="font-size:5pt;" Text="Repwest Insurance Company"></asp:Label>

                        </td>
                        <td colspan="4" 
                            style="width:162pt; padding-left:5.4pt; padding-right:5.4pt;">
                            <asp:Label ID="lbl_Company_Phone_Number" runat="server" 
                                Style="font-weight:bold; font-size:7pt;" Text="Company Phone Number"></asp:Label>
                                <p style="margin:0px;">
                                </p>
                                <asp:Label ID="lbl_Company_Phone_Number_out" runat="server" 
                                Style="font-size:5pt;" Text="1-800-858-0317"></asp:Label>
                        </td>
                    </tr>

                    <tr style="height: 21.6pt;" valign="top">
                        <td colspan="2" 
                            style="width:108.9pt; padding-left:5.4pt; padding-right:5.4pt;">
                            <asp:Label ID="lbl_Policy_Number" runat="server" 
                                Style="font-weight:bold; font-size:7pt;" Text="Policy Number"></asp:Label>
                                 <p style="margin:0px;">
                                </p>
                                <asp:Label ID="lbl_Policy_Number_out" runat="server" 
                                Style="font-size:5pt;" Text="PolicyPlaceHolder"></asp:Label>
                        </td>
                        <td colspan="2" 
                            style="width:85.5pt; padding-left:5.4pt; padding-right:5.4pt;">
                            <asp:Label ID="lbl_Effective_Date" runat="server" 
                                Style="font-weight:bold; font-size:7pt;" Text="Effective Date"></asp:Label>
                                 <p style="margin:0px;">
                                </p>
                                  <asp:Label ID="lbl_Effective_Date_out" runat="server" 
                                Style="font-size:5pt;" Text="4/1/2015"></asp:Label>
                        </td>
                        <td colspan="2" 
                            style="width:76.5pt; padding-left:5.4pt; padding-right:5.4pt;">
                            <asp:Label ID="lbl_Expiration_Date" runat="server" 
                                Style="font-weight:bold; font-size:7pt;" Text="Expiration Date"></asp:Label>
                                 <p style="margin:0px;">
                                </p>
                                <asp:Label ID="lbl_Expiration_Date_out" runat="server" 
                                Style="font-size:5pt;" Text="3/31/2016"></asp:Label>
                        </td>
                    </tr>

                    <tr style="height: 22.2pt;" valign="top">
                        <td style="width:50.4pt; padding-left:5.4pt; padding-right:5.4pt;">
                            <asp:Label ID="lbl_Year" runat="server" 
                                Style="font-weight:bold; font-size:7pt;" Text="Year"></asp:Label>
                                     <p style="margin:0px;">
                                </p>
                                <asp:Label ID="lbl_Year_out" runat="server" 
                                Style="font-size:5pt;" Text='<%# Eval("YEAR") %>'></asp:Label>
                        </td>
                        <td style="width:58.5pt; padding-left:5.4pt; padding-right:5.4pt;">
                            <asp:Label ID="lbl_Make_Model" runat="server" 
                                Style="font-weight:bold; font-size:7pt;" Text="Make/Model"></asp:Label>
                                     <p style="margin:0px;">
                                </p>
                                <asp:Label ID="lbl_Make_Model_out" runat="server" 
                                Style="font-size:5pt;" Text='<%# Eval("MODEL") %>'></asp:Label>
                        </td>
                        <td colspan="4" 
                            style="width:162pt; padding-left:5.4pt; padding-right:5.4pt;">
                            <asp:Label ID="lbl_VIN" runat="server" Style="font-weight:bold; font-size:7pt;" 
                                Text="Vehicle Identification Number"></asp:Label>
                                     <p style="margin:0px;">
                                </p>
                                <asp:Label ID="lbl_VIN_out" runat="server" Style="font-size:5pt;" 
                                Text='<%# Eval("VIN") %>'></asp:Label>
                        </td>
                    </tr>

                    <tr style="height: 37.8pt;" valign="top">
                        <td colspan="2" 
                            style="width:108.9pt; padding-left:5.4pt; padding-right:5.4pt;">
                            <asp:Label ID="lbl_Agency" runat="server" 
                                Style="font-weight:bold; font-size:7pt;" Text="Agency/Company Issuing Card"></asp:Label>
                                       <p style="margin:0px;">
                                </p>
                                   <asp:Label ID="lbl_Agency_out" runat="server" 
                                Style="font-size:5pt;">Company<br />ADDRESS<br />PHOENIX, AZ 85024</asp:Label>
                        </td>
                        <td colspan="4" 
                            style="width:162pt; padding-left:5.4pt; padding-right:5.4pt;">
                            <asp:Label ID="lbl_Insured_Name" runat="server" 
                                Style="font-weight:bold; font-size:7pt;" Text="Insured Name and Address"></asp:Label>
                                        <p style="margin:0px;">
                                </p>
                                 <asp:Label ID="lbl_Insured_Name_out" runat="server" Style="font-size:5pt;"><%# Eval("INSURED_NAME") %><br /><%# Eval("INSURED_ADDRESS") %><br /><%# Eval("INSURED_CITY")%>, <%# Eval("INSURED_STATE")%>&nbsp <%# Eval("INSURED_ZIPCODE") %></asp:Label>
                        </td>
                    </tr>

                    <tr style="height: 9.6pt;" valign="top">
                        <td align="center" colspan="6" 
                            style="width:260.1pt; padding-left:5.4pt; padding-right:5.4pt;">
                            <asp:Label ID="lbl_Not_valid" runat="server" 
                                Style="font-weight:bold; font-size:7pt;" 
                                Text="Not valid for more than one year from effective date"></asp:Label>
                        </td>
                    </tr>

                    <tr style="height: 12pt;">
                        <td align="center" colspan="6" 
                            style="width:260.1pt; padding-left:5.4pt; padding-right:5.4pt;">
                            <asp:Label ID="lbl_see_importance" runat="server" 
                                Style="font-weight:bold; font-size:7pt;" 
                                Text="See Important notice on reverse side"></asp:Label>
                        </td>
                    </tr>

                    <tr style="height: 13.7pt;">
                        <td colspan="6" 
                            style="width:260.1pt; padding-left:5.4pt; padding-right:5.4pt;">


                        </td>
                    </tr>
                </table>
Lily
  • 221
  • 1
  • 2
  • 11
  • Hi Lily, unfortunately the more lines of code you post the less likely your question will be answered, and you've got 366 which is a lot. Also, your code is not reproducible since we don't have your database. It would help if you posted way less code and something that we can all enter into VS and have it just work, ideally by abstracting your very specific problem to a more general one. For instance, a database is a collection of rows which are collections of cells, maybe (for demonstration purposes) you could just have an array of objects. – Chris Haas Oct 23 '14 at 13:52
  • Hello Chris thank you for helping with my problem. I substitute the code with a link so you see the demo. Also I included the second option to download the repeater. Once again thank you! – Lily Oct 23 '14 at 16:31
  • Thanks Lily, we're getting closer to something to work with. I'd recommend reading [this (unfortunately long) post about converting HTML to PDF](http://stackoverflow.com/a/25164258/231316). First off (paragraph 5), you'll find that `HTMLWorker` is obsolete, no longer supported and has a very, very basic HTML/CSS parser. You should switch to `XMLWorker` instead. Second (paragraph 4), once iTextSharp gets involved, *ASP.Net no longer matters* and there *must* only be HTML and CSS. If you could post a very small example of your HTML and CSS we might be able to help you more. – Chris Haas Oct 23 '14 at 16:45
  • I think I posted too much HTML. I have no CSS script except for the watermark which is an image. Everything else I coded in html body. What I don't get is why the table border doesn't get displayed. If I could get that displayed I would be in clear waters. Thank you! – Lily Oct 23 '14 at 16:50
  • This is still ASPX and not HTML. Can you render this and then only post the HTML that you want iTextSharp to parse? – Chris Haas Oct 23 '14 at 17:57
  • I changed the post Chris. What I don't get is that the pdf displays the table in the format I want but not its border. – Lily Oct 23 '14 at 18:25

1 Answers1

0

Ultimately your problem is that you are using an obsolete HTML parser, you should actually be getting a warning from your IDE if you're using a recent version of iTextSharp, and unless you have a lot of legacy code, there really isn't a good reason to not be running the most recent version. If you're running the 4.x version because of the license differences I strongly recommend you read the fourth block on this page.

Regardless, HTMLWorker has very limited support for style commands and border is not one of them. If you want to continue using HTMLWorker, read the fifth paragraph here which tells you which attributes you can use. If you want to change the border using HTMLWorker you're only option that I'm aware of is setting it on the HTML tag itself:

<table border="1">

However, if you switch over to XMLWorker your code will work as expected as long as you change your border-width property to an actual unit like 1px:

<table style="border-style:solid; border-width: 1px;">
Community
  • 1
  • 1
Chris Haas
  • 53,986
  • 12
  • 141
  • 274