我创建了一个简单的 JavaFX (2.2) FXML 项目。我正在尝试将 html 字符串转换为不同的元素,并将它们添加到 iText 生成的 PDF 文档中的段落中。由于 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>
, ... 或其他标签时,问题似乎开始了。