Java_iText_PDF—生成PDF工具


Maven:

  

 <!--iText(用於生成PDF)-->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.10</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
包引用:
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

  

1. 詳情見代碼:

/**
 * PDF工具
 */
public class PdfUtils {

    /**
     * 生成
     * @param iouData 借據數據
     * @param planList 還款計划List
     * @param accountsList 收本金賬戶
     * @param interstAccountsList 收息賬戶
     * @param orderCode
     * @return
     */
    public static String turnToPdf(LoanIouData iouData,List<LoanRepaymentPlan> planList, List<TransferAccount> accountsList, List<TransferAccount> interstAccountsList, String orderCode){

        String basePath = PropertiesUtil.getValue("pdf.path","/application.properties");
        File file = new File(basePath);
        if(!file.exists()){
             file.mkdirs();
        }

        String path = "";

        try {
            //創建文件
            Document document = new Document();
            //建立一個書寫器
            path = basePath+orderCode+"_還款計划表.pdf";
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));

            //打開文件
            document.open();

            //中文字體,解決中文不能顯示問題
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font font = new Font(bfChinese);//正常字體
            Font fontBold = new Font(bfChinese, 12, Font.BOLD);//正常加粗字體
            Font fontBig = new Font(bfChinese, 20);//大字體
            Font fontBigBold = new Font(bfChinese, 20, Font.BOLD);//加粗大字體

            //添加主題
            Paragraph theme = new Paragraph("還款計划表", fontBigBold);
            theme.setAlignment(Element.ALIGN_CENTER);
            document.add(theme);

            //借款信息
            Paragraph peopleInfo = new Paragraph("借款人:"+iouData.getBorrower()+"    "+"借款金額:"+iouData.getLoanAmount()+"萬元", font);
            peopleInfo.setAlignment(Element.ALIGN_CENTER);
            document.add(peopleInfo);

            //加入空行
            //Paragraph blankRow = new Paragraph(18f, " ");
            //document.add(blankRow);

            //TODO Table1 還款計划 : 3列的表.-------------------------------------
            PdfPTable table1 = new PdfPTable(3);
            table1.setWidthPercentage(90); // 寬度100%填充
            table1.setSpacingBefore(20f); // 前間距
            table1.setSpacingAfter(20f); // 后間距

            List<PdfPRow> listRow = table1.getRows();
            //設置列寬
            float[] columnWidths = {1f, 2f, 3f};
            table1.setWidths(columnWidths);

            //行1
            PdfPCell cells0[] = new PdfPCell[3];
            PdfPRow row0 = new PdfPRow(cells0);
            //單元格
            cells0[0] = new PdfPCell(new Paragraph("期數", fontBold));//單元格內容
            cells0[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
            cells0[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
            cells0[0].setFixedHeight(25f);
            cells0[0].setBackgroundColor(BaseColor.LIGHT_GRAY);
            cells0[1] = new PdfPCell(new Paragraph("日期", fontBold));
            cells0[1].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
            cells0[1].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
            cells0[1].setBackgroundColor(BaseColor.LIGHT_GRAY);
            cells0[2] = new PdfPCell(new Paragraph("金額(元)", fontBold));
            cells0[2].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
            cells0[2].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
            cells0[2].setBackgroundColor(BaseColor.LIGHT_GRAY);

            listRow.add(row0);

            //插入還款內容
            for (LoanRepaymentPlan plan : planList) {
                PdfPCell cells[] = new PdfPCell[3];
                PdfPRow row = new PdfPRow(cells);
                cells[0] = new PdfPCell(new Paragraph(String.valueOf(plan.getNum())));
                cells[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
                cells[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
                cells[0].setFixedHeight(25f);
                cells[1] = new PdfPCell(new Paragraph(String.valueOf(DateFormatUtils.format(plan.getRepaymentTime(), "yyyy/MM/dd"))));
                cells[1].setFixedHeight(25f);
                cells[1].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
                cells[1].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
                cells[2] = new PdfPCell(new Paragraph(String.valueOf(plan.getReturnAmount())));
                cells[2].setFixedHeight(25f);
                cells[2].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
                cells[2].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
                //把第單元格添加到集合(行)
                listRow.add(row);
            }


            //TODO Table2 賬戶列表 :  2列的表.------------------------------------------------------------------------

            PdfPTable table2 = new PdfPTable(2);
            table2.setWidthPercentage(90); // 寬度100%填充
            table2.setSpacingBefore(20f); // 前間距
            table2.setSpacingAfter(20f); // 后間距
            List<PdfPRow> listRow2 = table2.getRows();
            //設置列寬
            float[] columnWidths2 = {1f, 2f};
            table2.setWidths(columnWidths2);
            //TODO --第一部分
            PdfPCell cells2[] = new PdfPCell[2];
            PdfPRow row2 = new PdfPRow(cells2);
            cells2[0] = new PdfPCell(new Paragraph("還息賬號信息", fontBold));//單元格內容
            cells2[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
            cells2[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
            cells2[0].setFixedHeight(25f);
            cells2[0].setBackgroundColor(BaseColor.LIGHT_GRAY);
            cells2[0].setColspan(2);//合並單元格
            listRow2.add(row2);

            for(TransferAccount interstAccount : interstAccountsList){
                PdfPCell cells3[] = new PdfPCell[2];
                PdfPRow row3 = new PdfPRow(cells3);
                cells3[0] = new PdfPCell(new Paragraph("還息賬號", font));//單元格內容
                cells3[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
                cells3[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
                cells3[0].setFixedHeight(25f);

                cells3[1] = new PdfPCell(new Paragraph(interstAccount.getCertPan(), font));//單元格內容
                cells3[1].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
                cells3[1].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
                cells3[1].setFixedHeight(25f);

                listRow2.add(row3);

                PdfPCell cells4[] = new PdfPCell[2];
                PdfPRow row4 = new PdfPRow(cells4);
                cells4[0] = new PdfPCell(new Paragraph("還息戶名", font));//單元格內容
                cells4[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
                cells4[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
                cells4[0].setFixedHeight(25f);

                cells4[1] = new PdfPCell(new Paragraph(interstAccount.getName(), font));//單元格內容
                cells4[1].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
                cells4[1].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
                cells4[1].setFixedHeight(25f);
                listRow2.add(row4);

                PdfPCell cells5[] = new PdfPCell[2];
                PdfPRow row5 = new PdfPRow(cells5);
                cells5[0] = new PdfPCell(new Paragraph("還息開戶行", font));//單元格內容
                cells5[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
                cells5[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
                cells5[0].setFixedHeight(25f);

                cells5[1] = new PdfPCell(new Paragraph(interstAccount.getOpenBankName(), font));//單元格內容
                cells5[1].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
                cells5[1].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
                cells5[1].setFixedHeight(25f);

                listRow2.add(row5);
            }





            //TODO --第2部分
            PdfPCell cells6[] = new PdfPCell[2];
            PdfPRow row6 = new PdfPRow(cells6);
            cells6[0] = new PdfPCell(new Paragraph("還本金賬號信息", fontBold));//單元格內容
            cells6[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
            cells6[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
            cells6[0].setFixedHeight(25f);
            cells6[0].setBackgroundColor(BaseColor.LIGHT_GRAY);
            cells6[0].setColspan(2);//合並單元格
            listRow2.add(row6);

            for(TransferAccount account : accountsList){

                PdfPCell cells7[] = new PdfPCell[2];
                PdfPRow row7 = new PdfPRow(cells7);
                cells7[0] = new PdfPCell(new Paragraph("還本金賬號", font));//單元格內容
                cells7[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
                cells7[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
                cells7[0].setFixedHeight(25f);

                cells7[1] = new PdfPCell(new Paragraph(account.getCertPan(), font));//單元格內容
                cells7[1].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
                cells7[1].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
                cells7[1].setFixedHeight(25f);

                listRow2.add(row7);

                PdfPCell cells8[] = new PdfPCell[2];
                PdfPRow row8 = new PdfPRow(cells8);
                cells8[0] = new PdfPCell(new Paragraph("還本金戶名", font));//單元格內容
                cells8[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
                cells8[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
                cells8[0].setFixedHeight(25f);

                cells8[1] = new PdfPCell(new Paragraph(account.getName(), font));//單元格內容
                cells8[1].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
                cells8[1].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
                cells8[1].setFixedHeight(25f);

                listRow2.add(row8);

                PdfPCell cells9[] = new PdfPCell[2];
                PdfPRow row9 = new PdfPRow(cells9);
                cells9[0] = new PdfPCell(new Paragraph("還本金開戶行", font));//單元格內容
                cells9[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
                cells9[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
                cells9[0].setFixedHeight(25f);

                cells9[1] = new PdfPCell(new Paragraph(account.getOpenBankName(), font));//單元格內容
                cells9[1].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
                cells9[1].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
                cells9[1].setFixedHeight(25f);

                listRow2.add(row9);

            }


            //把表格添加到文件中
            document.add(table1);
            document.add(table2);

            //關閉文檔
            document.close();
            //關閉書寫器
            writer.close();
        }catch (Exception ex){
            ex.printStackTrace();
        }

        return path;
    }
}

結果:

2. 面向對象方式,適用特定形式(收據等)

  ... ...


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM