您的位置:首页 > 编程语言 > Java开发

关于Java Web 使用 iText 将数据库中的 表 的数据 生成 PDF 格式文件(升級版本)

2017-03-21 22:19 816 查看
<------重點强調一下 要導入兩個 iText 的包,分別是:itext-asian    +    itext-hyph-xml------>

// 导出PDF

 public void exportPDF() throws DocumentException, IOException {

  // new出一个document对象

  Document document = new Document();

  // 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。

  // 创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。

  PdfWriter pdfwriter = PdfWriter.getInstance(document, new FileOutputStream("F:/图书信息.pdf"));

  // 5列的表

  PdfPTable pdftable = new PdfPTable(5);

  float[] widths = { 0.50f, 0.50f, 0.50f, 0.50f, 0.50f };

  pdftable.setWidths(widths);

  

  // 中文處理

  BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.EMBEDDED);   

  Font fontChinese = new Font(bfChinese,10,Font.NORMAL);

  // 設置表標題

  Phrase phrase = new Phrase("圖書詳情信息", fontChinese);

  PdfPCell pdfcells = new PdfPCell(phrase);

  pdfcells.setColspan(5);

  pdfcells.setHorizontalAlignment(Element.ALIGN_CENTER);

  pdftable.addCell(pdfcells);

  // 設置表頭

  Phrase phrase2 = new Phrase("編號", fontChinese);

  Phrase phrase3 = new Phrase("書名", fontChinese);

  Phrase phrase4 = new Phrase("作者", fontChinese);

  Phrase phrase5 = new Phrase("價格", fontChinese);

  Phrase phrase6 = new Phrase("總數", fontChinese);

  PdfPCell pdfPCell2 = new PdfPCell(phrase2);

  pdfPCell2.setHorizontalAlignment(Element.ALIGN_CENTER);

  PdfPCell pdfPCell3 = new PdfPCell(phrase3);

  pdfPCell3.setHorizontalAlignment(Element.ALIGN_CENTER);

  PdfPCell pdfPCell4 = new PdfPCell(phrase4);

  pdfPCell4.setHorizontalAlignment(Element.ALIGN_CENTER);

  PdfPCell pdfPCell5 = new PdfPCell(phrase5);

  pdfPCell5.setHorizontalAlignment(Element.ALIGN_CENTER);

  PdfPCell pdfPCell6 = new PdfPCell(phrase6);

  pdfPCell6.setHorizontalAlignment(Element.ALIGN_CENTER);

  pdftable.addCell(pdfPCell2);

  pdftable.addCell(pdfPCell3);

  pdftable.addCell(pdfPCell4);

  pdftable.addCell(pdfPCell5);

  pdftable.addCell(pdfPCell6);

  // 添加數據

  List<ThefuzzyBook> listT = iThefuzzyBookService.findAll();

  for (ThefuzzyBook thefuzzyBook2 : listT) {

   Phrase phrase7_1 = new Paragraph(thefuzzyBook2.getBookNumber()+"",fontChinese);

   Phrase phrase7_2 = new Paragraph(thefuzzyBook2.getBookName(), fontChinese);

   Phrase phrase7_3 = new Paragraph(thefuzzyBook2.getBookAuthor(), fontChinese);

   Phrase phrase7_4 = new Paragraph((float) thefuzzyBook2.getBookPrice()+"",fontChinese);

   Phrase phrase7_5 = new Paragraph(thefuzzyBook2.getBookCount()+"",fontChinese);

   PdfPCell pdfPCell7_1 = new PdfPCell(phrase7_1);

   pdfPCell7_1.setHorizontalAlignment(Element.ALIGN_CENTER);

   PdfPCell pdfPCell7_2 = new PdfPCell(phrase7_2);

   pdfPCell7_2.setHorizontalAlignment(Element.ALIGN_CENTER);

   PdfPCell pdfPCell7_3 = new PdfPCell(phrase7_3);

   pdfPCell7_3.setHorizontalAlignment(Element.ALIGN_CENTER);

   PdfPCell pdfPCell7_4 = new PdfPCell(phrase7_4);

   pdfPCell7_4.setHorizontalAlignment(Element.ALIGN_CENTER);

   PdfPCell pdfPCell7_5 = new PdfPCell(phrase7_5);

   pdfPCell7_5.setHorizontalAlignment(Element.ALIGN_CENTER);

   pdftable.addCell(pdfPCell7_1);

   pdftable.addCell(pdfPCell7_2);

   pdftable.addCell(pdfPCell7_3);

   pdftable.addCell(pdfPCell7_4);

   pdftable.addCell(pdfPCell7_5);

  }

  document.open();// 打开文档

  document.add(pdftable);// 把表格添加到文件中

  

  document.close();// 关闭文档流

  pdfwriter.close();// 关闭书写器流

  System.out.println("已成功导出PDF格式文件!,请注意查看!");

 }

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息