如何调整PdfPTable的大小以适应页面? [英] How to resize a PdfPTable to fit the page?

查看:3530
本文介绍了如何调整PdfPTable的大小以适应页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在生成一个文档,如下面的代码所示,除了表格的内容,这些内容各不相同。我需要做的是确保此表的大小不会超过一页,无论单元格中的内容量如何。有办法吗?

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;

文档文档=新文档(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(TARGET));
document.open();

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();

编辑:@Bruno Lowagie 。包含在图像中的模板提示听起来恰到好处,我改变了代码,但我现在得到的只是一个空PDF。我做错了什么,或者这是一个错误的方法吗?

@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?

推荐答案

如果你想要一个适合页面的表,你应该在考虑页面大小之前创建表格并在表格中查询其高度,如 TableHeight 例子。请注意,除非您定义表的宽度,否则 getTotalHeight()方法将返回0。这可以这样做:

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 TableHeight example. Note that the getTotalHeight() method returns 0 unless you define the width of the table. This can be done like this:

    table.setTotalWidth(width);
    table.setLockedWidth(true);

现在你可以用尺寸创建一个文件 矩形(0,0,宽度+边距* 2,getTotalHeight()+边距* 2),当您使用<$ c添加文档时,表格应完全符合文档$ c> writeSelectedRows()方法。

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.

如果您不想要自定义页面大小,则需要创建 PdfTemplate 使用表的大小并将表添加到此模板对象。然后将模板对象包装在 Image 中,并使用 scaleToFit()来缩小表的大小。

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.

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();
}

这篇关于如何调整PdfPTable的大小以适应页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆