转换为pdf时,Html表丢失格式

时间:2015-04-08 05:42:40

标签: asp.net

我正在使用Itextsharp.dll将html表格转换为pdf.Pls在我的代码下方找到。

ASPX:

<asp:Panel ID="PnlTable" runat="server" ScrollBars="Horizontal" Width="707px" Visible="false">
<asp:Table ID="tbDynamic" CellPadding="2" CellSpacing="2" Width="100%" runat="server">
</asp:Table>
</asp:Panel>

<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />

cs:

protected void btnExport_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
Label lb = new Label();
Table dt = new Table();
TableRow trow = new TableRow();
TableCell tcell = new TableCell();
LiteralControl l1 = new LiteralControl();


PnlTable.Visible = true;

sb.Append("<table style='width:100%;text-align:center' border='1'>");
sb.Append("<tr>");
sb.Append("<td>Index</td>");
sb.Append("</tr>");

sb.Append("<tr>");
sb.Append("<td>");
sb.Append("<table style='width:100%;' border='0'>");
sb.Append("<tr>");
sb.Append("<td>First</td>");
sb.Append("<td>Second</td>");
sb.Append("<td>Third</td>");
sb.Append("</tr>");

sb.Append("<tr>");
sb.Append("<td>Fourth</td>");
sb.Append("<td>Fifth</td>");
sb.Append("<td>Six</td>");
sb.Append("<tr>");

sb.Append("<tr>");
sb.Append("<td>Seven</td>");
sb.Append("<td>Eight</td>");
sb.Append("<td>Nine</td>");
sb.Append("</tr>");

sb.Append("</table>");

sb.Append("</td>");
sb.Append("</tr>");
sb.Append("</table>");


l1 = new LiteralControl(sb.ToString());
tcell.Controls.Add(l1);
trow.Controls.Add(tcell);
tbDynamic.Controls.Add(trow);


Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
PnlTable.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();
PnlTable.Visible = false;
Response.Write(pdfDoc);
Response.End();

}

输出我喜欢这个

________________________________________________________
|- Index |
________________________________________________________
| First Second Third |

| Fourth Fifth Six |

| Seven Eight Nine |
________________________________________________________

我想将列线应用于内部表格而不是水平。如果应用border = 1,它将边框应用于行以及列。如何应用?我已经尝试rules="clos"并应用了boder样式to td.But它不工作。它在html表中工作。但是当我转换为pdf时,表格无法格式化

我想要这样。

________________________________________________________
|- Index |
________________________________________________________
| First | Second | Third |

| Fourth | Fifth | Six |

| Seven | Eight | Nine |
________________________________________________________

此处有垂直断线。我想要连续线

1 个答案:

答案 0 :(得分:0)

您可以在这里找到关于样式表所需的一切:https://css-tricks.com/complete-guide-table-element/