iText无法生成预期的PDF格式

时间:2015-11-19 05:16:34

标签: java pdf itext

我正在使用iText进行PDF生成,下面是代码片段。

belongsTo

我的模板如下所示:

String template="";//content given below for more readability
OutputStream file = new FileOutputStream(new File("D:\\Test.pdf"));
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, file);
document.open();
InputStream is = new ByteArrayInputStream(k.getBytes());
XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);

它看起来像浏览器中的给定图像

expected pdf

但是在PDF格式中我得到了类似的东西

actual pdf

我无法找到解决方法。

1 个答案:

答案 0 :(得分:1)

请查看ParseHtml12及相应的结果:test2015-11.pdf

enter image description here

你的结果有什么不同?

在您的搜索结果中,您使用的是<br />代码,这会导致新的线条操作考虑到前导。当你开始一个新的页面时,前导是0.一旦你有一些正常的段落,首次定义一个前导,领先的变化。

在你的情况下,领导的定义永远不会发生,所以你必须自己做。例如:

PdfWriter writer = PdfWriter.getInstance(document, file);
writer.setInitialLeading(12);

现在,使用<br />标记时,前导值将导致12个用户单位的空行。

关于徽标的对齐方式:我建议您将该徽标放在两列表格中,其中文本添加到第一列,徽标添加到第二列。