利用iText相關jar包進行pdf繪製
阿新 • • 發佈:2018-11-08
工作中有需求需要將回單進行繪製生成pdf以便列印,所以記錄下來
準備工作:
相關jar包:
示例:
iText-2.1.7.jar //核心jar
字型包:
iTextAsian.jar
iTextAsianCmaps.jar
ps:不同版本的iText的功能有所差異
Java類示例:
package com.test; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.Image; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfWriter; public class TestCreatePdf { public static void main(String[] args) throws Exception { try { String FileName = "C:\\Users\\Administrator.SC-201606041031\\Desktop\\test.pdf"; Map map = getMap(); File srcFile = new File(FileName); byte[] fileByte = new byte[1024]; FileName = srcFile.getName(); BufferedInputStream bufferedInputStream; bufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile)); ByteArrayOutputStream out = new ByteArrayOutputStream(); while (bufferedInputStream.read(fileByte) != -1) { out.write(fileByte); } BaseFont bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);//建立字型 Font font = new Font(bf,9,Font.BOLD);//使用字型 Font font1 = new Font(bf,8);//使用字型 Font font2 = new Font(bf,12,Font.BOLD);//使用字型 Document document = new Document(); PdfWriter.getInstance(document, out); try { document.open(); Image image = Image.getInstance("C:\\Users\\Administrator.SC-201606041031\\Desktop\\101.png"); //獲取印章圖片路徑 Image image1 = Image.getInstance("C:\\Users\\Administrator.SC-201606041031\\Desktop\\101.png"); image.setAlignment(Image.ALIGN_LEFT); image.scaleAbsolute(200f, 30f); image1.setAlignment(Image.ALIGN_LEFT); image1.scaleAbsolute(75, 55); float[] widths = {15f,25f,15f ,25f,15f,25f};// 設定表格的列寬和列數 預設是6列 float[] midWidths = {15f,30f,15f,30f}; PdfPTable table = new PdfPTable(widths); table.setSpacingBefore(20f); table.setWidthPercentage(100); float[] widths1 = {10f,10f,40f ,10f,10f,40f}; PdfPTable table1 = new PdfPTable(widths1); table1.setSpacingBefore(20f); table1.setWidthPercentage(100);// 設定表格寬度為100% // 第一行 // 第一行 PdfPCell cell = null; cell = new PdfPCell(image);// cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); //單元垂直對齊方式 cell.setBorderWidth(0); cell.setPaddingBottom(15); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Paragraph("電子回單",font2)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_LEFT); cell.setBorderWidth(0); cell.setPaddingTop(15); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Paragraph("業務型別",font)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph(map.get("TransChannel").toString(),font1)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph("記賬日期",font)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph(map.get("TransDate").toString(),font1)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph("回單編號",font)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(cell); cell = new PdfPCell(new Paragraph(map.get("TransJnlNo").toString(),font1)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(cell); PdfPTable midTable = new PdfPTable(midWidths); //第二行 cell = new PdfPCell(new Paragraph("代發工資名稱",font));// cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); cell = new PdfPCell(new Paragraph("付款賬號",font)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); //第三行 cell = new PdfPCell(new Paragraph("總筆數",font));// cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); cell = new PdfPCell(new Paragraph("總金額",font)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); //第四行 cell = new PdfPCell(new Paragraph("總手續費",font));// cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); cell = new PdfPCell(new Paragraph("交易日期",font)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); //第五行 cell = new PdfPCell(new Paragraph("成功筆數",font));// cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); cell = new PdfPCell(new Paragraph("成功金額",font)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); //第六行 cell = new PdfPCell(new Paragraph("失敗筆數",font));// cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); cell = new PdfPCell(new Paragraph("失敗金額",font)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); //第7行 cell = new PdfPCell(new Paragraph("批次狀態",font));// cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); cell = new PdfPCell(new Paragraph("授權狀態",font)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); midTable.addCell(cell); cell = new PdfPCell(new Paragraph(map.get("TransName").toString(),font1)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setColspan(2); midTable.addCell(cell); cell = new PdfPCell(midTable); cell.setColspan(6); table.addCell(cell); cell = new PdfPCell(new Paragraph("附加資訊及用途 :",font)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setBorderWidthBottom(0); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Paragraph("銀行蓋章:",font)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setBorderWidthBottom(0); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Paragraph(map.get("Remark").toString(),font1)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setBorderWidthTop(0); cell.setColspan(3); table.addCell(cell); //處理圖章顯示 float[] widths2 = {25f,15f,25f}; PdfPTable sealTable = new PdfPTable(widths2); cell = new PdfPCell(image1); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_RIGHT); cell.setBorderWidth(0); cell.setPaddingRight(5); cell.setPaddingBottom(5); cell.setColspan(3); sealTable.addCell(cell); cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_RIGHT); cell.setBorderWidthTop(0); cell.setColspan(3); sealTable.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.addElement(sealTable); table.addCell(cell); //第九行 cell = new PdfPCell(new Paragraph("重要提示:本回單不作為收付款方交易確認的最終依據。",font)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setColspan(6); cell.setBorderWidth(0); table.addCell(cell); cell = new PdfPCell(); cell.setColspan(6); cell.setPaddingBottom(15); cell.setBorderWidth(0); table.addCell(cell); float[] lastWidths = {15f,25f,15f ,25f,15f,25f};// 設定表格的列寬和列數 預設是6列 PdfPTable lastTable = new PdfPTable(lastWidths); table.setSpacingBefore(20f); table.setWidthPercentage(100); for(int i=0;i<10;i++){ cell = new PdfPCell(new Paragraph("名字a",font)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); lastTable.addCell(cell); cell = new PdfPCell(new Paragraph(map.get("TransChannel").toString(),font1)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); lastTable.addCell(cell); cell = new PdfPCell(new Paragraph("名字b",font)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); lastTable.addCell(cell); cell = new PdfPCell(new Paragraph(map.get("TransDate").toString(),font1)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); lastTable.addCell(cell); cell = new PdfPCell(new Paragraph("名字c",font)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); lastTable.addCell(cell); cell = new PdfPCell(new Paragraph(map.get("TransJnlNo").toString(),font1)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_CENTER); lastTable.addCell(cell); } document.add(table); document.add(lastTable); document.close(); if(bufferedInputStream!=null) bufferedInputStream.close(); } catch (Exception e) { System.out.println("file create exception" + e); } String resultPath="C:\\Users\\Administrator.SC-201606041031\\Desktop\\result1.pdf"; OutputStream os = new FileOutputStream(resultPath); os.write(out.toByteArray()); os.flush(); os.close(); out.close(); } catch (IOException e) { } catch (DocumentException e1) { e1.printStackTrace(); } } public static Map getMap(){ Map map = new HashMap<>(); map.put("DCFlag","C"); map.put("DCFlagKey","test"); map.put("TransName","test"); map.put("TransDate","test"); map.put("TransJnlNo","test"); map.put("JnlOtherAcName","test"); map.put("JnlAcName","test"); map.put("JnlOtherAcNo","test"); map.put("JnlAcNo","test"); map.put("Currency","test"); map.put("OtherBankName","test"); map.put("Amount","test"); map.put("AmountFmt","test"); map.put("TransChannel","test"); map.put("Remark","test"); map.put("TransDeptName","test"); map.put("TransDeptId","test"); return map; } }