Export Pdf File sử dụng iText thư viện trong JAVA

Hôm nay mình sẽ hướng dẫn các bạn làm việc với Pdf file sử dụng thư viện itext và itextAsia(Hỗ trợ tiếng Nhật).
Các bạn lên google search itext.jar, itextAsia.jar để download nhé.
Trong mã nguồn mở thì có rất nhiều thư viện có thể hỗ trợ công việc này nhưng mình thấy sử dụng thư việc trên rất dễ dàng.
Đề bài đưa ra là export 1 file pdf có format như dưới đây:
Và đây là cách giải quyết đề bài này.Các bạn tham khảo nhé.
Document document = new Document();
  String file = "c:/Export.pdf";
  try {   
   PdfWriter.getInstance(document, new FileOutputStream(file));
   document.setPageSize(PageSize.A4);
   document.setMargins(57, 57, 57, 57);
   document.setMarginMirroringTopBottom(true);
   document.open();
   // Khởi tạo các thuộc tính font cho document
   BaseFont bf = BaseFont.createFont("HeiseiMin-W3",
     "UniJIS-UCS2-HW-H", false);
   Font font = new Font(bf, 10);
   font.setStyle(1);
   BaseFont bf1 = BaseFont.createFont("HeiseiMin-W3",
     "UniJIS-UCS2-HW-H", false);
   Font font1 = new Font(bf1, 8);
   Font font2 = new Font(bf1, 8);
   font2.setStyle(1);   
   // Khởi tạo table Top có 3 cột: 日付/Date 部署/Dept. 氏名/Name 
   PdfPTable tableTop = new PdfPTable(3);
   tableTop.setWidthPercentage(100);
   // Khởi tạo cell 日付/Date
   PdfPCell dataCell = new PdfPCell(new Phrase("日付" + "/" + "Date ", font));
   dataCell.setHorizontalAlignment(Element.ALIGN_LEFT);
   dataCell.setBackgroundColor(new BaseColor(221, 216, 210));
   tableTop.addCell(dataCell);
   // Khởi tạo cell 部署/Dept. 
   dataCell = new PdfPCell(new Phrase("部署" + "/" + "Dept.", font));
   dataCell.setHorizontalAlignment(Element.ALIGN_LEFT);
   dataCell.setBackgroundColor(new BaseColor(221, 216, 210));
   tableTop.addCell(dataCell);
   // Khởi tạo cell 氏名/Name
   dataCell = new PdfPCell(new Phrase("氏名" + "/" + "Name", font));
   dataCell.setHorizontalAlignment(Element.ALIGN_LEFT);
   dataCell.setBackgroundColor(new BaseColor(221, 216, 210));
   tableTop.addCell(dataCell);
   // Khởi tạo cell chứa content của cell 日付/Date 
   dataCell = new PdfPCell(new Phrase("2015年01月06日", font1));
   dataCell.setHorizontalAlignment(Element.ALIGN_LEFT);
   tableTop.addCell(dataCell);
   // Khởi tạo cell chứa content của cell 部署/Dept. 
   dataCell = new PdfPCell(new Phrase("Admin",
     font1));
   dataCell.setHorizontalAlignment(Element.ALIGN_LEFT);
   tableTop.addCell(dataCell);
   // Khởi tạo cell chứa content của cell 氏名/Name 
   dataCell = new PdfPCell(new Phrase("カスタマサポート", font1));
   dataCell.setHorizontalAlignment(Element.ALIGN_LEFT);
   tableTop.addCell(dataCell);
   // Khởi tạo table có 1 cell: 特記事項 /Comment in particular
   PdfPTable tableCommnet = new PdfPTable(1);
   tableCommnet.setWidthPercentage(100);
   // Khởi tạo cell 特記事項 /Comment in particular
   PdfPCell commentCell = new PdfPCell(new Phrase("特記事項" + "/"
       + "Comment in particular", font));
   commentCell.setHorizontalAlignment(Element.ALIGN_CENTER);
   commentCell.setBackgroundColor(new BaseColor(221, 216, 210));
   tableCommnet.addCell(commentCell);
   // Khởi tạo cell chứa content của 特記事項 /Comment in particular
   commentCell = new PdfPCell(new Phrase("TTM test 1", font1));
//   commentCell.setFixedHeight(150);
   tableCommnet.addCell(commentCell);
   // Gán table Top vào document
   document.add(tableTop);
   // Gán tableCommnet Top vào document
   document.add(tableCommnet);
   // Khởi tạo table content có 3 cell:時間/Time,訪問先/Visited Company, 事項(記事・状況)/Details
   PdfPTable table = new PdfPTable(3);
   table.setWidthPercentage(100);
   table.setWidths(new int[]{8, 28, 64});
   // Khởi tạo cell 時間/Time
   PdfPCell c1 = new PdfPCell(new Phrase("時間" + "\n"
     + "Time", font2));
   c1.setBackgroundColor(new BaseColor(221, 216, 210));
   c1.setHorizontalAlignment(Element.ALIGN_LEFT);
   table.addCell(c1);
   // Khởi tạo cell 訪問先/Visited Company
   c1 = new PdfPCell(new Phrase("訪問先" + "\n"
     + "Visited Company", font2));
   c1.setBackgroundColor(new BaseColor(221, 216, 210));
   c1.setHorizontalAlignment(Element.ALIGN_LEFT);
   table.addCell(c1);
   // Khởi tạo cell 事項(記事・状況)/Details
   c1 = new PdfPCell(new Phrase("事項(記事・状況)" + "\n"
     + "Details", font2));
   c1.setBackgroundColor(new BaseColor(221, 216, 210));
   c1.setHorizontalAlignment(Element.ALIGN_LEFT);
   table.addCell(c1);
   table.setHeaderRows(1);
   // Ở đoạn content này nếu có nhiều dữ liệu ta có thể đưa vào vòng for
   // Khởi cell chứa content của 時間/Time
   c1 = new PdfPCell(new Phrase("00:30", font1));
   c1.setHorizontalAlignment(Element.ALIGN_LEFT);
   table.addCell(c1);
   c1 = new PdfPCell(new Phrase("TTm test", font1));
   c1.setHorizontalAlignment(Element.ALIGN_LEFT);
   table.addCell(c1);
   c1 = new PdfPCell(new Phrase("TTm test", font1));
   c1.setHorizontalAlignment(Element.ALIGN_LEFT);
   table.addCell(c1);
   // Gán table vào document
   document.add(table);    
   // Tạo dòng chữ 作成日
   Paragraph para1 = new Paragraph(new Phrase("作成日:"+"2015年01月06日", font));
   // Tạo dòng chữ 更新日
   Paragraph para2 = new Paragraph(new Phrase("更新日:"+"2015年01月06日", font));
   document.add(para1);
   document.add(para2);
   
  } catch (Exception e) {

   System.out.println("ReportServlet::generatePDF::Exception "
     + e.getMessage());
  }
  document.close();
Chúc các bạn thành công.
Previous
Next Post »