How to increase the width of PdfPTable in itext Pdf

扶醉桌前 提交于 2020-01-05 02:03:25

问题


I am trying to export records from database into pdf using itext pdf library in java..But i am getting following problems in alignment of pdf table inside pdf file..

1.Table is not showing in the full pdf page .It is leaving spaces from left and right of the pdf page.

2.Every page is showing values in half of the page only .Means pdf table is showing in half of the pdf pages..

Here is my code..

Document document = new Document();
        PdfWriter.getInstance(document, fos);


        PdfPTable table = new PdfPTable(10);

        table.setWidthPercentage(100);
        table.setSpacingBefore(0f);
        table.setSpacingAfter(0f);



        PdfPCell cell = new PdfPCell(new Paragraph("DateRange"));

        cell.setColspan(10);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setPadding(5.0f);
        cell.setBackgroundColor(new BaseColor(140, 221, 8));

        table.addCell(cell);

        table.addCell("Calldate");
        table.addCell("Calltime");
        table.addCell("Source");
        table.addCell("DialedNo");
        table.addCell("Extension");
        table.addCell("Trunk");
        table.addCell("Duration");
        table.addCell("Calltype");
        table.addCell("Callcost");
        table.addCell("Site");

        while (rs.next()) {
            table.addCell(rs.getString("date"));
            table.addCell(rs.getString("time"));
            table.addCell(rs.getString("source"));
            table.addCell(rs.getString("destination"));
            table.addCell(rs.getString("extension"));
            table.addCell(rs.getString("trunk"));
            table.addCell(rs.getString("dur"));
            table.addCell(rs.getString("toc"));
            table.addCell(rs.getString("callcost"));
            table.addCell(rs.getString("Site"));
        }




        table.setSpacingBefore(5.0f);       // Space Before table starts, like margin-top in CSS
        table.setSpacingAfter(5.0f);        // Space After table starts, like margin-Bottom in CSS                                          

        document.open();//PDF document opened........                   

        document.add(Chunk.NEWLINE);   //Something like in HTML :-)

        document.add(new Paragraph("TechsoftTechnologies.com"));
        document.add(new Paragraph("Document Generated On - " + new Date().toString()));

        document.add(table);
        document.add(Chunk.NEWLINE);   //Something like in HTML :-)           
        document.newPage();            //Opened new page
                   //In the new page we are going to add list

        document.close();

        fos.close();

回答1:


I had to read your question multiple times before I understood that you wanted to suppress the margins. I copy/pasted (and adapted) your example, and I thought I couldn't reproduce your problem because I (and many other developers) am used to the fact that pages have margins.

Anyway, this is my version of your example: FullPageTable and it creates the following PDF: full_page_table.pdf

I've made some minor changes, for instance: it doesn't make sense to pass a ´Paragraph´ as a parameter for a PdfPCell because that Paragraph will be treated as a Phrase, but I think the line you're looking for is:

Document document = new Document(PageSize.A4, 0, 0, 0, 0);

The zeros define the width of the margin, and if I understand your question correctly, you want to suppress those margins.

As for your allegation Every page is showing values in half of the page only .Means pdf table is showing in half of the pdf pages, I think you're causing that yourself by introducing document.newPage(); otherwise your allegation doesn't make sense ;-)



来源:https://stackoverflow.com/questions/19873263/how-to-increase-the-width-of-pdfptable-in-itext-pdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!