java導出pdf文件(插入圖片及信息)
//需下載iText.jar及iTextAsian.jar包 //生成ean13格式或ean8格式條形碼PDF打印 public void getExportPDF(List<Object> list, OutputStream os) throws MalformedURLException, IOException, DocumentException{ try { //itext在pdf中輸入中文字體(中文楷體)時: bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); // 設置之紙張為A4紙,左右上下邊距全部為5, document = new Document(PageSize.A4, 50, 50, 50, 50); // 構造好的pdf文件輸出位置 PdfWriter pdf = PdfWriter.getInstance(document, os); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //生成PDF頁腳 HeaderFooter footer = null; HeaderFooter header = null; //頁眉信息 String headerStr = "此處為頁眉"; //生成PDF頁眉 header=new HeaderFooter(new Phrase(new Paragraph(headerStr, new com.lowagie.text.Font(bfChinese))),false); //生成PDF頁腳,第多少頁 footer=new HeaderFooter(new Phrase(" 第 ", new com.lowagie.text.Font(bfChinese)),new Phrase(" 頁", new com.lowagie.text.Font(bfChinese))); //0居左1居中2居右 header.setAlignment(0); document.setHeader(header); footer.setBorder(Rectangle.NO_BORDER); footer.setAlignment(1); document.setFooter(footer); // 打開文件 document.open(); //創建表格 PdfPTable table = new PdfPTable(2);//兩列 table.setWidthPercentage(100); //傳入的對象信息 count = list.size(); //避免因為最后一行數據由于不能滿足總列數而導致itext自動將其去掉,而生成一些多余的數 int column = 2; int persons = count; int yushu = persons % 2; int tmppersons = persons + (column - yushu); String barCodeString = ""; String barCode = ""; //對象信息輸入pdf文件 for (int i = 0; i < tmppersons; i++) { if (i < persons) { //構造個兩列一行的表格 PdfPTable mytable = new PdfPTable(2); //插入圖片 Image image = Image.getInstance("image路徑"); mytable.addCell(image); mytable.setWidthPercentage(80); String cntext = "測試"; mess = new Paragraph(cntext, new com.lowagie.text.Font(bfChinese,11)); //第二列放文字信息 mytable.addCell(mess); //在單元格中放置構造好的一個表格 table.addCell(mytable); } else { //避免因為最后一行數據由于不能滿足總列數而導致itext自動將其去掉,不知道有沒有其他方法,目前用多余的空格填充 table.addCell(" "); } } document.add(table); document.close(); }
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!