Java iText旧版本中的换行符?
我试图用iText在文档中插入一个行分隔符(你知道,横穿文档的水平行)。我通过谷歌找到了一些使用com.lowagie.text.pdf.draw.LineSeparator的资源,但我使用的iText版本(1.4.2)似乎没有这个包 有人能给我的pdf添加一个好的行分隔符吗?请不要说更新.jar——我被锁定在1.4.2中Java iText旧版本中的换行符?,java,pdf,itext,Java,Pdf,Itext,我试图用iText在文档中插入一个行分隔符(你知道,横穿文档的水平行)。我通过谷歌找到了一些使用com.lowagie.text.pdf.draw.LineSeparator的资源,但我使用的iText版本(1.4.2)似乎没有这个包 有人能给我的pdf添加一个好的行分隔符吗?请不要说更新.jar——我被锁定在1.4.2中 谢谢 在iText的早期版本中,这方面有点混乱。如果将元素存储在PdfPCell中水平线上方,则可以将其边框设置为仅显示底部。(如果需要,该单元格也可以为空) 结果应该是(实
谢谢 在iText的早期版本中,这方面有点混乱。如果将元素存储在PdfPCell中水平线上方,则可以将其边框设置为仅显示底部。(如果需要,该单元格也可以为空) 结果应该是(实线,而不是方格线) 这会给你你想要的。这不是最佳解决方案,但它是一种绕过旧罐子局限性的方法 作为参考,如果您想执行此技巧,请在文本的上下各放一行,以给出
-----------
Hello World
-----------
setBorder()的参数是一个int,可以对其使用逐位运算来处理值。因此,上述示例可以通过
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
这将创建一个如下所示的表(假设您更正了我的输入错误)
示例:
table.setExtendLastRow(true)代码>
我会的 我也赞成使用行元素而不是表。。。不要重复HTML格式错误
final LineSeparator lineSeparator = new LineSeparator();
lineSeparator.drawLine(pdfCB, leftX, rightX, y);
只需向pdf文档对象添加一个行分隔符对象。应该是这样
LineSeparator objectName = new LineSeparator();
document.add(objectName);
Sean给出的解决方案在处理带有行分隔符下划线的文本时提供了更大的灵活性。
我不知道LineSeparator是否能做到这一点,它似乎只是一个行分隔符
Paragraph ph = new Paragraph(new Phrase("My line separator", yourFont));
PdfPCell cell = new PdfPCell(ph);
cell.Border = Rectangle.BOTTOM_BORDER;
cell.BorderColor = new BaseColor(44, 67, 144);
cell.BorderWidth = 2f;
PdfPTable table = new PdfPTable(1);
table.AddCell(cell);
table.HorizontalAlignment = Element.ALIGN_LEFT;
table.WidthPercentage = 100f;
doc.Add(table);
希望这能有所帮助。
应该打印这样的内容。如果您想要一个全新的行,请使用一种简单的方法:
document.add(Chunk.NEWLINE);
LineSeparator ls = new LineSeparator();
document.add(new Chunk(ls));
我也面临类似的问题,因为我的公司也在使用旧版本的iText,即1.4.2。这是我为创建水平规则而提出的两种解决方案。第一个使用图形,第二个使用带底部边框的表格。这两种方法对我都适用
解决方案1:
protected static final Graphic HR = new Graphic();
static {
HR.setHorizontalLine(1f, 100f, Color.BLACK);
}
解决方案2:
private static void addHorizontalLine(Document document, PdfWriter writer) throws DocumentException, IOException{
PdfPTable myTable = new PdfPTable(1);
myTable.setWidthPercentage(100.0f);
PdfPCell cellOne = new PdfPCell();
cellOne.setBorder(Rectangle.BOTTOM);
document.add(new Paragraph(" "));
document.add(myTable);
}
PS:我们不能更新JAR的原因是旧版本的iText可以免费用于商业用途,而新版本是付费的
希望能有帮助 我发现这与HTML
标记非常相似:
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.pdf.canvas.draw.SolidLine;
import com.itextpdf.layout.element.LineSeparator;
...
final SolidLine lineDrawer = new SolidLine(1f);
lineDrawer.setColor(Color.GRAY);
document.add(new LineSeparator(lineDrawer));
(iText 7.0.0)看起来这几乎正是我所需要的,但是边框会填满整个页面,还是只到段落的末尾我基本上需要一条从页面的一边到另一边的线。在上面的编辑中给出了一个如何实现这一点的示例。我刚刚在这里写了这个,所以当你发现语法错误时不要大喊大叫。关键部分是查看myTable.setWidthPercentage()。这与父元素有关。假设你把它添加到一个文档对象中,你就有了一个100%伸展文档的表格。。。我一直在寻找这一个,也就是说,将LineSeparator
添加到PdfContentByte
中。做了一件好事!:)OP特别说他不能用它。重复回答。op明确表示他不能使用LineSeparator。op明确表示他不能使用LineSeparator
document.add(Chunk.NEWLINE);
LineSeparator ls = new LineSeparator();
document.add(new Chunk(ls));
protected static final Graphic HR = new Graphic();
static {
HR.setHorizontalLine(1f, 100f, Color.BLACK);
}
private static void addHorizontalLine(Document document, PdfWriter writer) throws DocumentException, IOException{
PdfPTable myTable = new PdfPTable(1);
myTable.setWidthPercentage(100.0f);
PdfPCell cellOne = new PdfPCell();
cellOne.setBorder(Rectangle.BOTTOM);
document.add(new Paragraph(" "));
document.add(myTable);
}
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.pdf.canvas.draw.SolidLine;
import com.itextpdf.layout.element.LineSeparator;
...
final SolidLine lineDrawer = new SolidLine(1f);
lineDrawer.setColor(Color.GRAY);
document.add(new LineSeparator(lineDrawer));