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

用Java生成PDF,iText使用.

2006-03-22 14:32 686 查看
 
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
4000
.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;
 }

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