itext pdfHtml:设置页边距
我正在使用HTMLConverter将html转换为PDF,并尝试设置一些边距 现行守则:itext pdfHtml:设置页边距,itext,pdfhtml,Itext,Pdfhtml,我正在使用HTMLConverter将html转换为PDF,并尝试设置一些边距 现行守则: ConverterProperties props = new ConverterProperties(); props.setBaseUri("src/main/resources/xslt"); PdfDocument pdf = new PdfDocument(new PdfWriter(new FileOutputStream(dest))); pdf.setDe
ConverterProperties props = new ConverterProperties();
props.setBaseUri("src/main/resources/xslt");
PdfDocument pdf = new PdfDocument(new PdfWriter(new FileOutputStream(dest)));
pdf.setDefaultPageSize(new PageSize(612F, 792F));
HtmlConverter.convertToPdf( html, pdf, props);
有人能就如何增加利润提出建议吗?我使用Document类来设置margin,但不确定这是如何进入HTMLConverter的converttofdf方法的。您不可能使用
HTMLConverter\convertToElements
方法吗。它返回列表
,然后您可以将其元素添加到具有设置边距的文档中:
Document document = new Document(pdfDocument);
List<IElement> list = HtmlConverter.convertToElements(new FileInputStream(htmlSource));
for (IElement element : list) {
if (element instanceof IBlockElement) {
document.add((IBlockElement) element);
}
}
还有另一个解决方案:为
标记实现您自己的自定义标记工作程序,并在其级别上设置边距。例如,要设置零边距,可以为下一个工人创建标签:
public class CustomTagWorkerFactory extends DefaultTagWorkerFactory {
public ITagWorker getCustomTagWorker(IElementNode tag, ProcessorContext context) {
if (TagConstants.HTML.equals(tag.name())) {
return new ZeroMarginHtmlTagWorker(tag, context);
}
return null;
}
}
public class ZeroMarginHtmlTagWorker extends HtmlTagWorker {
public ZeroMarginHtmlTagWorker(IElementNode element, ProcessorContext context) {
super(element, context);
Document doc = (Document) getElementResult();
doc.setMargins(0, 0, 0, 0);
}
}
并将其作为ConverterProperties参数传递给Htmlconverter
:
converterProperties.setTagWorkerFactory(new CustomTagWorkerFactory());
HtmlConverter.convertToPdf(new File(htmlPath), new File(pdfPath), converterProperties);
converterProperties.setTagWorkerFactory(new CustomTagWorkerFactory());
HtmlConverter.convertToPdf(new File(htmlPath), new File(pdfPath), converterProperties);