How do I resize a PdfPTable to fit the page?

I create a document as in the following code, except of course the contents of the table are changing. 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 this?

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 right to me and I changed the code to match, but all I get now is an empty PDF. Am I doing something wrong, or is this the whole wrong approach?

+3


source to share


1 answer


If you want the table to fit the page, you must create the table even after thinking about the page size and ask the table for its height, as in the TableHeight example. Note that the method getTotalHeight()

returns 0 if you do not specify the width of the table. This can be done as follows:

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

      

Now you can create Document

with size Rectangle(0, 0, width + margin * 2, getTotalHeight() + margin * 2)

and the table should fit the document when you add it using the method writeSelectedRows()

.



If you don't need the custom page size, you need to create PdfTemplate

with the table size and add the table to this template object. Then wrap the template object in Image

and use scaleToFit()

to reduce the size of the table.

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

      

+8


source







All Articles