首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在itext7中使用pdfHtml的<fieldset>和<legend>标记

在itext7中使用pdfHtml的<fieldset>和<legend>标记
EN

Stack Overflow用户
提问于 2020-04-06 18:16:53
回答 1查看 265关注 0票数 0

使用HTML文件,我用iText pdfHTML生成了PDF文件。我想要为内容显示标题,就像在HTML <fieldset><legend>中所做的那样。

我使用了下面的HTML代码。在HTML页面中,它会按预期显示。但是当使用pdfHTML生成PDF文件时,“摘要”会出现在框内。不在框的边框中以显示内容的标题。

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<body>

 <fieldset>
  <legend>Summary</legend>
  <p>Some paragraph</p>  
 </fieldset>


</body>
</html>

我该如何解决这个问题呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-08 02:45:01

pdfHTML还不支持fieldset标签,实际上你的样例被转换成了如下格式:

您可以将您的自定义实现插入到pdfHTML中,以在一定程度上调整视觉外观(取决于您的需求)。下面是一个关于如何通过大量假设来调整视觉外观的基本示例:

代码语言:javascript
复制
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);
    }
}

然后,您只需将您的标记工作者工厂传递给转换器属性:

代码语言:javascript
复制
ConverterProperties properties = new ConverterProperties();
properties.setTagWorkerFactory(new CustomTagWorkerFactory());
HtmlConverter.convertToPdf(inFile, outFile, properties);

现在我们得到以下结果:

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61057500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档