0

Update: ITextSharp 5.5.2 Supports this feature but previous version of ITextSharp do not.

Does ITextSharp 5.5.1 support Class Element CSS selectors? Such as

<style>
    .test td {
        border: 1px solid green;
    }
</style>

I'm getting a result like this

Bad CSS

When it should be something like

Good CSS

If not how would I go about achieving the same results?

Using the following code I do not get the desired results.

byte[] bytes;
Document document = new Document();

using (var memoryStream = new MemoryStream())
using (var pdfWriter = PdfWriter.GetInstance(document, memoryStream))
{
    document.Open();
    XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, document, new StringReader(html));
    document.Close();
    bytes = memoryStream.ToArray();
}

return bytes;

Here is my full html for reference

<html>
<head>
<style>
    .test td {
        border: 1px solid green;
    }
</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>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Rovert Renchirk
  • 97
  • 1
  • 11

1 Answers1

1

This is a strange question since the default implementation of XML Worker does exactly what you need. We have an HTML file table_css.html that is converted to html_table_3.pdf and we use nothing more than the basic XML Worker code: ParseHtmlTable3

enter image description here

You only need 5 lines to achieve the result shown in the screen shot:

public void createPdf(String file) throws IOException, DocumentException {
    // step 1
    Document document = new Document();
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
    // step 3
    document.open();
    // step 4
    XMLWorkerHelper.getInstance().parseXHtml(writer, document,
            new FileInputStream(HTML));
    // step 5
    document.close();
}
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Could the difference be between IText in Java and ITextSharp in C#? I updated my original post with the code I am using. – Rovert Renchirk Jul 10 '14 at 16:29
  • 1
    Maybe the difference is the version. I'm using the version in the trunk (it will be released in a couple of weeks, and if I recall correctly, the XML Worker team has been working on better CSS support recently). What version are you using? – Bruno Lowagie Jul 10 '14 at 16:31
  • I'm using version 5.5.1. Is there an easy way to get the trunk assembly? – Rovert Renchirk Jul 10 '14 at 16:38
  • Using the trunk it seems to be working! A snapshot of r783 to be precise. Thanks! I'll look forward to 5.5.2 then. – Rovert Renchirk Jul 10 '14 at 17:16