-->

how can I add cellspacing to pdftable when parsing

2019-03-04 11:22发布

问题:

I am using XMLWorker and itext to convert html to pdf . my html have a table and I need to set it's cellspacing =0 cellpadding=0 . does anyone know how to do it ?

in html I saw I can replace it by setting the style :

border-collapse: collapse; border-spacing: 0px ; border : 0; padding : 0;

thanks Tami

回答1:

I've tried what you're doing using the CSS you propose and it works for me:

You can find my test here: ParseHtmlTable5

This is my HTML (including the CSS): table3_css.html

<html>
<head>
<style>
    table, td {
        border: 1px solid green;
        border-spacing: 0px;
        padding: 0px;
    }
</style>
</head>
<body>
<table class='test'>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td>Peter</td>
    <td>Griffin</td>
    <td>$100</td>
  </tr>
  <tr>
    <td>Lois</td>
    <td>Griffin</td>
    <td>$150</td>
  </tr>
  <tr>
    <td>Joe</td>
    <td>Swanson</td>
    <td>$300</td>
  </tr>
  <tr>
    <td>Cleveland</td>
    <td>Brown</td>
    <td>$250</td>
</tr>
</table>
</body>
</html>

I suggest that you compare your HTML with mine to find out what you're doing wrong. You should also use the latest version of XML Worker and iText(Sharp) as we've improved HTML parsing significantly in the latest releases.

Note that I've defined a solid, green border of 1px to prove that there is no padding and no spacing between the cells. If you change the CSS like this:

<style>
    table, td {
        border: 0px;
        border-spacing: 0px;
        padding: 0px;
    }
</style>

You'll get the (ugly) version of a table without borders, without spacing between the cells and without padding inside the cells.