【发布时间】:2021-09-02 06:48:16
【问题描述】:
PdfPTable#getTotalHeight() 方法在写入文档之前返回 0。有没有办法在写入文档之前获取高度?
Document document = new Document();
PdfWriter instance = PdfWriter.getInstance(document, new FileOutputStream("output.pdf"));
document.open();
PdfPTable table = ...
System.out.println("table total height: " + table.getTotalHeight());
document.add(table);
System.out.println("after adding to doc");
System.out.println("table total height: " + table.getTotalHeight());
document.close();
控制台输出:
table total height: 0.0
after adding to doc
table total height: 1249.3105
【问题讨论】:
-
您需要它是否有特定原因?只需为每个高度检查创建一个新的空文档。
-
@Nexarius 出于性能原因,我想避免写入文档。
-
你能做一个可编译的可复制示例吗?我实际上已经收到了一个非常相似的任务,并通过谷歌搜索找到了这个问题(因为我实际上认为它会像你描述的那样工作)。但是,就我而言,它实际上是开箱即用的,但现在我担心
getTotalHeight可能会返回零并破坏我的逻辑。 -
@adnan_e 我刚刚回答了这个问题。