gpt4 book ai didi

java - iText 未生成预期的 PDF 格式

转载 作者:行者123 更新时间:2023-12-01 10:52:13 25 4
gpt4 key购买 nike

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

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);

我的模板如下所示:

<html>
<head>
<style>.col{padding:3px 20px 3px 20px}</style>
</head>
<body style="font-family:tahoma">
<div style="background:rgb(230,230,230); padding:5px ;border:1px solid black;">
<b style="color:rgb(51,153,255)">Sample header</b>
<img style="float:right" height="25px" width= "80px" src="resources/images/itext.png" />
</div>
<br />
<table border='0' style='border-collapse: collapse;'>
<tr>
<td class="col">String 1</td>
<td class="col">: 1234354545</td>
</tr>
<tr>
<td class="col">String 2</td>
<td class="col">: rere</td>
</tr>
<tr>
<td class="col">String 3</td>
<td class="col">: ureuiu</td>
</tr>
<tr>
<td class="col">Date</td>
<td class="col">: dfdfjkdjk</td>
</tr>
</table>
<br />
<br />
<br />
<hr/>
<br />
Contact us
</body>
</html>

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

expected pdf

但是在 PDF 中我得到了这样的东西

actual pdf

我无法找到解决此问题的方法。

最佳答案

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

enter image description here

与你的结果有什么不同?

在您的结果中,您使用的是 <br />标签导致新行操作考虑前导。当您开始新页面时,行距为 0。一旦您有一些首次定义行距的普通段落,行距就会发生变化。

在你的例子中,行距的定义永远不会发生,所以你必须自己做。例如:

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

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

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

关于java - iText 未生成预期的 PDF 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33795772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com