gpt4 book ai didi

java - itext xmlworker 与 javafx htmleditor

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:48:14 26 4
gpt4 key购买 nike

我创建了一个简单的 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> , ... 或其他标签。

最佳答案

啊,这似乎与使用较旧的 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();
}

关于java - itext xmlworker 与 javafx htmleditor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23390011/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com