国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java操作PDF報表-iText的使用

2019-11-06 07:50:14
字體:
來源:轉載
供稿:網友

iText組件

生成PDF報表的java組件–iText,通過在服務器端生成PDF報表,客戶端采用超鏈接顯示或下載得到生成的報表。

iText組件的下載

maven坐標:

<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.9</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency>

iText的基本使用

public static void main(String[] args) { //下載pdf文檔 try { //1.創建文檔 Document document = new Document(); //2.新建pdf實例 PdfWriter.getInstance(document, new FileOutputStream(new File("f:/hello.pdf"))); //3.打開文檔 document.open(); //4.文檔中寫入內容 document.add(new Paragraph("hello world")); //5.關閉文檔 document.close(); } catch (FileNotFoundException e) { e.PRintStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }

iText的進階使用

/** * 導出分區pdf * @return * @throws Exception */ @Action("subarea_exportPdf") public String exportPdf() throws Exception{ //1.從數據庫查詢查詢出來的數據列表 //獲取業務查詢條件 Specification<Subarea> specification = getSubareaspecifiaction(); //調用業務層查詢數據 List<Subarea> subareaList = subareaService.findSubareaList(specification); //放入響應下載 String downFilename = "分區數據.pdf"; //獲取文件類型 String mimeType = ServletActionContext.getServletContext().getMimeType(downFilename); //將文件類型放入響應 ServletActionContext.getResponse().setContentType(mimeType); //設置瀏覽器類型 String agent = ServletActionContext.getRequest().getHeader("user-agent"); //解決附件名編碼 downFilename = FileUtils.encodeDownloadFilename(downFilename, agent); //獲取附件名稱和下載方式 String contentDisposition = "attachment;filename="+downFilename; //將附件名稱和下載方式放入響應頭信息中 ServletActionContext.getResponse().setHeader("Content-Disposition", contentDisposition); //將文件流寫入客戶端響應中 HttpServletResponse response = ServletActionContext.getResponse(); //根據數據生成pdf try { //1.創建一個文檔 Rectangle rectangle = new Rectangle(PageSize.A4); //設置文檔四周間距 Document document = new Document(rectangle,40,40,40,40); //指定文檔的輸出位置 PdfWriter.getInstance(document, response.getOutputStream()); //3.打開文檔 document.open(); //4.準備寫入內容 //參數1:使用宋體來寫pdf文檔 //參數2:字體編碼,gbk //參數3:是否內置字體:如果不內置:優點:文件個頭小,缺點:打開文檔的os中沒有該字體,可能顯示不正常 //如果內置:優點:os不需要有該字體,也能正常顯示,缺點:文件個頭大 BaseFont baseFontChinese = BaseFont.createFont(AsianFontMapper.ChineseSimplifiedFont, AsianFontMapper.ChineseSimplifiedEncoding_H, BaseFont.NOT_EMBEDDED); //文檔標題 //參數1:相當于字體(宋體) //參數2:字體大小 //參數3:是否加粗,斜體 //參數4:字體顏色 Font headerFont = new Font(baseFontChinese,30,Font.BOLD,BaseColor.PINK); //構建一個段落 Paragraph headerParagraph = new Paragraph("分區列表",headerFont); //文字居中 headerParagraph.setAlignment(Paragraph.ALIGN_CENTER); //將添加到文檔中 document.add(headerParagraph); //文檔副標題 Font header2font = new Font(baseFontChinese,20,Font.ITALIC,BaseColor.CYAN); //構建一個段落 Paragraph header2Paragraph = new Paragraph("張三",header2font); //文字居右 header2Paragraph.setAlignment(Paragraph.ALIGN_RIGHT); //設置行間距 header2Paragraph.setLeading(29f); //將副標題加入到文檔中 document.add(header2Paragraph); //文檔正文 //設置表格頭部字體 Font tableHeaderFont = new Font(baseFontChinese,10,Font.BOLD,BaseColor.BLUE); //設置表格內容字體 Font tablebodyFont = new Font(baseFontChinese,10,Font.NORMAL,BaseColor.BLACK); //構建一個表格 PdfPTable pdfPTable = new PdfPTable(7); pdfPTable.setSpacingBefore(29f);//設置表格到上面段落的高度 pdfPTable.setLockedWidth(true); //設置表格的寬度和高度 pdfPTable.setTotalWidth(800); pdfPTable.setTotalWidth(new float[]{60,60,60,50,50,50,150}); //水平對其方式 pdfPTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); //垂直對其方式 pdfPTable.getDefaultCell().setVerticalAlignment(Element.ALIGN_CENTER); //Paragraph:段落,換行 //Phrase:一段文字,不換行 //設置表頭內容 pdfPTable.addCell(new PdfPCell(new Phrase("分區編號", tableHeaderFont))); pdfPTable.addCell(new PdfPCell(new Phrase("區域編碼", tableHeaderFont))); pdfPTable.addCell(new PdfPCell(new Phrase("關鍵字", tableHeaderFont))); pdfPTable.addCell(new PdfPCell(new Phrase("起始號",tableHeaderFont))); pdfPTable.addCell(new PdfPCell(new Phrase("結束號",tableHeaderFont))); pdfPTable.addCell(new PdfPCell(new Phrase("單雙號",tableHeaderFont))); pdfPTable.addCell(new PdfPCell(new Phrase("位置信息",tableHeaderFont))); //表格內容 PdfPCell cell = new PdfPCell(); cell.setPadding(5f);//設置padding for (Subarea subarea : subareaList) { cell.setPhrase(new Phrase(subarea.getId(),tablebodyFont)); pdfPTable.addCell(cell); cell.setPhrase(new Phrase(subarea.getRegion().getId(),tablebodyFont)); pdfPTable.addCell(cell); cell.setPhrase(new Phrase(subarea.getAddresskey(),tablebodyFont)); pdfPTable.addCell(cell); cell.setPhrase(new Phrase(subarea.getStartnum(),tablebodyFont)); pdfPTable.addCell(cell); cell.setPhrase(new Phrase(subarea.getEndnum(),tablebodyFont)); pdfPTable.addCell(cell); cell.setPhrase(new Phrase(subarea.getSingle().toString(),tablebodyFont)); pdfPTable.addCell(cell); cell.setPhrase(new Phrase(subarea.getPosition(),tablebodyFont)); pdfPTable.addCell(cell); } //段落添加到文檔中 document.add(pdfPTable); //5.釋放資源 document.close(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("PDF文件下載失敗!"); } //響應的是文件流 return NONE; }
上一篇:GC java垃圾回收機制

下一篇:java單鏈表

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 游戏| 崇明县| 莱芜市| 大洼县| 琼海市| 应城市| 永顺县| 周宁县| 沙河市| 四子王旗| 连城县| 惠州市| 南丹县| 朝阳县| 吴旗县| 全椒县| 来安县| 苗栗市| 高州市| 绵竹市| 宜兰县| 庆元县| 十堰市| 隆化县| 遂昌县| 南阳市| 龙陵县| 双辽市| 鲁山县| 平阴县| 云梦县| 黄梅县| 邵阳市| 农安县| 阳城县| 葫芦岛市| 桐庐县| 台北县| 陇西县| 门头沟区| 湘阴县|