2012年6月25日 星期一

[JAVA_其它]利用itext來顯示pdf


最近作者比較有點空閒的時間,特別花了一點時間研究一下如何用java來寫入pdf,並用他來做一些基本的呈現,以及顯示中文

首先,先引入 itext-5.2.0.jar,如果你需要顯示中文化的話,需要引入itext-asian-5.2.0.jar

public class Emp {

 private String empNo;
 private String empName;
 private String birthday;
 private String year;
 private String tel;
 //以下為get,set
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class ItxtSample {

 //產生中文字型
 private static Font fontRedCN, fontBlueCN, fontBlueSmallCN, fontBlackSmallCN;
 // 產生PDF路徑
 private static final String fileName = "C:/ItextSample.pdf";
 
 private static String empTitle[] = {"員工編號", "姓名", "生日", "電話", "年齡"};

 // 產生字型,字體大小
 private static final Font smallFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);

 private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);

 private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);

 public static void main(String[] args) throws DocumentException {
  //建立假資料
  java.util.List<Emp> list = FalseInformation();

  try {
   // 產生中文字型,字體大小
   BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
   fontRedCN = new Font(bfChinese, 18, Font.BOLD, new BaseColor(255, 0, 0));
   fontBlueCN = new Font(bfChinese, 18, Font.BOLD, new BaseColor(0, 0, 255));
   fontBlueSmallCN = new Font(bfChinese, 12, Font.BOLD, new BaseColor(0, 255, 0));
   fontBlackSmallCN = new Font(bfChinese, 12, Font.BOLD, new BaseColor(0, 0, 0));

   // 產生一個A4大小的PDF檔案
   Document document = new Document(PageSize.A4);

   PdfWriter.getInstance(document, new FileOutputStream(fileName));

   document.open();

   // PDF 文件內容部分
   addMetaDataTitle(document);
   // PDF 表頭部分
   addTitlePage(document);
   // PDF 內容部分
   addContent(document, list);

   document.close();

  } catch (DocumentException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 /**
  * 假資料
  * @return
  */
 private static java.util.List<Emp> FalseInformation() {
  java.util.List<Emp> list = new ArrayList<Emp>();
  Emp emp = new Emp();
  emp.setEmpNo("000001");
  emp.setEmpName("王曉明");
  emp.setTel("031234567");
  emp.setYear("25");
  emp.setBirthday("1988/06/06");
  list.add(emp);
  Emp emp2 = new Emp();
  emp2.setEmpNo("000002");
  emp2.setEmpName("黃小強");
  emp2.setTel("037654321");
  emp2.setYear("52");
  emp2.setBirthday("1980/12/12");
  list.add(emp2);
  return list;
 }

 /**
  * PDF 內容部分
  * @param document
  * @param list
  * @throws DocumentException
  */
 private static void addContent(Document document, java.util.List<Emp> list) throws DocumentException {

  //建立標題一
  Anchor anchor = new Anchor("標題一", fontRedCN);
  anchor.setName("First Chapter");

  Chapter catPart = new Chapter(new Paragraph(anchor), 1);
  Paragraph subPara = new Paragraph("Subcategory 1", subFont);

  Section subCatPart = catPart.addSection(subPara);
  subCatPart.add(new Paragraph("Hello World"));

  Paragraph paragraph = new Paragraph();
  subCatPart.add(paragraph);

  addEmptyLine(paragraph, 10);

  subPara = new Paragraph("Subcategory 2", subFont);
  subCatPart = catPart.addSection(subPara);
  subCatPart.add(new Paragraph("Paragraph 1"));
  subCatPart.add(new Paragraph("Paragraph 2"));
  subCatPart.add(new Paragraph("Paragraph 3"));
  document.add(catPart);

  //建立標題二
  anchor = new Anchor("標題二", fontRedCN);
  anchor.setName("Second Chapter");

  catPart = new Chapter(new Paragraph(anchor), 2);
  subPara = new Paragraph("Subcategory 2", subFont);

  Section subCatPart2 = catPart.addSection(subPara);
  
  // Add a list
  createList(subCatPart2);
  // Add a table
  createTable(subCatPart2);
  // Add a table2
  createTable2(subCatPart2, list);

  document.add(catPart);

 }

 /**
  * 建立表格2
  * @param subCatPart2
  * @param list
  */
 private static void createTable2(Section subCatPart2, java.util.List<Emp> list) {
  
  PdfPTable table = new PdfPTable(5);
  PdfPCell cell = new PdfPCell(new Paragraph("員工基本資料", fontBlueCN));
  //字體內容至中
  cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  // 背景顏色
  cell.setBackgroundColor(new BaseColor(0, 255, 0));
  // 外框顏色
  cell.setBorderColor(new BaseColor(255, 0, 0));
  //合併儲存格
  cell.setColspan(5);
  
  table.addCell(cell);
  
  for(String title : empTitle ){
   PdfPCell cell1 = new PdfPCell(new Paragraph( title, fontBlueSmallCN));
   cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
   table.addCell(cell1);
  }
  
  if( list != null && !list.isEmpty() ){
   for(Emp emp : list ){
    PdfPCell cell1 = new PdfPCell(new Paragraph( emp.getEmpNo(), catFont));
    cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(cell1); 
    cell1 = new PdfPCell(new Paragraph( emp.getEmpName(), fontBlackSmallCN));
    cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(cell1);
    cell1 = new PdfPCell(new Paragraph( emp.getBirthday(), catFont));
    cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(cell1);
    cell1 = new PdfPCell(new Paragraph( emp.getTel(), catFont));
    cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(cell1);
    cell1 = new PdfPCell(new Paragraph( emp.getYear(), catFont));
    cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(cell1);
   }
  }
  subCatPart2.add(table);
 }

 /**
  * 建立表格
  * @param subCatPart
  */
 private static void createTable(Section subCatPart2) {
  PdfPTable table = new PdfPTable(3);
  //表格與上排字體間隙
  table.setSpacingBefore(5);
  //表格與下排字體間隙
  table.setSpacingAfter(5);
       
  PdfPCell c1 = new PdfPCell(new Phrase("Table Header 1"));
  //字體至中
  c1.setHorizontalAlignment(Element.ALIGN_CENTER);
  table.addCell(c1);

  c1 = new PdfPCell(new Phrase("Table Header 2"));
  c1.setHorizontalAlignment(Element.ALIGN_LEFT);
  table.addCell(c1);

  c1 = new PdfPCell(new Phrase("Table Header 3"));
  c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
  table.addCell(c1);
  table.setHeaderRows(1);

  table.addCell("1.0");
  table.addCell("1.1");
  table.addCell("1.2");
  table.addCell("2.1");
  table.addCell("2.2");
  table.addCell("2.3");

  subCatPart2.add(table);
  
 }

 /**
  * 增加list
  * @param subCatPart
  */
 private static void createList(Section subCatPart) {
  List list = new List(true, false, 10);
  //list.add(new ListItem("標題二", fontRedCN));
  list.add(new ListItem("First point"));
  list.add(new ListItem("Second point"));
  list.add(new ListItem("Third point"));
  subCatPart.add(list);
 }

 /**
  * 表頭部分
  * @param document
  * @throws DocumentException
  */
 private static void addTitlePage(Document document)throws DocumentException {

  Paragraph preface = new Paragraph("This is a paragraph", 
                  FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLDITALIC,new BaseColor(0, 0, 255)));

  //換行
  addEmptyLine(preface, 2);

  preface.add(new Paragraph("顯示中文", fontRedCN));

  addEmptyLine(preface, 2);

  preface.add(new Paragraph(addBlank(3) + "顯示中文", fontRedCN));

  addEmptyLine(preface, 8);

  preface.add(new Paragraph("This document is a preliminary version and not subject to your li cense agreement or any other agreement with vogella.com ;-).", smallFont));

  addEmptyLine(preface, 4);
  
  document.add(preface);
  
  Phrase phrase0 = new Phrase("PDF");
  Phrase phrase1 = new Phrase(new Chunk("字體顏色", fontRedCN));
  Phrase phrase2 = new Phrase(new Chunk("測試", fontBlueCN));
  document.add(phrase0);
  document.add(phrase1);
  document.add(phrase2);
  // 產生新的一頁
  document.newPage();
 }

 /**
  * 文件內容部分
  * @param document
  */
 private static void addMetaDataTitle(Document document) {

  // 增加標題
  document.addTitle("PDF表頭");
  // 增加作者
  document.addAuthor("Puma製作");
  // 增加建立PDF時間以及修改PDF日期
  document.addCreationDate();
  // 增加PDF中的關鍵字
  document.addKeywords("關鍵字");
  // 增加PDF的主題
  document.addSubject("PDF TEST");
  // 增加自訂內容
  document.addHeader("PDF1", "測試1");
  document.addHeader("PDF2", "測試2");

 }

 /**
  * 換行
  * @param paragraph
  * @param number
  */
 private static void addEmptyLine(Paragraph paragraph, int number) {
  if (number != 0) {
   for (int i = 0; i < number; i++) {
    paragraph.add(new Paragraph(" "));
   }
  }
 }

 /**
  * 增加空白
  * @param blank
  * @return
  */
 private static String addBlank(int blank) {
  StringBuilder bu = new StringBuilder();
  if( blank > 0 ){
   for (int i = 0; i <= blank; i++) {
    bu.append(" ");
   }
  }
  return bu.toString();
 }
}
結果






















其它文章

沒有留言:

張貼留言

標籤

Oracle (150) Oracle DB (144) Oracle_DB (143) Oracle SQL (135) JAVA (84) css-基本類 (65) MySQL (59) CSS Selector (58) jQuery (49) JavaScript-基本類 (39) Spring Boot (38) JavaScript (37) JavaScript HTML DOM (37) JavaScript-HTML_DOM (36) CSS3 (30) JAVA-基本類 (28) jQuery UI (27) Apache (23) Oracle GROUP BY (20) datepicker (20) Android (18) Oracle Date (17) c (17) JAVA-lang套件 (16) Linux (16) Oracle Sub Query (16) Spring-基本類 (16) jQuery-基本類 (16) MySQL-進階系列教學 (15) Android基本類 (14) Grails (14) Oracle join (14) SQLite (13) Spring (13) WIN7-基本類 (13) grails-基本類 (13) linux cent os (13) CKEditor (12) JAVA-流程控制類 (12) JAVA_Spring (12) PHP (11) Spring MVC (11) MySQL-基本系列教學 (10) Notepad (10) Notepad++ (10) SQLite for java (10) Windows (10) c/c++ (10) eclipse (9) jQuery-Selector (9) sqldeveloper (9) DB_Toad (8) JAVA_IDE_Eclipse (8) JavaScript-String類 (8) MySQL DB Toad (8) MySQL-DATE相關 (8) MySQL-函式相關 (8) Spring Bean (8) Android Studio (7) HTML5 (7) Hibernate (7) JAVA-OCWCD (7) JavaScript-陣列類 (7) Docker (6) JAVA-程式分享 (6) JAVA.util套件 (6) JavaScript-數學類 (6) MinGw (6) MySQL-其它類 (6) Servlet (6) centos (6) Apache_Tomcat (5) Apache套件_POI (5) CSS (5) JavaScript-Date物件 (5) JavaScript-其它類 (5) PostgreSQL (5) httpd (5) log4j (5) 基本資訊 (5) 開發工具 (5) CSS Properties (4) Dev-C++ (4) IntelliJ IDEA (4) Oracle DDL (4) Sublime (4) TortoiseSVN (4) apache_Maven (4) Android NDK (3) Eclipse IDE for C/C++ (3) Hibernate-基本類 (3) JAVA-問題 (3) JAVA-綀習分享 (3) JVM (3) Linux 指令 (3) Proxy Server (3) Spring Mobile (3) Spring web (3) Squid (3) VirtualBox (3) maven (3) zk (3) 生活其它 (3) Bootstrap (2) Filter (2) JAVA_IO (2) JAVA_其它_itext套件 (2) JBoss-問題 (2) JSP (2) Jboss (2) Listener (2) MySQL-語法快速查詢 (2) Spring AOP (2) Spring Batch (2) Spring Boot Actuator (2) Spring i18n (2) Subversive (2) Tomcat 8 (2) UML (2) WebJars (2) WinMerge (2) c++ (2) c語言綀習題 (2) jQuery Mobile (2) jQuery-事件處理 (2) jQuery-套件類 (2) putty (2) svn (2) weblogic (2) Apache_JMeter (1) Apache套件_BeanUtils (1) Apache套件_StringUtils (1) Base64 (1) Google API (1) HTML5-基本類 (1) Heap (1) JAVA 7 (1) JAVA SE 、JAVA EE、JAVA ME (1) JAVA 日期 (1) JAVA-OCJP (1) JAVA-WEB (1) JAVA_IDE (1) JAVA其它 (1) JBoss Server (1) JDK (1) JMX (1) JRE (1) Java RMI (1) Java String (1) Joda Time (1) Linux_其它 (1) MySQL教學 (1) Oracle_VirtualBox (1) SQL Server (1) SWT (1) Session (1) Stack (1) Struts 2 (1) Tool (1) ZK Studio (1) csv (1) grails-其它類 (1) jQuery-進階 (1) java mail (1) java web (1) java8 (1) jsoup (1) mockmvc (1) modules (1) tomcat (1) win10 (1) 其它類 (1) 圖片工具 (1) 模擬器 (1) 讀書分享 (1) 開發資訊 (1)

精選文章

初學 Java 的 HelloWorld 程式

撰寫一個JAVA程式 public class HelloWorld{ public static void main(String[ ] args){ System.out.println("我第一支Java程式!!"); } } ...