开发者

iText: Table(com.​lowagie.​text) created only occupies 80% of the entire page , How to make it utilise the entire page

开发者 https://www.devze.com 2023-02-15 18:45 出处:网络
I am writing the following code in order to create a PDF file with a table in it. Document document = new Document(PageSize.A4, 20, 20, 20, 80);

I am writing the following code in order to create a PDF file with a table in it.

        Document document = new Document(PageSize.A4, 20, 20, 20, 80);
        Font myfont = new Font(FontFactory.getFont(FontFactory.COURIER_BOLD, 13, Font.NORMAL));
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(request.getRealPath("/") + "SAMPLE.pdf"));

        document.open();
        Table table = new Table(2);
        Cell c2 = new Cell();

        int[] widths = {8, 150}; //Tried different values, but no change
        table.setBorder(Rectangle.BOX);
        table.setAlignment(Element.ALIGN_LEFT);
        table.setSpacing(0);
        table.setPadding(0);
        table.setTableFitsPage(true); //Tried with 'false', even removed it, but no change
        table.setWidths(widths);

        c2 = new Cell(new Paragraph("1.     ", myfont));
        c2.setBorder(Rectangle.NO_BORDER);
        table.addCell(c2);
        c2 = new Cell(new Paragraph("TEST DATA     ", myfont));
        c2.setBorder(Rectangle.NO_BORDER);
        table.addCell(c2);

        c2 = new Cell(new Paragraph("2.     ", myfont));
        c2.setBorder(Rectangle.NO_BORDER);
        table.addCell(c2);
        c2 = new Cell(new Paragraph("TEST DATA", myfont));
        c2.setBorder(Rectangle.NO_BORDER);
        table.addCell(c2);

        c2 = new Cell(new Paragraph("3.     ", myfont));
        c2.setBorder(Rectangle.NO_BORDER);
        table.addCell(c2);
        c2 = new Cell(new Paragraph("TEST DATA", myfont));
        c2.setBorder(Rectangle.NO_BOR开发者_Python百科DER);
        table.addCell(c2);

        document.add(table);
        document.close();

But the file created contains a table that occupies around 80-85% of the page. I want it to utilise the entire page.

I tried making some adjustments to the code like changing the table.setTableFitsPage(true); to table.setTableFitsPage(false); and even tried removing it. also altered around with the widths assigned. But in vain as in all cases it only gave me the file with a table occupying only 80-85% of page.

Is there something I am missing to add to my code or is there an attribute that is stopping the Table from taking up 100% of page.

It creates problem when the content is large as I end up getting long tables with spaces on the page still unoccupied.

Attached a screen-shot of the actual PDF file generated here!

Screen-shot of the PDF generated


You should rewrite your table code to use PdfPTable instead. You can find some examples of its use online. The entire 4th chapter of iText in Action 2nd ed is about tables, PdfPTables to be precise.

Lots of example code. Enjoy.


Use the PdfPTable class instead, and you can set the column widths on the entire table. Making it span the entire page can be done by constructing a

float delta = document.getPageSize().getWidth() / numberOfColumns;
// add delta numberOfColumn times
PdfPTable table = new PdfPTable(new float[] {delta, delta, delta, delta });
table.setWidthPercentage(100f);

The widths passed in the constructor determine how much space a given column gets relative to the width of the entire table.

0

精彩评论

暂无评论...
验证码 换一张
取 消