使用HTML文件,我用iText pdfHTML生成了PDF文件。我想要为内容显示标题,就像在HTML <fieldset>和<legend>中所做的那样。
我使用了下面的HTML代码。在HTML页面中,它会按预期显示。但是当使用pdfHTML生成PDF文件时,“摘要”会出现在框内。不在框的边框中以显示内容的标题。
<!DOCTYPE html>
<html>
<body>
<fieldset>
<legend>Summary</legend>
<p>Some paragraph</p>
</fieldset>
</body>
</html>我该如何解决这个问题呢?
发布于 2020-04-08 02:45:01
pdfHTML还不支持fieldset标签,实际上你的样例被转换成了如下格式:

您可以将您的自定义实现插入到pdfHTML中,以在一定程度上调整视觉外观(取决于您的需求)。下面是一个关于如何通过大量假设来调整视觉外观的基本示例:
private static class LegendTagWorker extends DivTagWorker {
public LegendTagWorker(IElementNode element, ProcessorContext context) {
super(element, context);
}
@Override
public IPropertyContainer getElementResult() {
IPropertyContainer result = super.getElementResult();
// Shift the position of the legend a bit above
result.setProperty(Property.POSITION, LayoutPosition.RELATIVE);
result.setProperty(Property.TOP, -14);
// Set the background of the text to white so that it does not overlap with the border of outer element
if (result instanceof Div && ((Div) result).getChildren().get(0) instanceof Paragraph) {
Paragraph p = (Paragraph) ((Div) result).getChildren().get(0);
((Text)p.getChildren().get(0)).setBackgroundColor(ColorConstants.WHITE);
}
return result;
}
}
private static class CustomTagWorkerFactory extends DefaultTagWorkerFactory {
@Override
public ITagWorker getCustomTagWorker(IElementNode tag, ProcessorContext context) {
if (tag.name().equals("legend")) {
return new LegendTagWorker(tag, context);
}
return super.getCustomTagWorker(tag, context);
}
}然后,您只需将您的标记工作者工厂传递给转换器属性:
ConverterProperties properties = new ConverterProperties();
properties.setTagWorkerFactory(new CustomTagWorkerFactory());
HtmlConverter.convertToPdf(inFile, outFile, properties);现在我们得到以下结果:

请注意,这是一个非常基本的示例,您需要根据自己的需要对其进行调整。在pdfHTML中实现此功能之前,您必须使用一种变通方法--或者按照我的建议在代码中使用,或者可以使用CSS进行一些调整,类似于我在Java代码中所做的调整
https://stackoverflow.com/questions/61057500
复制相似问题