我创建了一个简单的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工作室无法识别html字符串中的一堆标记。 HTML编辑器创建了这个html字符串:
<html><head></head><body contenteditable="true"><p style="text-align: left;"><font face="'Segoe UI'">test</font></p></body></html>
如果是这种情况,有没有办法清理&#39; 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>
,...或其他标签时似乎问题就出现了。
答案 0 :(得分:3)
似乎它必须使用较旧的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();
}