iText 单元格边框穿过文本
我正在编写一个程序,使用 iText 生成其中包含表格的 pdf 或 rtf 文件。 我使用了 iText 类表格和单元格,而不是更具体的 RtfTable 或 pdfTable,以便最后可以生成任一文件。 我需要将单元格填充值设置为 -1,否则打印表上的每行数据之间的空间太大。 但是,我现在尝试添加边框(特别是 pdf 文件),但单元格未与文本对齐。 每个单元格的底部边框直接穿过文本。 仅当单元格内边距设置为 2 或更高时,它才会真正包围文本。 下面是我正在做的示例:
Document document = new Document();
Paragraph paragraph = new Paragraph();
Font iTextFont = new Font(Font.TIMES_ROMAN, 9, Font.NORMAL);
try{
PdfWriter.getInstance(document, new FileOutputStream("C:/datafiles/TestiText.pdf"));
document.open();
Table table = new Table(3);
table.setPadding(-1);
table.setWidth(90);
Cell cell1 = new Cell();
cell1.setBorder(Rectangle.BOX);
cell1.setVerticalAlignment(ElementTags.ALIGN_TOP);
table.setDefaultCell(cell1);
paragraph = new Paragraph("header", iTextFont);
Cell cell = new Cell(paragraph);
cell.setHeader(true);
cell.setColspan(3);
table.addCell(cell);
paragraph = new Paragraph("example cell", iTextFont);
table.addCell(paragraph);
paragraph = new Paragraph("one", iTextFont);
table.addCell(cell);
paragraph = new Paragraph("two", iTextFont);
cell = new Cell(paragraph);
table.addCell(paragraph);
paragraph = new Paragraph("Does this start a new row?", iTextFont);
table.addCell(paragraph);
paragraph = new Paragraph("Four", iTextFont);
table.addCell(paragraph);
paragraph = new Paragraph("Five", iTextFont);
table.addCell(paragraph);
document.add(table);
} catch (Exception e) {
//handle exception
}
document.close();
}
有没有办法解决这个问题,或者通过将整个边框向下移动(不影响文本位置),或者消除每行之间的空格(间距似乎只是文本上方的问题,而不是下方的问题)而不将单元格填充设置为 -1?
I am writing a program that generates a pdf or rtf file with a table in it, using iText. I used the iText class table and cell, rather than the more specific RtfTable or pdfTable so that either file can be generated at the end. I needed to set the cell padding to a value of -1, or else there was too much space between each row of data on the printed sheet. However, I am now trying to add borders (specifically to the pdf file), and the cells are not lining up with the text. The bottom border of each cell cuts directly through the text. It only actually surrounds the text when the cell padding is set to 2 or higher. Below is a sample of what I am doing:
Document document = new Document();
Paragraph paragraph = new Paragraph();
Font iTextFont = new Font(Font.TIMES_ROMAN, 9, Font.NORMAL);
try{
PdfWriter.getInstance(document, new FileOutputStream("C:/datafiles/TestiText.pdf"));
document.open();
Table table = new Table(3);
table.setPadding(-1);
table.setWidth(90);
Cell cell1 = new Cell();
cell1.setBorder(Rectangle.BOX);
cell1.setVerticalAlignment(ElementTags.ALIGN_TOP);
table.setDefaultCell(cell1);
paragraph = new Paragraph("header", iTextFont);
Cell cell = new Cell(paragraph);
cell.setHeader(true);
cell.setColspan(3);
table.addCell(cell);
paragraph = new Paragraph("example cell", iTextFont);
table.addCell(paragraph);
paragraph = new Paragraph("one", iTextFont);
table.addCell(cell);
paragraph = new Paragraph("two", iTextFont);
cell = new Cell(paragraph);
table.addCell(paragraph);
paragraph = new Paragraph("Does this start a new row?", iTextFont);
table.addCell(paragraph);
paragraph = new Paragraph("Four", iTextFont);
table.addCell(paragraph);
paragraph = new Paragraph("Five", iTextFont);
table.addCell(paragraph);
document.add(table);
} catch (Exception e) {
//handle exception
}
document.close();
}
Is there a way to resolve this issue either by moving the whole border down a drop (without affecting the text placement), or to get rid of the spaces between each line (spacing appears to only be a problem above the text, not below) without setting the cell padding to -1?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编写一个类或常用方法来构建您的表 - 无论您使用的是 Table 还是 PdfPTable。
这些方法将为您处理标准对齐、基于上升/下降的测量等。它们还提供了一个公共位置来添加“3pt 空白段落”或您可能需要的任何其他标准格式。
面向对象的软件并不是要消除重复的、可能不一致的代码部分。
希望这可以帮助。
Write a class or common methods to build your table -- whether you're using Table or PdfPTable.
These methods will handle standard alignment, measurement based on ascenders/descenders, etc for you.. They also provide a common place to add a "3pt blank paragraph" or whatever other standard formattings, you may need.
OO software isn't meant to be about clanking out repetitive, and potentially inconsistent, sections of code.
Hope this helps.
你应该使用
PdfPTable
它有很多有用且包装良好的组件的方法我发布了这个答案,以便任何遇到同样问题的人都知道从哪里开始
它可能不是问题的典型答案,但它来了...
在表格中组织内容
PDF 输出
you should use
PdfPTable
it has many method that is useful and well wrapped componenti posted this answer so any one faces the same problem know where to start
it might not be the typical answer to the question but here it comes...
Organizing content in tables
The PDF output