解析HTML片段并添加到PdfPTable [英] Parsing HTML snippets and adding to PdfPTable

查看:685
本文介绍了解析HTML片段并添加到PdfPTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个由几个PdfPTable组成的PDF,其中一些PdfCell由简单的短语组成,其他的则需要解析HTML片段。为了确保解析的HTML添加了所需的样式,并在正确的位置,我已将其存储在段落中,然后将其添加到PdfPCell。但是,这样做会导致我在处理列表和引号等HTML标记时遇到问题。下面是我正在做的一个粗略的例子,我该怎么做才能正确处理HTML列表,引号等?

I am creating a PDF made of several PdfPTables where some PdfCell's consist of simple Phrases and others need to be parsed HTML snippets. To make sure the parsed HTML is added with the needed styling and in the correct place I have been storing it in a Paragraph then adding it to a PdfPCell. However doing this causes me to run into issues when dealing with some HTML tags like lists and quotes. Below is a rough example of what I am doing, what can I do to properly handle HTML lists, quotes, etc. ?

例如:iText正确处理HTML列表并知道将其转换为iText List / ListItem。我需要将该List添加到我的PdfTable中。我知道把列表元素放在段落中取消了正确的样式(整个列表最终都在一行没有编号),并且想知道处理这个的正确方法

For example: iText properly handles a HTML list and knows to convert it to an iText List/ListItem. I need to add that List into my PdfTable. I know putting the List Element in a Paragraph cancels out the proper styling (the entire list ends up being on one line with no numbering) and would like to know the proper way of handling this

PdfPTable table = new PdfPTable(1);
    table.addCell(parseHtmlToParagraph(htmlString));
    table.addCell(new Phrase("Name" + user.getName()));







public Paragraph parseHtmlToParagraph(String str) throws IOException {
    StringReader body = new StringReader(str);
    final Paragraph para = new Paragraph();

    XMLWorkerHelper.getInstance().parseXHtml(new ElementHandler() {
        @Override
        public void add(Writable w) {
            if (w instanceof WritableElement) {
                List<Element> elements = ((WritableElement) w).elements();
                for (Element e : elements) {
                    para.add(e);
                }
            }
        }
    }, body);

    return para;
}


推荐答案

答案很简单:你通过在文本模式中创建单元格而不是在复合模式中创建单元格,抛弃所有结构(例如列表结构)。

The answer is simple: you are throwing away all structure (such as a list structure), by creating a cell in text mode instead of creating a cell in composite mode.

像这样创建你的单元格:

Create your cell like this:

PdfPCell cell = new PdfPCell();
List<Element> elements = ((WritableElement) w).elements();
for (Element e : elements) {
     cell.addElement(e);
}

您隐式创建 PdfPCell 实例使用 addCell()方法。您正在将段落传递给此方法,但此段落已投放到词组。当您使用短语隐式创建 PdfPCell 时,该短语中的所有内容将被降级为纯文本元素。

You are implicitly creating a PdfPCell instance by using the addCell() method. You are passing a Paragraph to this method, but this Paragraph is casted to a Phrase. When you implicitly create a PdfPCell with a Phrase, all content present in that Phrase will be downgraded to mere text elements.

这篇关于解析HTML片段并添加到PdfPTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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