【问题标题】:Set width of table cell in PDF ITextSharp在 PDF ITextSharp 中设置表格单元格的宽度
【发布时间】:2014-02-10 19:25:48
【问题描述】:

我正在使用 itextsharp 将 html 表格转换为 pdf。

它工作正常,但宽度属性在 PDF 中不起作用。意味着当您将表格列的固定宽度更改为 20% 或 20px 时,它不会反映在 PDF 上。

如何设置PDF的列宽?

我的代码:

"<table border='1'>"
    + "<thead><tr>" 
        + "<th style='width:10%;'>ID</th>"
        + "<th style='width:70%;'>Testing Point</th>"
        + "<th style='width:20%;'>Notes</th>"
    + "</tr></thead></table>"

上面的代码显示了带边框的表格,但 PDF 的总宽度分为 3 部分。每个单元格采用相同的宽度。

如何固定表格的宽度,使其采用用户定义的列宽。

请帮忙

谢谢

【问题讨论】:

标签: c# jquery asp.net pdf itextsharp


【解决方案1】:

您必须在每行中为 td 设置百分比宽度,而不仅仅是在标题行中。

<table width="75%">
  <tr>
    <th scope="col" width="10%">&nbsp;</th>    <th scope="col">&nbsp;</th>
  </tr>
  <tr>
    <td width="10%">&nbsp;</td>    <td>&nbsp;</td>
  </tr>
  <tr>
    <td width="10%">&nbsp;</td>    <td>&nbsp;</td>
  </tr>
</table>

【讨论】:

  • 不适用于旧版本的 ITextSharp。所以请确保您使用的是最新版本!