itext創建PDF

openkk 13年前發布 | 1K 次閱讀
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Cell;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.List;
import com.lowagie.text.ListItem;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

/**
 * @author 劉毅
 * @date 2010-2-24
 * @ClassName WriteForPDF.java
 * @Email liu_yi126@163.com
 * @param 創建PDF
 * @param
 */

/*
 com.lowagie.text.Document 是生成 PDF 的主要的類。它是需要使用的第一個類。一旦開始創建文檔,將需要一個寫入器向文檔中寫入內容。 com.lowagie.text.pdf.PdfWriter 就是一個 PDF 寫入器。下面列出了通常需要使用的類:
  com.lowagie.text.Paragraph —— 這個類表示一個縮進的段落。
  com.lowagie.text.Chapter —— 這個類表示 PDF 文檔中的章節。使用 Paragraph 作為題目并使用 int 作為章節號碼來創建它。
  com.lowagie.text.Font —— 這個類包含了全部的字體規范,例如字體、大小、樣式和顏色。各種字體都在這個類中聲明為靜態常數。
  com.lowagie.text.List —— 這個類表示一個列表,按順序包含許多 ListItems。
  com.lowagie.text.Table —— 這個類表示包含單元格的表,單元格有序地排列在矩陣中。
 */
public class WriteForPDF {
 /**
   *PDF文檔、章節、小節、字體、段落、表格、列表的使用和中文的使用
   */
  public  void writePDF(String fileName) {
   File file = new File(fileName);
   FileOutputStream out = null;
   try {
    //創建文檔,設置頁面大小,      左、右、上和下頁邊距。
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    //document是創建的文檔,out是輸出
    PdfWriter writer =PdfWriter.getInstance(document, new FileOutputStream(file));
    //設置加密的用戶密碼,所有者密碼,可復制,可打印,加密位數
    writer.setEncryption("1".getBytes(), "2".getBytes(), PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
    //打開文檔
    writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
    document.open();
    //-----------------------------------------------------------------------------------------
    //設置字體的大小,顏色,樣式,FontFactory用于指定段落的字體.
    Font font = FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255));
    Font font1 = FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255, 0, 0));
    //設置段落標題并設置字體
    Paragraph chapterTitle = new Paragraph("Chapter 1",font);
    //創建了一個章節的對象,標題為"Chapter 1"
    Chapter chapter1 = new Chapter(chapterTitle,1);
    //設置為0,則在標題前面沒有編號.反之...
    chapter1.setNumberDepth(0);
    //創建章節下的子章節.
    Paragraph sectionTitle = new Paragraph("Section 1 of Chapter 1", font1);
    //添加對象,屬于chapter1。
    Section section1 = chapter1.addSection(sectionTitle);
    //寫文本內容
    Paragraph text = new Paragraph("This is the first text in section 1 of chapter 1.");
    section1.add(text);
    text = new Paragraph("Following is a 5×5 table:");
    section1.add(text);
    //創建表格對象
    Table table = new Table(5, 5);
    //設置表格邊框顏色
    table.setBorderColor(new Color(220, 255, 100));
    //邊距
    table.setPadding(1);
    //間距
    table.setSpacing(1);
    table.setBorderWidth(1);
    //單元格對象
    Cell cell = null;
    //添加表頭信息
    for (int colNum=0; colNum<5; colNum++){
     cell = new Cell("header-" + colNum);
     cell.setHeader(true);
     table.addCell(cell);
    }
    table.endHeaders();
    //添加表的內容
    for (int rowNum=1; rowNum<5; rowNum++){
     for (int colNum=0; colNum<5; colNum++){
      cell= new Cell("value-" + rowNum + "-" + colNum);
      table.addCell(cell);
     }
    }
    //將表格對象添加到對象中
    section1.add(table);
    //添加列表
    // 列表包含一定數量的 ListItem。可以對列表進行編號,也可以不編號。
    // 將第一個參數設置為 true 表明想創建一個進行編號的列表;
    // 第二個參數設置為true表示列表采用字母進行編號,為false則用數字進行編號;
    // 第三個參數為列表內容與編號之間的距離。
    List list = new List(true, false, 20);
    ListItem item = new ListItem("First item of list;");
    list.add(item);
    item = new ListItem("Second item of list;");
    list.add(item);
    item = new ListItem("Third item of list.");
    list.add(item);
    //將列表對象添加到小節對象中
    section1.add(list);
    //simfang.ttf是仿宋的字體文件
    //中文,simfang.ttf是仿宋的字體文件
    BaseFont bfChinese = BaseFont.createFont("simfang.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    //中文大小為20,加粗
    font = new Font(bfChinese, 30, Font.BOLD);
    text = new Paragraph("PDF中文測試", font);
    section1.add(text);
    //加入到文檔中
    document.add(chapter1);
    //添加圖片
    Image backImg = Image.getInstance("src/1.jpg");
    backImg.setAbsolutePosition(3, 257);
    backImg.setAlignment(Image.LEFT);
    //圖片填充方式
    backImg.setAlignment(Image.UNDERLYING);
    document.add(backImg);
    //關閉文檔
    document.close();
    System.out.println("PDF文件生成成功,PDF文件名:" + file.getAbsolutePath());
   } catch (DocumentException e) {
    System.out.println("PDF文件"+ file.getAbsolutePath() + "生成失敗!" + e);
    e.printStackTrace();
   } catch (IOException ee) {
    System.out.println("PDF文件"+ file.getAbsolutePath() + "生成失敗!" + ee);
    ee.printStackTrace();
   } finally {
     try {
     if (out != null){
      //關閉輸出文件流
      out.close();
     }
     } catch (IOException e1) {
    }
   }
  }


  public static void main(String[] args) {
   WriteForPDF pdf = new WriteForPDF();
    String fileName = "src/tempPDF.pdf";
    pdf.writePDF(fileName);
   }
}
 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!