如何在itext Pdf中增加PdfPTable的宽度 [英] How to increase the width of PdfPTable in itext Pdf

查看:2482
本文介绍了如何在itext Pdf中增加PdfPTable的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用java中的itext pdf库将数据库中的记录导出为pdf。但是我在pdf文件中对齐pdf表时出现以下问题..

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没有在完整的pdf页面中显示。它在pdf页面的左侧和右侧留下空格。

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

2.每页都显示值只有一半的页面.Means pdf表格显示了一半的pdf页面..

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

这是我的代码..

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


推荐答案

在我理解之前,我必须多次阅读你的问题你想抑制边距。我复制/粘贴(并改编)你的例子,我认为我无法重现你的问题,因为我(以及许多其他开发人员)习惯了页面有边距这一事实。

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.

无论如何,这是我的示例版本: FullPageTable 并创建以下PDF: full_page_table.pdf

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

我做了一些小改动,例如:传递一个'Paragraph'作为参数是没有意义的 PdfPCell 因为段落将被视为词组,但我认为您正在寻找的行是:

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.

至于你的指控每页只显示一半页面的值.Means pdf表显示在一半的pdf页面,我认为你是通过引入 document.newPage(); 自己引起的,否则你的指控没有意义; - )

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

这篇关于如何在itext Pdf中增加PdfPTable的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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