itext7设置标签样式ITagWorkerFactory报凑No worker found for tag em
时间: 2024-03-26 22:40:30 浏览: 272
出现"No worker found for tag"的错误通常是因为缺少相应的标签工厂类。在iText7中,标签工厂类可以通过实现ITagWorkerFactory接口来创建。
针对你提到的"em"标签,可以尝试以下代码:
```java
class EmTagWorkerFactory implements ITagWorkerFactory {
@Override
public ITagWorker createTagWorker(IElementNode tag, ProcessorContext context) {
return new EmTagWorker(tag, context);
}
}
class EmTagWorker implements ITagWorker {
private ProcessorContext context;
private IElementNode tag;
public EmTagWorker(IElementNode tag, ProcessorContext context) {
this.tag = tag;
this.context = context;
}
@Override
public void processEnd(IElementNode endElement, ProcessorContext context) {
// do nothing
}
@Override
public void process(ProcessorContext context) {
// do nothing
}
}
```
然后在创建PDF的过程中,将该工厂类添加到HtmlConverter类的构造函数中即可:
```java
HtmlConverter.convertToPdf(html, os, new ConverterProperties()
.setBaseUri(baseUri)
.setTagWorkerFactory(new DefaultTagWorkerFactory() {
@Override
public ITagWorker getCustomTagWorker(IElementNode tag) {
if ("em".equalsIgnoreCase(tag.name())) {
return new EmTagWorkerFactory().createTagWorker(tag, this);
}
return super.getCustomTagWorker(tag);
}
}));
```
这样,在HTML中使用"em"标签时,就可以正常转换为PDF了。
阅读全文