openPdf 不能设置setTotalHeight
时间: 2023-12-01 17:02:28 浏览: 28
非常抱歉,我的回答有误。在 OpenPDF 中,确实没有提供 `setTotalHeight` 方法用于设置表格的总高度,我向您道歉。
如果您需要控制每页表格的行数,可以使用 `PdfPTable` 的 `splitLate` 方法来实现。该方法会将表格拆分为多个部分,并返回最后一个部分,您可以在每页结束时调用该方法来获取当前页的表格,并在新页面上继续渲染。
以下是一个示例代码,其中 `rowHeight` 为每行的高度,`numRowsPerPage` 为每页的行数:
```
// 创建表格
PdfPTable table = new PdfPTable(numColumns);
// 设置行高
table.setTotalWidth(PageSize.A4.getWidth());
table.setLockedWidth(true);
float rowHeight = 20f;
table.getDefaultCell().setMinimumHeight(rowHeight);
// 添加数据到表格...
// 渲染表格
PdfContentByte canvas = writer.getDirectContent();
table.writeSelectedRows(0, -1, document.leftMargin(), document.top() - table.getTotalHeight(), canvas);
// 拆分表格并继续渲染
while (!table.isComplete()) {
PdfPTable overflowTable = table.splitLate(numRowsPerPage);
overflowTable.writeSelectedRows(0, -1, document.leftMargin(), document.top(), canvas);
document.newPage();
}
```
希望这个解决方案可以帮助到您。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)