package dao.other;
import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.util.PDFTextStripper;
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.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;
public class PDFTest {
/**
* 很多應用程序要求動態生成 PDF 文檔。這類應用程序包括銀行生成用于電子郵件投遞的客戶報表,到讀者購買特定圖書章節并以 PDF
* 格式接收這些文檔。例子羅列下去是很多的。在本文中,將使用 iText Java 庫生成 PDF
* 文檔,并引導您完成一個示例應用程序,以使您能夠更好地理解和使用 iText。 iText 是 Lowagie.com 站點(請參閱
* 參考資料)免費提供的 Java 庫。iText 庫的功能很強大,支持 HTML、RTF 和 XML 文檔的生成,此外還能夠生成 PDF
* 文檔??梢詮亩喾N字體中選擇文檔中所使用的字體。同時,iText 的結構允許使用相同的代碼生成以上任意類型的文檔。
* http://www.lowagie.com/iText/ iText API:近距離觀察 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 ——
* 這個類表示包含單元格的表,單元格有序地排列在矩陣中。
*/
/**
* 寫PDF文件,展示了PDF文檔、章節、小節、字體、段落、表格、列表的使用 最后展示如何使用寫入中文。
*
* @param fileName
*/
public void writePDF(String fileName) {
File file = new File(fileName);
FileOutputStream out = null;
try {
// (1)實例化文檔對象
// 第一個參數是頁面大小。接下來的參數分別是左、右、上和下頁邊距。
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
// (2)創建寫入器
// 第一個參數是對文檔對象的引用,第二個參數是輸出的文件,將out和document連接起來
out = new FileOutputStream(file);
PdfWriter writer = PdfWriter.getInstance(document, out);
// 打開文檔準備寫入內容
document.open();
// (3)下面創建章節對象
// 首先創建段落對象,作為章節的標題。FontFactory用于指定段落的字體。
Font font = FontFactory.getFont(FontFactory.HELVETICA, 18,
Font.BOLDITALIC, new Color(0, 0, 255));
Paragraph chapter1_title = new Paragraph("Chapter 1", font);
// 創建了一個章節對象,標題為"Chapter 1"
Chapter chapter1 = new Chapter(chapter1_title, 1);
// 將編號級別設為 0 就不會在頁面上顯示章節編號
chapter1.setNumberDepth(0);
// (4)創建小節對象
// 創建小節對象的標題
font = FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD,
new Color(255, 0, 0));
Paragraph section1_title1 = new Paragraph("Section 1 of Chapter 1",
font);
// 創建一個小節對象,標題為"This is Section 1 in Chapter 1",屬于chapter1。
Section section1 = chapter1.addSection(section1_title1);
// (5)往小節中寫文本內容
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);
// (6)往小節中寫表格
// 創建表格對象
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);
// (7)添加列表
// 列表包含一定數量的 ListItem??梢詫α斜磉M行編號,也可以不編號。
// 將第一個參數設置為 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);
// (8)添加中文
// 允許在PDF中寫入中文,將字體文件放在classPath中。
// simfang.ttf是仿宋的字體文件
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// 中文大小為20,加粗
font = new Font(bfChinese, 20, Font.BOLD);
text = new Paragraph("PDF中文測試", font);
section1.add(text);
// (9)將章節對象加入到文檔中
document.add(chapter1);
// (10)關閉文檔
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 {
if (out != null) {
try {
// 關閉輸出文件流
out.close();
} catch (IOException e1) {
}
}
}
}
/**
* 讀PDF文件,使用了pdfbox開源項目,新的版本已經支持中文了。 上www.pdfbox.org下載讀PDF的jar包
*
* @param fileName
*/
public void readPDF(String fileName) {
File file = new File(fileName);
FileInputStream in = null;
try {
in = new FileInputStream(fileName);
// 新建一個PDF解析器對象
PDFParser parser = new PDFParser(in);
// 對PDF文件進行解析
parser.parse();
// 獲取解析后得到的PDF文檔對象
PDDocument pdfdocument = parser.getPDDocument();
// 新建一個PDF文本剝離器
PDFTextStripper stripper = new PDFTextStripper();
// 從PDF文檔對象中剝離文本
String result = stripper.getText(pdfdocument);
System.out.println("PDF文件" + file.getAbsolutePath() + "的文本內容如下:");
System.out.println(result);
} catch (Exception e) {
System.out.println("讀取PDF文件" + file.getAbsolutePath() + "生失?。? + e);
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
}
}
}
public static void main(String[] args) {
PDFTest pdf = new PDFTest();
String fileName = "E:/source/java/youz/dist/data/tempPDF.pdf";
pdf.writePDF(fileName);
//pdf.readPDF(fileName);
}
}
posted on 2009-06-18 12:04
楊愛友 閱讀(2792)
評論(0) 編輯 收藏 所屬分類:
java相關技術