编程语言
首页 > 编程语言> > 使用JavaFX htmleditor的itext xmlworker

使用JavaFX htmleditor的itext xmlworker

作者:互联网

我创建了一个简单的JavaFX(2.2)FXML项目.我正在尝试将html字符串转换为不同的Elements并将它们添加到iText生成的PDF文档中的Paragraph中.由于不推荐使用HTMLWorker,因此我使用XMLWorker.它似乎确实可以使用自己编写的简单html字符串,例如:

StringReader in = new StringReader("<html><body><p>test</p></body></html>");

但是,当我尝试使用JavaFX HTMLEditor控件生成的html字符串时,收到以下消息:

Caused by: java.lang.NoSuchMethodError: com.itextpdf.tool.xml.html.pdfelement.NoNewLineParagraph.setMultipliedLeading(F)V
at com.itextpdf.tool.xml.html.AbstractTagProcessor.currentContentToParagraph(AbstractTagProcessor.java:268)
at com.itextpdf.tool.xml.html.Span.end(Span.java:77)
at com.itextpdf.tool.xml.html.AbstractTagProcessor.endElement(AbstractTagProcessor.java:192)
at com.itextpdf.tool.xml.pipeline.html.HtmlPipeline.close(HtmlPipeline.java:207)
at com.itextpdf.tool.xml.XMLWorker.endElement(XMLWorker.java:142)
at com.itextpdf.tool.xml.parser.XMLParser.endElement(XMLParser.java:396)
at com.itextpdf.tool.xml.parser.state.ClosingTagState.process(ClosingTagState.java:71)
at com.itextpdf.tool.xml.parser.XMLParser.parseWithReader(XMLParser.java:236)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:214)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:149)
at hmtltopdf.FXMLDocumentController.handleButtonAction(FXMLDocumentController.java:64)
... 54 more

我的猜测是html字符串中有一堆XMLWorker无法识别的标签. HTML编辑器创建以下html字符串:

<html><head></head><body contenteditable="true"><p style="text-align: left;"><font face="'Segoe UI'">test</font></p></body></html>

如果是这样,是否有办法“清理” html字符串?还是我必须使用其他某种HTML编辑器?

我的示例项目的代码:

//using iText 5.3.1 and XMLWorker 5.5.0

@FXML
private HTMLEditor htmlEditor;

@FXML
private void handleButtonAction(ActionEvent event) {
    final Document document = new Document();

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:/test/loremipsum.pdf"));

    document.open();

    //StringReader in = new StringReader("<html><body><p>test</p></body></html>"); //this works
    StringReader in = new StringReader(htmlEditor.getHtmlText()); //this does not work

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

        document.add(test);
    } catch (IOException | DocumentException e) {
        System.out.println(e.toString());
        System.out.println(e.getMessage());
    }

    document.close();
}

编辑:

当使用< b>,< i>,…或其他标签时,问题似乎开始了.

解决方法:

嗯,似乎必须使用较旧的iText版本来做一些事情.我已经更新到iText 5.5.0,而且现在似乎可以使用.我仍然必须删除所有< br>和< hr>标签你.

//using iText 5.5.0 and XMLWorker 5.5.0

@FXML
private HTMLEditor htmlEditor;

@FXML
private void handleButtonAction(ActionEvent event) {
    final Document document = new Document();

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:/test/loremipsum.pdf"));

    document.open();

    String htmlString = htmlEditor.getHtmlText();

    htmlString = htmlString.replace("<br>", "");
    htmlString = htmlString.replace("<br/>", "");
    htmlString = htmlString.replace("<br />", "");

    htmlString = htmlString.replace("<hr>", "<p></p>");
    htmlString = htmlString.replace("<hr/>", "<p></p>");
    htmlString = htmlString.replace("<hr />", "<p></p>");

    StringReader in = new StringReader(htmlString);

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

        document.add(test);
    } catch (IOException | DocumentException e) {
        System.out.println(e.toString());
        System.out.println(e.getMessage());
    }

    document.close();
}

标签:itext,javafx-2,pdf-generation,xmlworker,java
来源: https://codeday.me/bug/20191121/2054469.html