1、前言java
首先須要在Maven中添加相應的jar包依賴,若項目沒用到Maven,也可自行下載相應所需的jar包(itextpdf.jar 與 itext-asian.jar),以下圖所示。點此下載api
Maven中添加依賴jar包以下所示:服務器
1 <!-- pdf start --> 2 <dependency> 3 <groupId>com.itextpdf</groupId> 4 <artifactId>itextpdf</artifactId> 5 <version>5.3.2</version> 6 </dependency> 7 <dependency> 8 <groupId>com.itextpdf</groupId> 9 <artifactId>itext-asian</artifactId> 10 <version>5.2.0</version> 11 </dependency> 12 <!-- pdf end -->
2、正文app
一、首先獲取須要填寫至PDF中的數據,以下述所示,realPath爲添加圖片的路徑,test.pdf爲未添加圖片的PDF臨時文件,在Tomcat服務器對應路徑下,以及導出的PDF文件存至桌面:函數
1 //點擊下載獲取企業信息並存至Pdf 2 @RequestMapping(value="/downloadPdf.do") 3 @ResponseBody 4 public String downloadPdf(@RequestParam(value = "download_corp_id") String download_corp_id, HttpServletRequest request) throws Exception { 5 List<TCorp> corpIfs = searchCorpService.getCorpInfo(Integer.parseInt(download_corp_id)); 6 TCorp tCorp = new TCorp(); 7 for (TCorp corpInfo: corpIfs){ 8 tCorp.setId(corpInfo.getId()); 9 tCorp.setRegNo(corpInfo.getRegNo()); 10 tCorp.setCorpName(corpInfo.getCorpName()); 11 tCorp.setCorpAddr(corpInfo.getCorpAddr()); 12 tCorp.setBelongOrg(corpInfo.getBelongOrg()); 13 tCorp.setBelongDistOrg(corpInfo.getBelongDistOrg()); 14 tCorp.setBelongTrade(corpInfo.getBelongTrade()); 15 tCorp.setEconKind(corpInfo.getEconKind()); 16 tCorp.setAdmitMain(corpInfo.getAdmitMain()); 17 tCorp.setStartDate(corpInfo.getStartDate()); 18 tCorp.setCheckDate(corpInfo.getCheckDate()); 19 tCorp.setOperManIdentNo(corpInfo.getOperManIdentNo()); 20 tCorp.setOperManName(corpInfo.getOperManName()); 21 tCorp.setCorpStatus(corpInfo.getCorpStatus()); 22 tCorp.setRegCapi(corpInfo.getRegCapi()); 23 tCorp.setPaidCapi(corpInfo.getPaidCapi()); 24 tCorp.setFareTermStart(corpInfo.getFareTermStart()); 25 tCorp.setFareTermEnd(corpInfo.getFareTermEnd()); 26 tCorp.setFareScope(corpInfo.getFareScope()); 27 tCorp.setUniScid(corpInfo.getUniScid()); 28 tCorp.setCorpTel(corpInfo.getCorpTel()); 29 tCorp.setCorpWebUrl(corpInfo.getCorpWebUrl()); 30 tCorp.setCorpLogo(corpInfo.getCorpLogo()); 31 tCorp.setCorpEmail(corpInfo.getCorpEmail()); 32 tCorp.setPracPersonNum(corpInfo.getPracPersonNum()); 33 tCorp.setOrgInstCode(corpInfo.getOrgInstCode()); 34 tCorp.setTaxpayNum(corpInfo.getTaxpayNum()); 35 tCorp.setStaffSize(corpInfo.getStaffSize()); 36 tCorp.setEnglishName(corpInfo.getEnglishName()); 37 tCorp.setFormerName(corpInfo.getFormerName()); 38 tCorp.setCorpInfo(corpInfo.getCorpInfo()); 39 tCorp.setCreateDate(corpInfo.getCreateDate()); 40 tCorp.setCreateOrg(corpInfo.getCreateOrg()); 41 } 42 43 String realPath = request.getSession().getServletContext().getRealPath("/") + "\\icon\\logo_64.png"; 44 PDFReport.settCorp(tCorp); 45 new PDFReport("test.pdf").generatePDF(); 46 PDFUtil.addImage("test.pdf", "C:\\Users\\Administrator\\Desktop\\"+tCorp.getCorpName()+".pdf",realPath); 47 48 return "提示:數據導出成功!"; 49 }
二、PDFReport類中申明一個文檔類型創建Document對象,設置頁面樣式等:學習
1 package util; 2 3 import com.itextpdf.text.Document; 4 import com.itextpdf.text.PageSize; 5 import com.itextpdf.text.Rectangle; 6 import com.itextpdf.text.pdf.PdfPTable; 7 import com.itextpdf.text.pdf.PdfWriter; 8 import entity.TCorp; 9 10 import java.io.File; 11 import java.io.FileOutputStream; 12 13 public class PDFReport { 14 private static TCorp tCorp; 15 16 Document document = new Document();// 創建一個Document對象 17 18 public PDFReport(String out) { 19 try { 20 File file = new File(out); 21 file.createNewFile(); 22 Rectangle pageSize = new Rectangle(PageSize.A4); 23 document.setPageSize(pageSize); 24 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); 25 PDFBuilder builder = new PDFBuilder(); 26 writer.setPageEvent(builder); 27 document.open(); 28 PdfPTable table = generatePDF(); 29 document.add(table); 30 document.close(); 31 } catch (Exception e) { 32 e.printStackTrace(); 33 } 34 } 35 36 public static void settCorp(TCorp tCorp) { 37 PDFReport.tCorp = tCorp; 38 } 39 40 41 public PdfPTable generatePDF() { 42 //設置單元格爲5列 43 PdfPTable table = PDFUtil.createTable(5); 44 45 table.addCell(PDFUtil.createHeadCell("企業信息列表")); 46 table.addCell(PDFUtil.createTitleCell_1("企業名稱")); 47 table.addCell(PDFUtil.createCell_1(tCorp.getCorpName())); 48 table.addCell(PDFUtil.createTitleCell_1("聯繫方式")); 49 table.addCell(PDFUtil.createCell_1(tCorp.getCorpTel())); 50 table.addCell(PDFUtil.createCell_2("Logo")); 51 52 table.addCell(PDFUtil.createTitleCell_1("企業郵箱")); 53 table.addCell(PDFUtil.createCell_1(tCorp.getCorpEmail())); 54 table.addCell(PDFUtil.createTitleCell_1("網址")); 55 table.addCell(PDFUtil.createCell_1(tCorp.getCorpWebUrl())); 56 57 table.addCell(PDFUtil.createTitleCell_1("企業地址")); 58 table.addCell(PDFUtil.createCell_1(tCorp.getCorpAddr())); 59 table.addCell(PDFUtil.createTitleCell_1("註冊/實繳")); 60 table.addCell(PDFUtil.createCell_1(String.valueOf(tCorp.getRegCapi())+"萬 / "+String.valueOf(tCorp.getPaidCapi())+"萬")); 61 62 table.addCell(PDFUtil.createTitleCell_1("成立日期")); 63 table.addCell(PDFUtil.createCell_1(tCorp.getStartDate())); 64 table.addCell(PDFUtil.createTitleCell_1("統一社會信用代碼")); 65 table.addCell(PDFUtil.createCell_3(tCorp.getUniScid())); 66 67 table.addCell(PDFUtil.createTitleCell_1("法定表明人")); 68 table.addCell(PDFUtil.createCell_1(tCorp.getOperManName())); 69 table.addCell(PDFUtil.createTitleCell_1("納稅人識別號")); 70 table.addCell(PDFUtil.createCell_3(tCorp.getTaxpayNum())); 71 72 table.addCell(PDFUtil.createTitleCell_1("註冊號")); 73 table.addCell(PDFUtil.createCell_1(tCorp.getRegNo())); 74 table.addCell(PDFUtil.createTitleCell_1("組織機構代碼")); 75 table.addCell(PDFUtil.createCell_3(tCorp.getOrgInstCode())); 76 77 table.addCell(PDFUtil.createTitleCell_1("公司類型")); 78 table.addCell(PDFUtil.createCell_1(tCorp.getEconKind())); 79 table.addCell(PDFUtil.createTitleCell_1("人員規模")); 80 table.addCell(PDFUtil.createCell_3(tCorp.getStaffSize())); 81 82 table.addCell(PDFUtil.createTitleCell_1("營業期限")); 83 table.addCell(PDFUtil.createCell_1(tCorp.getFareTermStart()+" 至 "+tCorp.getFareTermEnd())); 84 table.addCell(PDFUtil.createTitleCell_1("登記機關")); 85 table.addCell(PDFUtil.createCell_3(tCorp.getBelongOrg())); 86 87 table.addCell(PDFUtil.createTitleCell_1("覈准日期")); 88 table.addCell(PDFUtil.createCell_1(tCorp.getCheckDate())); 89 table.addCell(PDFUtil.createTitleCell_1("所屬行業")); 90 table.addCell(PDFUtil.createCell_3(tCorp.getBelongTrade())); 91 92 table.addCell(PDFUtil.createTitleCell_1("英文名稱")); 93 table.addCell(PDFUtil.createCell_1(tCorp.getEnglishName())); 94 table.addCell(PDFUtil.createTitleCell_1("曾用名")); 95 table.addCell(PDFUtil.createCell_3(tCorp.getFormerName())); 96 97 table.addCell(PDFUtil.createTitleCell_2("經營範圍")); 98 table.addCell(PDFUtil.createCell_4(tCorp.getFareScope())); 99 100 return table; 101 } 102 }
三、PDFUtil類中設置字體、表格樣式、以及水印文字樣式,setColspan函數爲設置所跨列數,setRowspan函數爲設置所跨行數:測試
1 package util; 2 3 import com.itextpdf.text.*; 4 import com.itextpdf.text.pdf.*; 5 6 import javax.imageio.ImageIO; 7 import java.io.BufferedOutputStream; 8 import java.io.File; 9 import java.io.FileOutputStream; 10 11 public class PDFUtil { 12 private static Font headfont ; // 設置字體大小 13 private static Font keyfont; // 設置字體大小 14 private static Font textfont; // 設置字體大小 15 16 static{ 17 BaseFont bfChinese; 18 try { 19 bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); 20 headfont = new Font(bfChinese, 24, Font.BOLD,BaseColor.BLACK);// 設置字體大小 21 keyfont = new Font(bfChinese, 12, Font.BOLD,BaseColor.BLACK);// 設置字體大小 22 textfont = new Font(bfChinese, 10, Font.NORMAL,BaseColor.BLACK);// 設置字體大小 23 } catch (Exception e) { 24 e.printStackTrace(); 25 } 26 } 27 28 //表格標題 29 public static PdfPCell createHeadCell(String value){ 30 PdfPCell cell = new PdfPCell(); 31 cell.setVerticalAlignment(15); 32 cell.setHorizontalAlignment(15); 33 cell.setColspan(5); 34 cell.setPhrase(new Phrase(value,headfont)); 35 cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中 36 cell.setPadding(10.0f); 37 cell.setBorder(0); 38 cell.setPaddingTop(5.0f); 39 cell.setPaddingBottom(18.0f); 40 return cell; 41 } 42 43 //表格表頭樣式1 44 public static PdfPCell createTitleCell_1(String value){ 45 PdfPCell cell = new PdfPCell(); 46 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 47 cell.setHorizontalAlignment(Element.ALIGN_CENTER); 48 cell.setPhrase(new Phrase(value, keyfont)); 49 cell.setBackgroundColor(new BaseColor(29, 181, 238)); 50 cell.setColspan(1); 51 cell.setFixedHeight(35); 52 return cell; 53 } 54 55 //表格表頭樣式2 56 public static PdfPCell createTitleCell_2(String value){ 57 PdfPCell cell = new PdfPCell(); 58 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 59 cell.setHorizontalAlignment(Element.ALIGN_CENTER); 60 cell.setPhrase(new Phrase(value, keyfont)); 61 cell.setBackgroundColor(new BaseColor(29, 181, 238)); 62 cell.setColspan(1); 63 cell.setRowspan(3); 64 cell.setFixedHeight(105); 65 return cell; 66 } 67 68 //表格內容樣式1 69 public static PdfPCell createCell_1(String value){ 70 PdfPCell cell = new PdfPCell(); 71 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 72 cell.setHorizontalAlignment(Element.ALIGN_CENTER); 73 cell.setPhrase(new Phrase(value,textfont)); 74 cell.setBackgroundColor(new BaseColor(255, 255, 255)); 75 cell.setColspan(1); 76 cell.setFixedHeight(35); 77 return cell; 78 } 79 80 //表格內容樣式2 81 public static PdfPCell createCell_2(String value){ 82 PdfPCell cell = new PdfPCell(); 83 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 84 cell.setHorizontalAlignment(Element.ALIGN_CENTER); 85 cell.setPhrase(new Phrase(value,textfont)); 86 cell.setBackgroundColor(new BaseColor(255, 255, 255)); 87 cell.setColspan(1); 88 cell.setRowspan(3); 89 cell.setFixedHeight(105); 90 return cell; 91 } 92 93 //表格內容樣式3 94 public static PdfPCell createCell_3(String value){ 95 PdfPCell cell = new PdfPCell(); 96 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 97 cell.setHorizontalAlignment(Element.ALIGN_CENTER); 98 cell.setPhrase(new Phrase(value,textfont)); 99 cell.setBackgroundColor(new BaseColor(255, 255, 255)); 100 cell.setColspan(2); 101 cell.setFixedHeight(35); 102 return cell; 103 } 104 105 //表格內容樣式4 106 public static PdfPCell createCell_4(String value){ 107 PdfPCell cell = new PdfPCell(); 108 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 109 cell.setHorizontalAlignment(Element.ALIGN_CENTER); 110 cell.setPhrase(new Phrase(value,textfont)); 111 cell.setBackgroundColor(new BaseColor(255, 255, 255)); 112 cell.setColspan(4); 113 cell.setRowspan(3); 114 cell.setFixedHeight(105); 115 return cell; 116 } 117 118 //生成表格 119 public static PdfPTable createTable(int colNumber){ 120 int widths[] = { 35,40,35,35,30 }; 121 PdfPTable baseTable = new PdfPTable(colNumber); 122 baseTable.setWidthPercentage(100); 123 baseTable.setSpacingBefore(10); 124 try { 125 baseTable.setWidths(widths); 126 } catch (DocumentException e) { 127 e.printStackTrace(); 128 } 129 return baseTable; 130 } 131 132 133 public static void addImage(String input,String output,String realPath) throws Exception{ 134 BufferedOutputStream out = new BufferedOutputStream( 135 new FileOutputStream(new File(output))); 136 PdfReader reader = new PdfReader(input); 137 PdfStamper stamper = new PdfStamper(reader, out); 138 addWatermark(stamper,"測試添加水印文字"); 139 int total = reader.getNumberOfPages(); 140 try { 141 Image image = Image.getInstance(realPath); 142 image.setAbsolutePosition(350, 200); 143 image.scaleToFit(160, 70); 144 PdfContentByte content= stamper.getOverContent(total);// 在內容上方加水印 145 content.addImage(image); 146 }catch (Exception e){ 147 e.printStackTrace(); 148 } 149 150 stamper.close(); 151 reader.close(); 152 } 153 154 public static void addWatermark(PdfStamper pdfStamper, String waterMarkName) throws Exception { 155 PdfContentByte content; 156 BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", 157 BaseFont.NOT_EMBEDDED); 158 Rectangle pageRect; 159 PdfGState gs = new PdfGState(); 160 try { 161 if (base == null || pdfStamper == null) { 162 return; 163 } 164 // 設置透明度爲0.4 165 gs.setFillOpacity(0.3f); 166 gs.setStrokeOpacity(0.3f); 167 int toPage = pdfStamper.getReader().getNumberOfPages(); 168 for (int i = 1; i <= toPage; i++) { 169 pageRect = pdfStamper.getReader().getPageSizeWithRotation(i); 170 // 計算水印X,Y座標 171 float x = pageRect.getWidth() / 2; 172 float y = pageRect.getHeight() / 2; 173 // 得到PDF最頂層 174 content = pdfStamper.getOverContent(i); 175 content.saveState(); 176 // set Transparency 177 content.setGState(gs); 178 content.beginText(); 179 content.setColorFill(BaseColor.GRAY); 180 content.setFontAndSize(base, 30); 181 // 水印文字成45度角傾斜 182 content.showTextAligned(Element.ALIGN_CENTER, waterMarkName, x, 183 y, 45); 184 content.endText(); 185 } 186 } catch (Exception ex) { 187 ex.printStackTrace(); 188 } 189 } 190 }
四、PDFBuilder類中爲設置頁面附加屬性:字體
1 package util; 2 3 import com.itextpdf.text.*; 4 import com.itextpdf.text.pdf.*; 5 6 import java.io.IOException; 7 8 public class PDFBuilder extends PdfPageEventHelper { 9 /** 10 * 頁眉 11 */ 12 public String header = ""; 13 14 /** 15 * 文檔字體大小,頁腳頁眉最好和文本大小一致 16 */ 17 public int presentFontSize = 12; 18 19 20 // 模板 21 public PdfTemplate total; 22 23 // 基礎字體對象 24 public BaseFont bf = null; 25 26 // 利用基礎字體生成的字體對象,通常用於生成中文文字 27 public Font fontDetail = null; 28 29 /** 30 * 31 * Creates a new instance of PdfReportM1HeaderFooter 無參構造方法. 32 * 33 */ 34 public PDFBuilder() { 35 36 } 37 38 public void setHeader(String header) { 39 this.header = header; 40 } 41 42 /** 43 * 44 * TODO 文檔打開時建立模板 45 * 46 * @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument(com.itextpdf.text.pdf.PdfWriter, 47 * com.itextpdf.text.Document) 48 */ 49 public void onOpenDocument(PdfWriter writer, Document document) { 50 total = writer.getDirectContent().createTemplate(50, 50);// 共 頁 的矩形的長寬高 51 } 52 53 /** 54 * 55 * TODO 關閉每頁的時候,寫入頁眉,寫入'第幾頁共'這幾個字。 56 * 57 * @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(com.itextpdf.text.pdf.PdfWriter, 58 * com.itextpdf.text.Document) 59 */ 60 public void onEndPage(PdfWriter writer, Document document) { 61 this.addPage(writer, document); 62 } 63 64 //加分頁 65 public void addPage(PdfWriter writer, Document document){ 66 try { 67 if (bf == null) { 68 bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false); 69 } 70 if (fontDetail == null) { 71 fontDetail = new Font(bf, presentFontSize, Font.NORMAL);// 數據體字體 72 } 73 } catch (DocumentException e) { 74 e.printStackTrace(); 75 } catch (IOException e) { 76 e.printStackTrace(); 77 } 78 79 // 1.寫入頁眉 80 ColumnText.showTextAligned(writer.getDirectContent(), 81 Element.ALIGN_LEFT, new Phrase(header, fontDetail), 82 document.left(), document.top() + 20, 0); 83 // 2.寫入前半部分的 第 X頁/共 84 int pageS = writer.getPageNumber(); 85 String foot1 = "第 " + pageS + " 頁 / 共"; 86 Phrase footer = new Phrase(foot1, fontDetail); 87 88 // 3.計算前半部分的foot1的長度,後面好定位最後一部分的'Y頁'這倆字的x軸座標,字體長度也要計算進去 = len 89 float len = bf.getWidthPoint(foot1, presentFontSize); 90 91 // 4.拿到當前的PdfContentByte 92 PdfContentByte cb = writer.getDirectContent(); 93 94 // 5.寫入頁腳1,x軸就是(右margin+左margin + right() -left()- len)/2.0F 95 // 再給偏移20F適合人類視覺感覺,不然肉眼看上去就太偏左了 96 // ,y軸就是底邊界-20,不然就貼邊重疊到數據體裏了就不是頁腳了;注意Y軸是從下往上累加的,最上方的Top值是大於Bottom好幾百開外的。 97 ColumnText 98 .showTextAligned( 99 cb, 100 Element.ALIGN_CENTER, 101 footer, 102 (document.rightMargin() + document.right() 103 + document.leftMargin() - document.left() - len) / 2.0F + 20F, 104 document.bottom() - 20, 0); 105 106 // 6.寫入頁腳2的模板(就是頁腳的Y頁這倆字)添加到文檔中,計算模板的和Y軸,X=(右邊界-左邊界 - 前半部分的len值)/2.0F + 107 // len , y 軸和以前的保持一致,底邊界-20 108 cb.addTemplate(total, (document.rightMargin() + document.right() 109 + document.leftMargin() - document.left()) / 2.0F + 20F, 110 document.bottom() - 20); // 調節模版顯示的位置 111 112 } 113 114 115 /** 116 * 117 * TODO 關閉文檔時,替換模板,完成整個頁眉頁腳組件 118 * 119 * @see com.itextpdf.text.pdf.PdfPageEventHelper#onCloseDocument(com.itextpdf.text.pdf.PdfWriter, 120 * com.itextpdf.text.Document) 121 */ 122 public void onCloseDocument(PdfWriter writer, Document document) { 123 // 7.最後一步,是關閉文檔的時候,將模板替換成實際的 Y 值,至此,page x of y 製做完畢,完美兼容各類文檔size。 124 total.beginText(); 125 total.setFontAndSize(bf, presentFontSize);// 生成的模版的字體、顏色 126 String foot2 = " " + (writer.getPageNumber()-1) + " 頁"; 127 total.showText(foot2);// 模版顯示的內容 128 total.endText(); 129 total.closePath(); 130 } 131 }
最後附上生成的PDF效果圖:ui
本文學習參考了:https://blog.csdn.net/qq_30490591/article/details/53434777this
至此是關於JavaWeb項目生成PDF文件添加水印圖片並導出,僅供參考。
若有疏漏錯誤之處,還請不吝賜教!