iText xmlWorker上边距

时间:2012-11-01 23:22:12

标签: html-parsing itext margin

当我运行此代码时,我在文档的顶部获得了一个边距。

image

我不知道如何摆脱它。我正在运行最新版本的iText和XMLWorker。

Document document = new Document(PageSize.A5.rotate(), 0f, 0f, 0f, 0f);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E:/itext/test.pdf"));
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);

htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(false);

cssResolver.addCss("p{color: #a00; font-size: 20pt; margin: 0; padding: 0;}", true);

Pipeline<?> pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));

XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser xmlParser = new XMLParser(worker);

document.open();

String htmlData = "<html> <p>Lorem ipsum</p> </html>";

xmlParser.parse(new StringReader(htmlData));

document.close();

2 个答案:

答案 0 :(得分:2)

HTML <p>标记通常在上方和下方有填充。如果你改用<div>标签,也许你会得到你正在寻找的结果?

答案 1 :(得分:1)

您可以在文档对象上设置边距。

document.setMargins(0,0,0,0);