iText没有生成预期的PDF格式 [英] iText not generating expected PDF format

查看:180
本文介绍了iText没有生成预期的PDF格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  String template =; //下面给出的内容是为了提高可读性
OutputStream file = new FileOutputStream(new File(D:\\Test.pdf));
凭证凭证=新凭证();
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)>样本标题< / b>
< img style =float:rightheight =25pxwidth =80pxsrc =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>日期< / td>
< td class =col>:dfdfjkdjk< / td>
< / tr>
< / table>
< br />
< br />
< br />
< hr />
< br />
联系我们
< / body>
< / html>

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





但在PDF格式中我得到的结果是这样的





我无法找到解决方法。

解决方案

请查看



你的结果有什么不同?



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



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

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

现在,当您使用<$时,前导的值将导致12个用户单位的空行c $ c>< br /> 代码。



至于徽标的对齐方式:我建议您将徽标放入一个两列表,其中文本被添加到第一列,徽标被添加到第二列。


I am working on PDF generation using iText, Below is the code snippet.

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

My template looks like this:

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

It looks like the given image in browser

But in PDF i am getting something like this

I am not able to find how to fix this.

解决方案

Please take a look at the ParseHtml12 and the corresponding result: test2015-11.pdf

What is the difference with your result?

In your result, you are using <br /> tags which result in new line operations that take into account the leading. When you start a new page, the leading is 0. The leading changes as soon as you have some normal paragraphs where a leading is defined for the first time.

In your case, the definition of the leading never happens, so you have to do it yourself. For instance:

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

Now the value of the leading will result in empty lines of 12 user units when you use the <br /> tag.

As for the alignment of the logo: I suggest that you put that logo in a two column table where the text is added to the first column and the logo to the second column.

这篇关于iText没有生成预期的PDF格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆