How can I convert XHTML nested list to pdf with iText?

前端 未结 2 1493
情书的邮戳
情书的邮戳 2020-11-30 14:21

I have XHTML content, and I have to create from this content a PDF file on the fly. I use iText pdf converter. I tried the simple way, but I always get bad result after call

2条回答
  •  Happy的楠姐
    2020-11-30 14:59

    Please take a look at the example NestedListHtml

    In this example, I take your code snippet list.html:

    • First
      1. Second
      2. Second
    • First

    And I parse it into an ElementList:

    // CSS
    CSSResolver cssResolver =
        XMLWorkerHelper.getInstance().getDefaultCssResolver(true);
    
    // HTML
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
    htmlContext.autoBookmark(false);
    
    // Pipelines
    ElementList elements = new ElementList();
    ElementHandlerPipeline end = new ElementHandlerPipeline(elements, null);
    HtmlPipeline html = new HtmlPipeline(htmlContext, end);
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
    
    // XML Worker
    XMLWorker worker = new XMLWorker(css, true);
    XMLParser p = new XMLParser(worker);
    p.parse(new FileInputStream(HTML));
    

    Now I can add this list to the Document:

    for (Element e : elements) {
        document.add(e);
    }
    

    Or I can list this list to a Paragraph:

    Paragraph para = new Paragraph();
    for (Element e : elements) {
        para.add(e);
    }
    document.add(para);
    

    You will get the desired result as shown in nested_list.pdf

    You can not add nested lists to a PdfPCell or to a ColumnText. For instance: this will not work:

    PdfPTable table = new PdfPTable(2);
    table.addCell("Nested lists don't work in a cell");
    PdfPCell cell = new PdfPCell();
    for (Element e : elements) {
        cell.addElement(e);
    }
    table.addCell(cell);
    document.add(table);
    

    This is due to a limitation in the ColumnText class that has been there for many years. We have evaluated the problem and the only way to fix this, would be to rewrite ColumnText entirely. This is not an item on our current technical road map.

提交回复
热议问题