java生成pdf表格,调用itext创建

  昨天花了很长的时间去找pdf生成表格的代码,发现网上大家写的代码太多了,而且又没有注释,让我一个小白是完全看不懂,这就很过分了,所以秉着我们代码界共享的原则,我要把我昨天的收获分享给大家,好了废话不多说,贴代码了。

1.第一步 导包

   <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.4.3</version>
        </dependency>

2.第二步看代码把




import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

public class PDFXXX {


    public static void main(String[] args) throws Exception, DocumentException {
        List<String> ponum = new ArrayList<String>();
        add(ponum, 26);
        List<String> line = new ArrayList<String>();
        add(line, 26);
        List<String> part = new ArrayList<String>();
        add(part, 26);
        List<String> description = new ArrayList<String>();
        add(description, 26);
        List<String> origin = new ArrayList<String>();
        add(origin, 26);

        //Create Document Instance
        Document document = new Document();

        //add Chinese font
        BaseFont bfChinese = BaseFont.createFont("d:\\pdf\\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

        //Font headfont=new Font(bfChinese,10,Font.BOLD);
        Font keyfont = new Font(bfChinese, 8, Font.BOLD);
        Font textfont = new Font(bfChinese, 8, Font.NORMAL);

        //Create Writer associated with document
        PdfWriter.getInstance(document, new FileOutputStream(new File("D:\\POReceiveReport.pdf")));

        document.open();

        //Seperate Page controller
        int recordPerPage = 10;
        int fullPageRequired = ponum.size() / recordPerPage;
        int remainPage = ponum.size() % recordPerPage > 1 ? 1 : 0;
        int totalPage = 1;

        for (int j = 0; j < totalPage; j++) {
            document.newPage();

            String company = "等待";
            //record header field
            PdfPTable t = new PdfPTable(5);
            float[] widths = {1.5f, 1f, 1f, 1.5f, 1f};
            t.setWidths(widths);
            t.setTotalWidth(100);
            t.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
            PdfPCell c1 = new PdfPCell(new Paragraph("PO#", keyfont));
            t.addCell(c1);
            c1 = new PdfPCell(new Paragraph("Line", keyfont));
            t.addCell(c1);
            c1 = new PdfPCell(new Paragraph("Part#", keyfont));
            t.addCell(c1);
            c1 = new PdfPCell(new Paragraph("Description", keyfont));
            t.addCell(c1);
            c1 = new PdfPCell(new Paragraph("Origin", keyfont));
            t.addCell(c1);

            //calculate the real records within a page ,to calculate the last record number of every page
            int maxRecordInPage = j + 1 == totalPage ? (remainPage == 0 ? recordPerPage : (ponum.size() % recordPerPage)) : recordPerPage;

            for (int i = j * recordPerPage; i < ((j * recordPerPage) + maxRecordInPage); i++) {
                PdfPCell c2 = new PdfPCell(new Paragraph(ponum.get(i), textfont));
                t.addCell(c2);
                c2 = new PdfPCell(new Paragraph(line.get(i), textfont));
                t.addCell(c2);
                c2 = new PdfPCell(new Paragraph(part.get(i), textfont));
                t.addCell(c2);
                c2 = new PdfPCell(new Paragraph(description.get(i), textfont));
                t.addCell(c2);
                c2 = new PdfPCell(new Paragraph(origin.get(i), textfont));
                t.addCell(c2);
            }
            document.add(t);

        }
        document.close();
    }

    public static String leftPad(String str, int i) {
        int addSpaceNo = i - str.length();
        String space = "";
        for (int k = 0; k < addSpaceNo; k++) {
            space = " " + space;
        }
        ;
        String result = space + str;
        return result;
    }

    public static String printBlank(int tmp) {
        String space = "";
        for (int m = 0; m < tmp; m++) {
            space = space + " ";
        }
        return space;
    }

    public static void add(List<String> list, int num) {
        for (int i = 0; i < num; i++) {
            list.add("test老葛-" + i);
        }
    }

}

3.注意事项:simhei.ttf 这是字体,对中文有效的;然后如果导了com.lowagie包可能在引包的时候会出现问题,所以看我代码上的import导的什么包就行了。最后,记得点赞,需要大家的鼓励,谢谢。

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java使用com.lowagie.text.pdf插件编写的PDF报表工具类,支持动态报表创建,使用简单,附件中包含了测试类和生成的报表文件。附件中的代码需要修改相关的保存路径后可以直接使用。创建一张报表例子: private JsFileExportResult createRowReport() { String condition = "开始时间:2018-02-02 14:00:30 结束时间:2018-02-06 16:00:30"; PDFGridReport pdfReport = new PDFGridReport("报表创建测试", GridReportTestModel.getModels()); pdfReport.addCondition(condition); pdfReport.header("字段名称1", "item1", 0, 0).width(160); pdfReport.header("字段名称3", "item3", 0, 2).getCell().backgroundColor(Color.ORANGE); pdfReport.header("字段名称4", "item4", 0, 3); pdfReport.header("字段名称5", "item5", 0, 4); pdfReport.header("字段名称2", "item2", 0, 1); pdfReport.header("值", "value", 0, 5).alignH(HAlign.ALIGN_CENTER).getCell().alignH(HAlign.ALIGN_RIGHT); pdfReport.header("时间", "time", 0, 6); pdfReport.header("图片", "image", 0, 7).width(60).alignH(HAlign.ALIGN_CENTER).getCell() .alignH(HAlign.ALIGN_CENTER).alignV(VAlign.ALIGN_MIDDLE); // 横向打印 pdfReport.getPageSetting().setPrintHorizontal(); pdfReport.group("item1").childGroup("item2"); pdfReport.setCellFormat(new PDFCellValueFormat() { @Override public String format(String fieldName, String oriValue) { if ("value".equals(fieldName)) { return String.format("%.2f", Double.parseDouble(oriValue)).toString(); } else { return oriValue; } } }); JsFileExportResult fileResult = pdfReport.createReport(); return fileResult; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值