我正在尝试使用iText将行分隔符(您知道,跨文档运行的水平线)插入到我的文档中.我通过Google找到了一些使用com.lowagie.text.pdf.draw.LineSeparator的资源,但我正在使用的iText版本(1.4.2)似乎没有该软件包.
任何人都可以建议另一种方法为我的PDF添加一个好的行分隔符?请不要说更新.jar--我已经锁定到1.4.2.
谢谢!
小智 27
LineSeparator ls = new LineSeparator();
document.add(new Chunk(ls));
Run Code Online (Sandbox Code Playgroud)
示例:iText正在运行中
Sea*_*ean 15
在早期版本的iText中,有一种混乱的方式.如果将元素存储在PdfPCell中的水平线上方,则可以将其边框设置为仅显示底部.(如果需要,该单元格也可以为空白)
PdfPCell myCell = new PdfPCell(new Paragraph("Hello World") );
myCell.setBorder(Rectangle.BOTTOM);
Run Code Online (Sandbox Code Playgroud)
结果看起来应该是(实线,没有方格)
Hello World
-----------
Run Code Online (Sandbox Code Playgroud)
这应该给你你想要的.不是最佳解决方案,但它是一种解决旧jar限制的方法.
作为参考,如果你想要执行这个技巧,在文本的顶部和下方放一条线来给出结果
-----------
Hello World
-----------
Run Code Online (Sandbox Code Playgroud)
setBorder()的参数是一个int,您可以使用按位操作来操作值.所以上面的例子可以完成
myCell.setBorder(Rectangle.BOTTOM | Rectangle.TOP);
Run Code Online (Sandbox Code Playgroud)
编辑:示例
//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
Run Code Online (Sandbox Code Playgroud)
这应该创建一个看起来像下面的表(假设你纠正我的错别字)
Hello World
------------------------------------
Bottom Left | Bottom Right
Run Code Online (Sandbox Code Playgroud)
我也赞成使用Line元素而不是表格...不要重复HTML格式错误!
final LineSeparator lineSeparator = new LineSeparator();
lineSeparator.drawLine(pdfCB, leftX, rightX, y);
Run Code Online (Sandbox Code Playgroud)
小智 5
只需将行分隔符对象添加到pdf文档对象.那应该是它
LineSeparator objectName = new LineSeparator();
document.add(objectName);
Run Code Online (Sandbox Code Playgroud)