用Java生成PDF,iText使用.

 
package Wills;

// 導入IO庫類
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

// 導入 POI庫類
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

// 導入 iText庫類
import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

public class PrintStudent {

 /**
  * @param args
  * @throws IOException
  * @throws DocumentException
  */

 public static void main(String[] args) throws DocumentException,
   IOException {
  // 定義頁面
  PageSize Pg = new PageSize();
  // 生成PDF文檔實例
  Document document = new Document(Pg.A4);
  // 載入中文字庫
  BaseFont bfChinese = BaseFont.createFont("STSong-Light",
    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  try {
   // 輸入文檔實例
   PdfWriter.getInstance(document,
     new FileOutputStream("Chap0101.pdf"));
   document.open();
   /*
    * 生成表頭 讀取文檔 350.XLS
    */
   WillsRecor Re = new WillsRecor("350.xls");
   // 定義第一頁的數據列表.
   ArrayList ReL;
   // 讀取一頁
   ReL = Re.Next();
   // 如果讀取列表中有數據,則生成一頁內容.
   while (!ReL.isEmpty()) {
    // 定義頁頭
    Paragraph Title = new Paragraph();
    // 設置頁面格式
    Title.setSpacingBefore(8);
    Title.setSpacingAfter(2);
    Title.setAlignment(1);
    // 定義標題
    Title.add(new Chunk("南華大學2004級學生成績登記冊",
      new com.lowagie.text.Font(bfChinese, 20,
        com.lowagie.text.Font.BOLD)));

    document.add(new Paragraph("大學體育4", new com.lowagie.text.Font(
      bfChinese, 14, com.lowagie.text.Font.BOLD)));
    // 將標題輸出到PDF文檔
    document.add(Title);

    Paragraph aa = new Paragraph("2005/2006學年第一學期",
      new com.lowagie.text.Font(bfChinese, 10,
        com.lowagie.text.Font.NORMAL));
    aa.setAlignment(1);
    document.add(aa);
    // 加入任課教師
    aa = new Paragraph(" 任課教師:", new com.lowagie.text.Font(
      bfChinese, 9, com.lowagie.text.Font.NORMAL));
    aa.setAlignment(0);
    document.add(aa);
    /*
     * 開始生成表格,首先是表頭.
     */
    Table table = new Table(15);
    // 定義表格的單元格寬度
    int WidthE[] = { 5, 3, 7, 4, 3, 3, 3, 2, 5, 3, 7, 4, 3, 3, 3 };
    // 設置表格的格式
    table.setWidth(100);
    table.setWidths(WidthE);
    table.setBorder(1);
    table.setPadding(0);
    table.setSpacing(0);
    table.setDefaultHorizontalAlignment(1);
    // 加入表頭項
    table.addCell(new Cell(new Chunk("學院",
      new com.lowagie.text.Font(bfChinese, 10,
        com.lowagie.text.Font.BOLD))));
    table.addCell(new Cell(new Chunk("專業",
      new com.lowagie.text.Font(bfChinese, 10,
        com.lowagie.text.Font.BOLD))));
    table.addCell(new Cell(new Chunk("學號",
      new com.lowagie.text.Font(bfChinese, 10,
        com.lowagie.text.Font.BOLD))));
    table.addCell(new Cell(new Chunk("姓名",
      new com.lowagie.text.Font(bfChinese, 10,
        com.lowagie.text.Font.BOLD))));
    table.addCell(new Cell(new Chunk("性別",
      new com.lowagie.text.Font(bfChinese, 10,
        com.lowagie.text.Font.BOLD))));
    table.addCell(new Cell("  "));
    table.addCell(new Cell("  "));

    // 加入中間空白列,用來將表格間隔成兩個小表格
    Cell Blank = new Cell("");
    Blank.setRowspan(1);
    Blank.setBorderWidth(0);
    table.addCell(Blank);

    table.addCell(new Cell(new Chunk("學院",
      new com.lowagie.text.Font(bfChinese, 10,
        com.lowagie.text.Font.BOLD))));
    table.addCell(new Cell(new Chunk("專業",
      new com.lowagie.text.Font(bfChinese, 10,
        com.lowagie.text.Font.BOLD))));
    table.addCell(new Cell(new Chunk("學號",
      new com.lowagie.text.Font(bfChinese, 10,
        com.lowagie.text.Font.BOLD))));
    table.addCell(new Cell(new Chunk("姓名",
      new com.lowagie.text.Font(bfChinese, 10,
        com.lowagie.text.Font.BOLD))));
    table.addCell(new Cell(new Chunk("性別",
      new com.lowagie.text.Font(bfChinese, 10,
        com.lowagie.text.Font.BOLD))));
    table.addCell(new Cell("  "));
    table.addCell(new Cell("  "));

    // table.addCell(new Cell(new Chunk("20014210101", new
    // com.lowagie.text.Font(bfChinese, 10,
    // com.lowagie.text.Font.BOLD))));
    // 加入數據到表體.
   
    for (int i = 0; i < 30; i++) {
     if (i < Re.Num) {
      Record Wills = (Record) ReL.get(i);
      table.addCell(new Cell(new Chunk(Wills.Sco,
        new com.lowagie.text.Font(bfChinese, 10,
          com.lowagie.text.Font.BOLD))));
      table.addCell(new Cell(new Chunk(Wills.SMajor
        .substring(3, 5), new com.lowagie.text.Font(
        bfChinese, 10, com.lowagie.text.Font.BOLD))));
      table.addCell(new Cell(new Chunk(Wills.SSum,
        new com.lowagie.text.Font(bfChinese, 10,
          com.lowagie.text.Font.BOLD))));
      table.addCell(new Cell(new Chunk(Wills.SName,
        new com.lowagie.text.Font(bfChinese, 10,
          com.lowagie.text.Font.BOLD))));
      table.addCell(new Cell(new Chunk("  ",
        new com.lowagie.text.Font(bfChinese, 10,
          com.lowagie.text.Font.BOLD))));
      table.addCell(new Cell("  "));
      table.addCell(new Cell("  "));
      if (i + 30 < Re.Num) {
       Wills = (Record) ReL.get(i + 30);
       table.addCell(Blank);
       table.addCell(new Cell(new Chunk(Wills.Sco,
         new com.lowagie.text.Font(bfChinese, 10,
           com.lowagie.text.Font.BOLD))));
       table.addCell(new Cell(new Chunk(Wills.SMajor
         .substring(3, 5),
         new com.lowagie.text.Font(bfChinese, 10,
           com.lowagie.text.Font.BOLD))));
       table.addCell(new Cell(new Chunk(Wills.SSum,
         new com.lowagie.text.Font(bfChinese, 10,
           com.lowagie.text.Font.BOLD))));
       table.addCell(new Cell(new Chunk(Wills.SName,
         new com.lowagie.text.Font(bfChinese, 10,
           com.lowagie.text.Font.BOLD))));
       table.addCell(new Cell(new Chunk("  ",
         new com.lowagie.text.Font(bfChinese, 10,
           com.lowagie.text.Font.BOLD))));
       table.addCell(new Cell("  "));
       table.addCell(new Cell("  "));
      } else if (i + 30 >= Re.Num) {
       Cell Blank1 = new Cell("");
       Blank1.setColspan(8);
       Blank1.setBorderWidth(0);
       table.addCell(Blank1);
      }
     }
     // 表格
    }
    // 輸出表格到PDF
    document.add(table);
    aa = new Paragraph(
      "  本冊爲學生成績登記原始記錄冊,由教研室任課教師登記一式三份,於考完後四天內送教務科"
        + "、學生所在院系各一份,教研室自留一份.不及格成績用紅筆記錄;考查課程按優(90~),良

(80~),中(70~,及"
        + "格(60~)和不及格記載;空白說明原因.",
      new com.lowagie.text.Font(bfChinese, 9,
        com.lowagie.text.Font.NORMAL));
    aa.setAlignment(0);
    aa.setIndentationRight(260);
    document.add(aa);
    // 頁尾
    // 讀取下一頁的內容
    ReL = Re.Next();
    // 添加新的一頁
    document.newPage();
   }

   // ------------------------------------------------------------------------------

   // ------------------------------------------------------------------------------
  } catch (DocumentException de) {
   System.err.println(de.getMessage());
  } catch (IOException ioe) {
   System.err.println(ioe.getMessage());
  }
  document.close();

  /*
   * try { Executable.printDocumentSilent("Chap0101.pdf"); } catch
   * (IOException ioe) { System.err.println(ioe.getMessage()); }
   */
  System.out.println("生成PDF成功!");
 }
}

class WillsRecor {
 private ArrayList List;

 public int Num = 0;

 private static int CurrentRecordNum = 1;

 private static String SNum = "";

 private static int INum = 0;

 private InputStream myxls;

 private HSSFWorkbook wb;

 private HSSFSheet sheet;

 private HSSFRow row;

 private HSSFCell cell;

 public WillsRecor(String file) throws IOException {
  myxls = new FileInputStream(file);
  wb = new HSSFWorkbook(myxls);
  sheet = wb.getSheetAt(0); // 第一個工作表

 }

 public ArrayList Next() {

  /*
   * 下面是主體了,我們要讀取 Excel 表格 第一步,將 SNum 和 INum 設爲空和 0,並將 List 設爲空;
   * 第二步,從當前記錄中取一條記錄,將其值放入 SNum 和 INum 中; 第三步,讀取記錄到 List 中.
   */
  List = new ArrayList();
  // System.out.println(CurrentRecordNum);
  Num = 0;
  while (CurrentRecordNum <= sheet.getLastRowNum()) {

   row = sheet.getRow(CurrentRecordNum);
   cell = row.getCell((short) 0);

   if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
    if (cell.getStringCellValue().equals(SNum) && Num != 0) {
     Add();
     // System.out.println(List.get(0).toString());
     /* 添加到對象 */
    } else if (Num == 0) {
     SNum = cell.getStringCellValue();
     Add();
     // System.out.print("In Cell");
    } else
     break;
   } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
    if (cell.getCellNum() == INum) {
     /* 添加到對象 */
    } else {
     INum = cell.getCellNum();
     break;
    }
   } else {
    System.out.println("空");
   }
   CurrentRecordNum++;
  }
  System.out.println(Num);
  return List;
 }

 public void Add() {
  try {
   Record aa = new Record(row.getCell((short) 0).getStringCellValue(),
     row.getCell((short) 8).getStringCellValue(), row.getCell(
       (short) 7).getStringCellValue(), row.getCell(
       (short) 1).getStringCellValue(), row.getCell(
       (short) 6).getStringCellValue(), row.getCell(
       (short) 0).getStringCellValue());
   List.add(aa);
   Num++;
  } catch (Exception e) {
   System.out.print(e.toString());
  }
 }
}

class Record {
 public String ClassN;

 public String Sco;

 public String SMajor;

 public String SSum;

 public String SName;

 public String ISex;

 public Record(String ClassN, String Sco, String SMajor, String SSum,
   String SName, String ISex) {
  this.ClassN = ClassN;
  this.Sco = Sco;
  this.SMajor = SMajor;
  this.SSum = SSum;
  this.SName = SName;

 }

 public String getSco() {
  return Sco;
 }

 public String toString() {
  return SSum;
 }

}

發佈了25 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章