在Pdf中显示/下载表格

时间:2014-10-23 01:27:35

标签: c# itextsharp

我有下表,我需要下载到PDF。

该方法下载为pdf但不显示表格的边框。

谢谢!

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>

1 个答案:

答案 0 :(得分:0)

最终你的问题是你使用的是过时的HTML解析器,如果你使用的是最近版本的iTextSharp,你实际上应该从你的IDE收到警告,除非你有很多遗留代码,否则真的没有没有运行最新版本的一个很好的理由。如果由于许可证差异而运行4.x版本,我强烈建议您阅读fourth block on this page

无论如何,HTMLWorker对样式命令的支持非常有限,border不是其中之一。如果您想继续使用HTMLWorker,请阅读fifth paragraph here,告诉您可以使用哪些属性。如果您想使用HTMLWorker更改边框,我只知道将其设置在HTML标记本身上:

<table border="1">

但是,如果您切换到XMLWorker,只要您将border-width属性更改为1px之类的实际单位,您的代码就会按预期运行:

<table style="border-style:solid; border-width: 1px;">