java 如何调整 PdfPTable 的大小以适合页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14958396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to resize a PdfPTable to fit the page?
提问by Jannis Alexakis
I am generating a document, like in the following code, except ofcourse the contents of the table, which are varying. What I need to do is make sure that this table never exceeds one page in size, regardless of the amount of content in the cells. Is there a way to do it ?
我正在生成一个文档,就像下面的代码一样,当然表的内容除外,它们是变化的。我需要做的是确保该表格的大小永远不会超过一页,无论单元格中的内容量如何。有没有办法做到这一点?
import com.itextpdf.text.Phrase; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import java.awt.Desktop; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;public void createTemplate() throws DocumentException, FileNotFoundException, IOException{ String TARGET = System.getProperty("user.home")+"\temp.pdf";
Document document = new Document(PageSize.A4); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(TARGET)); document.open();PdfPTable table = new PdfPTable(7); for (int i = 0; i < 105; i++) { Phrase p = new Phrase("some text"); PdfPCell cell = new PdfPCell(); cell.addElement(p); table.addCell(cell); } table.setTotalWidth(PageSize.A4.getWidth()-10); table.setLockedWidth(true); PdfContentByte canvas = writer.getDirectContent(); PdfTemplate template = canvas.createTemplate(table.getTotalWidth(),table.getTotalHeight()); table.writeSelectedRows(0, -1, 0, PageSize.A4.getHeight(), template); Image img = Image.getInstance(template); img.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight()); img.setAbsolutePosition(0, PageSize.A4.getHeight()); document.add(img); document.close(); Desktop desktop = Desktop.getDesktop(); File file = new File(TARGET); desktop.open(file); }
Edit: @Bruno Lowagie. The hint with the template wrapped in an image sounds just right to me, and I changed the code according, but all I get now is an empty PDF. Am I doing something wrong, or is this the wrong approach alltogether?
编辑:@Bruno Lowagie。包含在图像中的模板提示对我来说听起来恰到好处,我相应地更改了代码,但现在我得到的只是一个空的 PDF。我做错了什么,还是这完全是错误的方法?
回答by Bruno Lowagie
If you want a table to fit a page, you should create the table before even thinking about page size and ask the table for its height as is done in the TableHeightexample. Note that the getTotalHeight()
method returns 0 unless you define the width of the table. This can be done like this:
如果您希望表格适合页面,您应该在考虑页面大小之前创建表格并询问表格的高度,如TableHeight示例中所做的那样。请注意,getTotalHeight()
除非您定义表格的宽度,否则该方法返回 0。这可以像这样完成:
table.setTotalWidth(width);
table.setLockedWidth(true);
Now you can create a Document
with size Rectangle(0, 0, width + margin * 2, getTotalHeight() + margin * 2)
and the table should fit the document exactly when you add it with the writeSelectedRows()
method.
现在您可以创建一个Document
大小,Rectangle(0, 0, width + margin * 2, getTotalHeight() + margin * 2)
并且当您使用该writeSelectedRows()
方法添加表格时,表格应该完全适合文档。
If you don't want a custom page size, then you need to create a PdfTemplate
with the size of the table and add the table to this template object. Then wrap the template object in an Image
and use scaleToFit()
to size the table down.
如果您不想要自定义页面大小,那么您需要创建一个PdfTemplate
具有表格大小的表格并将表格添加到此模板对象中。然后将模板对象包装在一个中Image
并用于scaleToFit()
缩小表格的大小。
public static void main(String[] args) throws DocumentException, FileNotFoundException, IOException {
String TARGET = "temp.pdf";
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(TARGET));
document.open();
PdfPTable table = new PdfPTable(7);
for (int i = 0; i < 700; i++) {
Phrase p = new Phrase("some text");
PdfPCell cell = new PdfPCell();
cell.addElement(p);
table.addCell(cell);
}
table.setTotalWidth(PageSize.A4.getWidth());
table.setLockedWidth(true);
PdfContentByte canvas = writer.getDirectContent();
PdfTemplate template = canvas.createTemplate(table.getTotalWidth(), table.getTotalHeight());
table.writeSelectedRows(0, -1, 0, table.getTotalHeight(), template);
Image img = Image.getInstance(template);
img.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight());
img.setAbsolutePosition(0, (PageSize.A4.getHeight() - table.getTotalHeight()) / 2);
document.add(img);
document.close();
}