如何在Itext5中填充页面的剩余空白部分?

http://img1.mukewang.com/614196e10001f59b07320888.jpg

如上所示,我使用 pdfTable 作为页面的正文部分。现在我想用表格的边框,列,行填充整个身体,如果表格没有填充整个身体部分。如下图所示。

http://img4.mukewang.com/614196e70001529008470928.jpg


幕布斯6054654
浏览 245回答 1
1回答

慕斯709654

如果您在创建表时这样做,您可以通过欺骗来完成此操作。我将提供一个部分解决方案,它利用 PDF 中的一个复杂性表:表只是 PDF 中的行。它们不是结构化内容。您可以利用这一点 - 在渲染表格时跟踪您正在绘制垂直线的位置,然后将它们继续到页面底部。让我们创建一个新的单元格事件。它跟踪4件事:left哪个是表格最左边的x坐标,right哪个是表格最右边的x坐标,xCoordinates它是我们画垂直线的所有x坐标的集合,最后cellHeights哪个是一个列表所有单元格高度。class CellMarginEvent implements PdfPCellEvent {&nbsp; &nbsp; Set<Float> xCoordinates = new HashSet<Float>();&nbsp; &nbsp; Set<Float> cellHeights = new HashSet<Float>();&nbsp; &nbsp; Float left = Float.MAX_VALUE;&nbsp; &nbsp; Float right = Float.MIN_VALUE;&nbsp; &nbsp; public void cellLayout(PdfPCell pdfPCell, Rectangle rectangle, PdfContentByte[] pdfContentBytes) {&nbsp; &nbsp; &nbsp; &nbsp; this.xCoordinates.add(rectangle.getLeft());&nbsp; &nbsp; &nbsp; &nbsp; this.xCoordinates.add(rectangle.getRight());&nbsp; &nbsp; &nbsp; &nbsp; this.cellHeights.add(rectangle.getHeight());&nbsp; &nbsp; &nbsp; &nbsp; left = Math.min(left,rectangle.getLeft());&nbsp; &nbsp; &nbsp; &nbsp; right = Math.max(right, rectangle.getRight());&nbsp; &nbsp; }&nbsp; &nbsp; public Set<Float> getxCoordinates() {&nbsp; &nbsp; &nbsp; &nbsp; return xCoordinates;&nbsp; &nbsp; }}然后我们将所有单元格添加到表格中,但暂时不会将表格添加到文档中&nbsp; &nbsp; Document document = new Document();&nbsp; &nbsp; PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(OUTPUT_FILE));&nbsp; &nbsp; document.open();&nbsp; &nbsp; PdfPTable table = new PdfPTable(4);&nbsp; &nbsp; CellMarginEvent cellMarginEvent = new CellMarginEvent();&nbsp; &nbsp; for (int aw = 0; aw < 320; aw++) {&nbsp; &nbsp; &nbsp; &nbsp; PdfPCell cell = new PdfPCell();&nbsp; &nbsp; &nbsp; &nbsp; cell.addElement(new Paragraph("Cell: " + aw));&nbsp; &nbsp; &nbsp; &nbsp; cell.setCellEvent(cellMarginEvent);&nbsp; &nbsp; &nbsp; &nbsp; table.addCell(cell);&nbsp; &nbsp; }不,我们添加 get top- 表格的顶部位置,并将表格添加到文档中。float top = writer.getVerticalPosition(false);document.add(table);然后我们绘制完成的表格的垂直和水平线。对于每个单元格的高度,我只使用了cellHeights.Set<Float> xCoordinates = cellMarginEvent.getxCoordinates();&nbsp; &nbsp; //Draw the column lines&nbsp; &nbsp; PdfContentByte canvas = writer.getDirectContent();&nbsp; &nbsp; for (Float x : xCoordinates) {&nbsp; &nbsp; &nbsp; &nbsp; canvas.moveTo(x, top);&nbsp; &nbsp; &nbsp; &nbsp; canvas.lineTo(x, 0 + document.bottomMargin());&nbsp; &nbsp; &nbsp; &nbsp; canvas.closePathStroke();&nbsp; &nbsp; }&nbsp; &nbsp; Set<Float> cellHeights = cellMarginEvent.cellHeights;&nbsp; &nbsp; Float cellHeight = (Float)cellHeights.toArray()[0];&nbsp; &nbsp; float currentPosition = writer.getVerticalPosition(false);&nbsp; &nbsp; //Draw the row lines&nbsp; &nbsp; while (currentPosition >= document.bottomMargin()) {&nbsp; &nbsp; &nbsp; &nbsp; canvas.moveTo(cellMarginEvent.left,currentPosition);&nbsp; &nbsp; &nbsp; &nbsp; canvas.lineTo(cellMarginEvent.right,currentPosition);&nbsp; &nbsp; &nbsp; &nbsp; canvas.closePathStroke();&nbsp; &nbsp; &nbsp; &nbsp; currentPosition -= cellHeight;&nbsp; &nbsp; }最后关闭文档:document.close()示例输出:请注意,我说这是一个不完整示例的唯一原因是因为您可能需要对top标题单元格进行一些调整,或者您可能需要自定义单元格样式(背景颜色、线条颜色等)为自己记账。我还会注意到我刚刚想到的另一个缺点 - 在标记的 PDF 的情况下,此解决方案无法添加标记的表格单元格,因此如果您有该要求,则会违反合规性。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java