iText中的行分隔符? [英] Line Separator in iText?

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

问题描述

我正在尝试使用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
-----------

这应该可以满足您的需求。不是最佳解决方案,但它是一种解决旧jar限制的方法。

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中的行分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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