元素内部类CSS定义不适用使用ITextSharp(5.5.1)XHTML到PDF [英] Element inside class CSS definition not applied using ITextSharp (5.5.1) XHTML to PDF

查看:460
本文介绍了元素内部类CSS定义不适用使用ITextSharp(5.5.1)XHTML到PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:ITextSharp 5.5.2支持此功能,但以前版本的ITextSharp不支持。

ITextSharp 5.5.1支持类元素CSS选择器?如

Does ITextSharp 5.5.1 support Class Element CSS selectors? Such as

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

我得到了这样的结果

喜欢

如果不是,我将如何获得相同的结果?

使用以下代码获得所需的结果。

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;

这是我的完整html参考

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>


推荐答案

这是一个奇怪的问题,因为XML的默认实现工人完全按照你的需要。我们有一个HTML文件 table_css.html 转换为 html_table_3.pdf ,我们只使用基本的XML Worker代码: ParseHtmlTable3

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

您只需要5行来实现屏幕截图中显示的结果:

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();
}

这篇关于元素内部类CSS定义不适用使用ITextSharp(5.5.1)XHTML到PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆