1.Introduce maveny dependencies
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency>
2. Implement the code
// pdf file saving path String fileName = voSubLedger.getTargetPath() + "/" + beginDate + "-" + endDate + categoryName + "detailed account" + ".pdf"; //Create a document instance, set the specification to A4 paper, and set the margins Document document = new Document(PageSize.A4, 10, 10, 36, 36); try {<!-- --> PdfWriter.getInstance(document, new FileOutputStream(fileName)); document.open(); //Add Chinese font BaseFont bfChines = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); //Set font style Font FontChinese = new Font(bfChines, 16, Font.NORMAL); Font FontData = new Font(bfChines, 12, Font.UNDEFINED); Font FontTable = new Font(bfChines, 10, Font.NORMAL); Font FontTableHeader = new Font(bfChines, 10, Font.NORMAL, CMYKColor.RED); //Set pdf title Paragraph title = new Paragraph(categoryName + "Detailed Account", FontChinese); title.setAlignment(Element.ALIGN_CENTER); document.add(title); //Set header-time PdfPTable tableHeader = new PdfPTable(1); PdfPCell cell = new PdfPCell(new Paragraph("Time: " + beginDate + "-" + endDate, FontData)); cell.setBorder(Rectangle.NO_BORDER); tableHeader.addCell(cell); //Set header-account cell = new PdfPCell(new Paragraph("Subject: " + categoryName + " " + voSubLedger.getSubjects(), FontData)); cell.setBorder(Rectangle.NO_BORDER); tableHeader.addCell(cell); //Set the distance above the table tableHeader.setSpacingBefore(30f); //Set the column width as a percentage float[] widths = {<!-- -->1f}; tableHeader.setWidths(widths); tableHeader.setSpacingBefore(15f); document.add(tableHeader); //Create the main table, 9 columns PdfPTable table = new PdfPTable(9); cell = new PdfPCell(new Paragraph("Year", FontTableHeader)); cell.setHorizontalAlignment(Element.ALIGN_CENTER);//Centered display table.addCell(cell); cell = new PdfPCell(new Paragraph("month", FontTableHeader)); cell.setHorizontalAlignment(Element.ALIGN_CENTER);//Centered display table.addCell(cell); cell = new PdfPCell(new Paragraph("日", FontTableHeader)); cell.setHorizontalAlignment(Element.ALIGN_CENTER);//Centered display table.addCell(cell); cell = new PdfPCell(new Paragraph("Voucher Number", FontTableHeader)); cell.setHorizontalAlignment(Element.ALIGN_CENTER);//Centered display table.addCell(cell); cell = new PdfPCell(new Paragraph("Abstract", FontTableHeader)); cell.setHorizontalAlignment(Element.ALIGN_CENTER);//Centered display table.addCell(cell); cell = new PdfPCell(new Paragraph("Debit", FontTableHeader)); cell.setHorizontalAlignment(Element.ALIGN_CENTER);//Centered display table.addCell(cell); cell = new PdfPCell(new Paragraph("Lender", FontTableHeader)); cell.setHorizontalAlignment(Element.ALIGN_CENTER);//Centered display table.addCell(cell); cell = new PdfPCell(new Paragraph("Borrow/Loan", FontTableHeader)); cell.setHorizontalAlignment(Element.ALIGN_CENTER);//Centered display table.addCell(cell); cell = new PdfPCell(new Paragraph("Balance", FontTableHeader)); cell.setHorizontalAlignment(Element.ALIGN_CENTER);//Centered display table.addCell(cell); // Fixed row height cell.setFixedHeight(20f); // List data filling for (BizLedgerPdfVO bizLedger : bizLedgerVOList ) {<!-- --> cell = new PdfPCell(new Paragraph(bizLedger.getYear(), FontTable)); //Year cell.setHorizontalAlignment(Element.ALIGN_CENTER);// Horizontally centered cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // Vertically centered table.addCell(cell); cell = new PdfPCell(new Paragraph(bizLedger.getMonth(), FontTable));//Month cell.setHorizontalAlignment(Element.ALIGN_CENTER);// Horizontally centered cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // Vertically centered table.addCell(cell); cell = new PdfPCell(new Paragraph(bizLedger.getDay(), FontTable));//Day cell.setHorizontalAlignment(Element.ALIGN_CENTER);// Horizontally centered cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // Vertically centered table.addCell(cell); cell = new PdfPCell(new Paragraph(bizLedger.getVoucherNo(), FontTable));//Voucher number cell.setHorizontalAlignment(Element.ALIGN_CENTER);//Horizontal centering cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // Vertically centered table.addCell(cell); cell = new PdfPCell(new Paragraph(bizLedger.getAbstracts(), FontTable));//Abstract cell.setHorizontalAlignment(Element.ALIGN_CENTER);//Horizontal centering cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // Vertically centered table.addCell(cell); cell = new PdfPCell(new Paragraph(bizLedger.getDebitAmount(), FontTable));//Debit amount cell.setHorizontalAlignment(Element.ALIGN_CENTER);//Horizontal centering cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // Vertically centered table.addCell(cell); cell = new PdfPCell(new Paragraph(bizLedger.getCreditAmount(), FontTable));//Credit amount cell.setHorizontalAlignment(Element.ALIGN_CENTER);//Horizontal centering cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // Vertically centered table.addCell(cell); cell = new PdfPCell(new Paragraph(bizLedger.getBalanceDirection(), FontTable));//Direction cell.setHorizontalAlignment(Element.ALIGN_CENTER);//Horizontal centering cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // Vertically centered table.addCell(cell); cell = new PdfPCell(new Paragraph(bizLedger.getBalance(), FontTable));//Balance cell.setHorizontalAlignment(Element.ALIGN_CENTER);// Horizontally centered cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // Vertically centered table.addCell(cell); // Set header for each page table.setHeaderRows(1); } //Column width ratio setting float[] width = {<!-- -->0.1f, 0.1f, 0.1f, 0.1f, 0.2f, 0.1f, 0.1f, 0.1f, 0.1f}; table.setWidths(width); table.setSpacingBefore(15f); document.add(table); //Close document document.close(); } catch (Exception e) {<!-- --> e.printStackTrace(); }