iText旧版本中LineSeparator的替代者? [英] Alternative for LineSeparator in iText old versions?

查看:523
本文介绍了iText旧版本中LineSeparator的替代者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用iText将行分隔符(横跨文档的水平线)插入我的文档中.我已经通过Google找到了一些使用com.lowagie.text.pdf.draw.LineSeparator的资源,但是我正在使用的iText版本(1.4.2)似乎没有该软件包.

I'm trying to insert a line separator (you know, that horizontal line that runs across documents) into my document with iText. I've found some resources via Google that use com.lowagie.text.pdf.draw.LineSeparator but the version of iText that I'm using (1.4.2) doesn't seem to have that package.

有人可以建议另一种方法来为我的pdf添加漂亮的行分隔符吗?而且请不要说更新.jar,我已锁定为1.4.2.

Can anyone suggest another way to add a nice line seperator for my pdf? And please don't say update the .jar-- I'm locked in to 1.4.2.

谢谢!

推荐答案

在早期版本的iText中,此方法有些混乱.如果将元素存储在PdfPCell中水平线上方,则可以设置其边框以仅显示底部. (如果需要,该单元格也可以为空白)

There is a bit of a messy way around this in the earlier versions of iText. If you store the element above the horizontal line in a PdfPCell, you can then set the border of that to show only the bottom. (That cell can also be blank if needed)

PdfPCell myCell = new PdfPCell(new Paragraph("Hello World") );
myCell.setBorder(Rectangle.BOTTOM);

结果应该看起来像(实线,不是方格的)

The result should look like (solid line, not checkered)

Hello World
-----------

这应该给您您想要的东西.不是最佳解决方案,而是一种解决旧罐子的局限性的方法.

This should give you what you desire. Not the optimal solution but it is a way to work around the limitations of the old jar.

供参考,如果您想执行此技巧,请在文本的上方和下方放置一行以得出

For your reference, if you want to perform this trick to put a line on top and below your text to give a result of

-----------
Hello World
-----------

setBorder()的参数是一个int,您可以使用按位运算来操纵这些值.因此,以上示例可以通过

The argument to setBorder() is an int which you can use bitwise operation on to manipulate the values. So the above example can would be accomplished with

myCell.setBorder(Rectangle.BOTTOM | Rectangle.TOP);

示例

//Create the table which will be 2 Columns wide and make it 100% of the page
PdfPTable myTable = new PdfPtable(2);
myTable.setWidthPercentage(100.0f);

//create a 3 cells and add them to the table
PdfPCell cellOne = new PdfPCell(new Paragraph("Hello World"));
PdfPCell cellTwo = new PdfPCell(new Paragraph("Bottom Left"));
PdfPcell cellThree = new PdfPCell(new Paragraph("Bottom Right"));

cellOne.setColspan(2);
cellOne.setBorder(Rectangle.BOTTOM);
cellOne.setHorizontalAlignment(Element.ALIGN_LEFT);

cellTwo.setBorder(Rectangle.NO_BORDER);
cellTwo.setHorizontalAlignment(Element.ALIGN_LEFT);
cellThree.setBorder(Rectangle.LEFT);
cellThree.setHorizontalAlignment(Element.ALIGN_RIGHT);

//Add the three cells to the table
myTable.addCell(cellOne);
myTable.addCell(cellTwo);
myTable.addCell(cellThree);

//Do something to add the table to your root document

这应该会为您创建一个表格,其外观类似于以下内容(假设您更正了我的错字)

This should create you a table which looks something like the following (assuming you correct my typos)

Hello World
------------------------------------
Bottom Left      |      Bottom Right

这篇关于iText旧版本中LineSeparator的替代者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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