Java使用itext生成pdf

<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
   <groupId>com.itextpdf</groupId>
   <artifactId>itextpdf</artifactId>
   <version>5.5.11</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.itextpdf/layout -->
<dependency>
   <groupId>com.itextpdf</groupId>
   <artifactId>layout</artifactId>
   <version>7.0.4</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.itextpdf/kernel -->
<dependency>
   <groupId>com.itextpdf</groupId>
   <artifactId>kernel</artifactId>
   <version>7.0.4</version>
</dependency>

這個是我使用的itext的依賴,廢話不多說直接上代碼吧。

public static final String DEST = "d://simple_table.pdf";
    private static final int default_minimumHeight=45;
    public static void main(String[] args) throws Exception {
        ProductionManageRepairApplyPdfDTO repairApplyPdfDTO=new ProductionManageRepairApplyPdfDTO();
        String testDate="2019-01-05";
        repairApplyPdfDTO.setApplyDoneDate(testDate);
        repairApplyPdfDTO.setCallRepairDate(testDate);
        repairApplyPdfDTO.setCallRepairDepartmentName("銷售部門");
        repairApplyPdfDTO.setCallRepairEmployeeName("酒吧");
        repairApplyPdfDTO.setEstimateDoneRepairDate(testDate);
        repairApplyPdfDTO.setOrderTakerDate(testDate);
        repairApplyPdfDTO.setOrderTakerName("周杰倫");
        repairApplyPdfDTO.setRepairDepartmentName("維修部門-生產部門");
        repairApplyPdfDTO.setRepairNo("201910171110");
        repairApplyPdfDTO.setRepairContent("我的大提琴壞了 快點來給我修一修吧v");
        List<ProductionManageConsumeStuffDTO> consumeStuffDTOList=new ArrayList<>();
        ProductionManageConsumeStuffDTO consumeStuffDTO1=new ProductionManageConsumeStuffDTO();
        consumeStuffDTO1.setStuffName("錘子");
        consumeStuffDTO1.setSpecification("1/克");
        consumeStuffDTO1.setStuffAmount(20);

        ProductionManageConsumeStuffDTO consumeStuffDTO2=new ProductionManageConsumeStuffDTO();
        consumeStuffDTO2.setStuffName("飛機");
        consumeStuffDTO2.setSpecification("1/駕");
        consumeStuffDTO2.setStuffAmount(120);
        consumeStuffDTOList.add(consumeStuffDTO1);
        consumeStuffDTOList.add(consumeStuffDTO2);
        repairApplyPdfDTO.setConsumeStuffDTOList(consumeStuffDTOList);

        System.out.println(JSONObject.toJSONString(repairApplyPdfDTO));
        exportPdf(repairApplyPdfDTO);
    }

    private static void exportPdf(ProductionManageRepairApplyPdfDTO repairApplyPdfDTO) throws Exception {
        BaseFont baseFontHei = BaseFont.createFont("simhei.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
        Document document = new Document(PageSize.A4, 70, 70, 20, 100);//上下左右頁邊距
        Rectangle pageSize = document.getPageSize();

        PdfWriter.getInstance(document, new FileOutputStream("d://sample.pdf"));
        //打開文本
        document.open();

        //標題
        Paragraph paragraph = new Paragraph(80);//邊距
        //1 2 3  中右左
        paragraph.setAlignment(1);  //對齊方式
        Font font = new Font(baseFontHei);//字體
        font.setSize(14);//字體大小
        paragraph.setFont(font);//設置段落字體
        Chunk chunk = new Chunk("工程維修單");
        chunk.setLineHeight(80);
        paragraph.add(chunk);
        paragraph.setSpacingAfter(20);//往下距離200
        document.add(paragraph);

        Paragraph paragraph1 = new Paragraph(10);
        //1 2 3  中右左
        paragraph1.setAlignment(2);  //對齊方式
        Font font1 = new Font(baseFontHei);//字體
        font1.setSize(10);
        paragraph1.setFont(font1);
        Chunk chunk1 = new Chunk("編號:"+repairApplyPdfDTO.getRepairNo());
        paragraph1.add(chunk1);
//        paragraph1.setSpacingBefore(-25);
        paragraph1.setSpacingAfter(5);//往下距離200
        document.add(paragraph1);

        PdfPTable table = new PdfPTable(4);
        table.setTotalWidth(500);
        float[] columnWidth1={100,150,100,150};
        table.setTotalWidth(columnWidth1);
        table.setLockedWidth(true);//寬度算正確
        setTable(baseFontHei, table, columnWidth1,new String[]{"報修部門:",repairApplyPdfDTO.getCallRepairDepartmentName(),"保修人員:",repairApplyPdfDTO.getCallRepairEmployeeName()},default_minimumHeight);
        document.add(table);


        //畫表頭第二行
        table = new PdfPTable(4);
        table.setTotalWidth(500);
        table.setTotalWidth(columnWidth1);
        table.setLockedWidth(true);//寬度算正確
        setTable(baseFontHei, table, columnWidth1,new String[]{"報修時間:",repairApplyPdfDTO.getCallRepairDate(),"接單時間:",repairApplyPdfDTO.getOrderTakerDate()},default_minimumHeight);
        document.add(table);

        //畫表頭第三行
        table = new PdfPTable(4);
        table.setTotalWidth(500);
        table.setTotalWidth(columnWidth1);
        table.setLockedWidth(true);//寬度算正確
        setTable(baseFontHei, table, columnWidth1,new String[]{"維修部門:",repairApplyPdfDTO.getRepairDepartmentName(),"接單人員:",repairApplyPdfDTO.getOrderTakerName()},default_minimumHeight);
        document.add(table);

        //畫表頭第4行
        table = new PdfPTable(3);
        table.setTotalWidth(500);
        float[] columnWidth4={300,100,100};
        setTable(baseFontHei, table, columnWidth4,new String[]{"維修內容","申請完成時間","工程預計完成時間"},default_minimumHeight);
        document.add(table);

        //畫表頭第5行
        table = new PdfPTable(3);
        table.setTotalWidth(500);
        float[] columnWidth5={300,100,100};
        setTable(baseFontHei, table, columnWidth4,new String[]{repairApplyPdfDTO.getRepairContent(),repairApplyPdfDTO.getApplyDoneDate(),repairApplyPdfDTO.getEstimateDoneRepairDate()},default_minimumHeight);
        document.add(table);

        //畫表頭第6行
        table = new PdfPTable(1);
        table.setTotalWidth(500);
        float[] columnWidth6={500};
        setTable(baseFontHei, table, columnWidth6,new String[]{"消耗材料名稱規格數量"},18);
        document.add(table);
        List<ProductionManageConsumeStuffDTO> consumeStuffDTOList = repairApplyPdfDTO.getConsumeStuffDTOList();
        if (CollectionUtils.isNotEmpty(consumeStuffDTOList)){
            //畫表頭第5行
            table = new PdfPTable(3);
            table.setTotalWidth(500);
            float[] columnWidth={168,166,166};
            setTable(baseFontHei, table, columnWidth,new String[]{"材料名稱","規格","數量"},26);
            document.add(table);
            for (ProductionManageConsumeStuffDTO productionManageConsumeStuffDTO : consumeStuffDTOList) {
                String stuffName = productionManageConsumeStuffDTO.getStuffName();
                String specification = productionManageConsumeStuffDTO.getSpecification();
                Integer stuffAmount = productionManageConsumeStuffDTO.getStuffAmount();
                table = new PdfPTable(3);
                table.setTotalWidth(500);
                float[] s_columnWidth={168,166,166};
                setTable(baseFontHei, table, s_columnWidth,new String[]{stuffName,specification,stuffAmount+""},30);
                document.add(table);
            }
        }

        //畫表頭第7行
        table = new PdfPTable(2);
        table.setTotalWidth(500);
        float[] columnWidth7={190,310};
        setTable(baseFontHei, table, columnWidth7,new String[]{"報修部門確認:",""},default_minimumHeight);
        document.add(table);

        //畫表頭第7行
        table = new PdfPTable(2);
        table.setTotalWidth(500);
        setTable(baseFontHei, table, columnWidth7,new String[]{"維修部門意見:",""},default_minimumHeight);
        document.add(table);

        //畫表頭第7行
        table = new PdfPTable(2);
        table.setTotalWidth(500);
        setTable(baseFontHei, table, columnWidth7,new String[]{"報修部門主管領導審批:",""},default_minimumHeight);
        document.add(table);

        //畫表頭第7行
        table = new PdfPTable(2);
        table.setTotalWidth(500);
        setTable(baseFontHei, table, columnWidth7,new String[]{"維修部門主管領導審批:",""},default_minimumHeight);
        document.add(table);

        //畫表頭第7行
        table = new PdfPTable(2);
        table.setTotalWidth(500);
        float[] columnWidth8={250,250};
        setTable(baseFontHei, table, columnWidth8,new String[]{"維修人員簽字、完成日期、時間","報修部門跟進人員確認簽字"},default_minimumHeight);
        document.add(table);


        //畫表頭第7行
        table = new PdfPTable(2);
        table.setTotalWidth(500);
        float[] columnWidth9={250,250};
        setTable(baseFontHei, table, columnWidth8,new String[]{"",""},default_minimumHeight);
        document.add(table);
        document.close();
    }

    //
    private static void setTable(BaseFont baseFontHei, PdfPTable table, float[] columnWidth2,String[] cells,int minimumHeight) throws Exception {

        table.setTotalWidth(500);
        table.setTotalWidth(columnWidth2);
        table.setLockedWidth(true);//寬度算正確
        for (String cell : cells) {
            table.addCell(drawPdfPCell(cell,baseFontHei,10,1,minimumHeight));
        }
    }

    private static   PdfPCell drawPdfPCell(String cellText, BaseFont baseFont, float size, int alignment, int minimumHeight ) throws Exception{
        //爲null會報錯  防止報錯
        if(cellText==null){
            cellText=" ";
        }
        //表格開始
        Paragraph paragraph = new Paragraph();
        paragraph.setAlignment(alignment);  //對齊方式
        Font font = new Font(baseFont);//字體
        font.setSize(size);//字體大小
        paragraph.setFont(font);//設置段落字體
        Chunk chunk = new Chunk(cellText);
        paragraph.add(chunk);
        PdfPCell cell = new PdfPCell();
        cell.setUseAscender(true);
        cell.setVerticalAlignment(cell.ALIGN_MIDDLE);//設置cell垂直居中
        cell.setMinimumHeight(minimumHeight);//設置單元格最小高度,當前行最小高度
        cell.addElement(paragraph);
        return cell;
    }

最後生成的效果是:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章