最近項(xiàng)目中有個(gè)需求需要將數(shù)據(jù)庫中的數(shù)據(jù)導(dǎo)出到PDF文件中,所以在網(wǎng)上查找了相關(guān)的開源框架——pdfbox&itext
于是乎寫了一個(gè)簡單的工具類,如有需要的可以直接拿去用,切勿跟俺客氣~。
本工具類所用到的相關(guān)jar包及版本有:
1.pdfbox-1.5.0.jar
2.fontbox-1.5.0.jar
3.jempbox-1.5.0.jar
4.iText-5.0.6.jar;
5.SIMHEI.TTF()——黑體常規(guī)字體,可以去C:\\windows\\Fonts目錄下去找,并放在工程的src目錄下面~
寫小入門的帖子我喜歡開門見山,直接上代碼,其中的注釋已經(jīng)非常清楚,main方法中有使用實(shí)例,代碼結(jié)構(gòu)講太多了顯得自己很啰嗦,或者覺得自己不太善于講。沒有直接上代碼來得爽快!有喜歡的拿回去自己好好研究一下,運(yùn)行之前記得先將之上列出的jar補(bǔ)齊了就OK~。
- package com.test.common.util;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
-
- import org.apache.pdfbox.pdfparser.PDFParser;
- import org.apache.pdfbox.pdmodel.PDDocument;
- import org.apache.pdfbox.util.PDFTextStripper;
-
- import com.itextpdf.text.BaseColor;
- import com.itextpdf.text.Chapter;
- import com.itextpdf.text.Document;
- import com.itextpdf.text.DocumentException;
- import com.itextpdf.text.Font;
- import com.itextpdf.text.FontFactory;
- import com.itextpdf.text.List;
- import com.itextpdf.text.ListItem;
- import com.itextpdf.text.PageSize;
- import com.itextpdf.text.Paragraph;
- import com.itextpdf.text.Phrase;
- import com.itextpdf.text.Rectangle;
- import com.itextpdf.text.Section;
- import com.itextpdf.text.pdf.BaseFont;
- import com.itextpdf.text.pdf.PdfWriter;
-
- /**
- * 功能 PDF讀寫類
- * @CreateTime 2011-4-14 下午02:44:11
- */
- public class PDFUtil {
-
- // public static final String CHARACTOR_FONT_CH_FILE = "SIMFANG.TTF"; //仿宋常規(guī)
- public static final String CHARACTOR_FONT_CH_FILE = "SIMHEI.TTF"; //黑體常規(guī)
-
- public static final Rectangle PAGE_SIZE = PageSize.A4;
- public static final float MARGIN_LEFT = 50;
- public static final float MARGIN_RIGHT = 50;
- public static final float MARGIN_TOP = 50;
- public static final float MARGIN_BOTTOM = 50;
- public static final float SPACING = 20;
-
-
- private Document document = null;
-
- /**
- * 功能:創(chuàng)建導(dǎo)出數(shù)據(jù)的目標(biāo)文檔
- * @param fileName 存儲(chǔ)文件的臨時(shí)路徑
- * @return
- */
- public void createDocument(String fileName) {
- File file = new File(fileName);
- FileOutputStream out = null;
- document = new Document(PAGE_SIZE, MARGIN_LEFT, MARGIN_RIGHT, MARGIN_TOP, MARGIN_BOTTOM);
- try {
- out = new FileOutputStream(file);
- // PdfWriter writer =
- PdfWriter.getInstance(document, out);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (DocumentException e) {
- e.printStackTrace();
- }
- // 打開文檔準(zhǔn)備寫入內(nèi)容
- document.open();
- }
-
- /**
- * 將章節(jié)寫入到指定的PDF文檔中
- * @param chapter
- * @return
- */
- public void writeChapterToDoc(Chapter chapter) {
- try {
- if(document != null) {
- if(!document.isOpen()) document.open();
- document.add(chapter);
- }
- } catch (DocumentException e) {
- e.printStackTrace();
- }
- }
-
- /**
- * 功能 創(chuàng)建PDF文檔中的章節(jié)
- * @param title 章節(jié)標(biāo)題
- * @param chapterNum 章節(jié)序列號(hào)
- * @param alignment 0表示align=left,1表示align=center
- * @param numberDepth 章節(jié)是否帶序號(hào) 設(shè)值=1 表示帶序號(hào) 1.章節(jié)一;1.1小節(jié)一...,設(shè)值=0表示不帶序號(hào)
- * @param font 字體格式
- * @return Chapter章節(jié)
- */
- public static Chapter createChapter(String title, int chapterNum, int alignment, int numberDepth, Font font) {
- Paragraph chapterTitle = new Paragraph(title, font);
- chapterTitle.setAlignment(alignment);
- Chapter chapter = new Chapter(chapterTitle, chapterNum);
- chapter.setNumberDepth(numberDepth);
- return chapter;
- }
-
- /**
- * 功能:創(chuàng)建某指定章節(jié)下的小節(jié)
- * @param chapter 指定章節(jié)
- * @param title 小節(jié)標(biāo)題
- * @param font 字體格式
- * @param numberDepth 小節(jié)是否帶序號(hào) 設(shè)值=1 表示帶序號(hào) 1.章節(jié)一;1.1小節(jié)一...,設(shè)值=0表示不帶序號(hào)
- * @return section在指定章節(jié)后追加小節(jié)
- */
- public static Section createSection(Chapter chapter, String title, Font font, int numberDepth) {
- Section section = null;
- if(chapter != null) {
- Paragraph sectionTitle = new Paragraph(title, font);
- sectionTitle.setSpacingBefore(SPACING);
- section = chapter.addSection(sectionTitle);
- section.setNumberDepth(numberDepth);
- }
- return section;
- }
-
- /**
- * 功能:向PDF文檔中添加的內(nèi)容
- * @param text 內(nèi)容
- * @param font 內(nèi)容對(duì)應(yīng)的字體
- * @return phrase 指定字體格式的內(nèi)容
- */
- public static Phrase createPhrase(String text,Font font) {
- Phrase phrase = new Paragraph(text,font);
- return phrase;
- }
-
- /**
- * 功能:創(chuàng)建列表
- * @param numbered 設(shè)置為 true 表明想創(chuàng)建一個(gè)進(jìn)行編號(hào)的列表
- * @param lettered 設(shè)置為true表示列表采用字母進(jìn)行編號(hào),為false則用數(shù)字進(jìn)行編號(hào)
- * @param symbolIndent
- * @return list
- */
- public static List createList(boolean numbered, boolean lettered, float symbolIndent) {
- List list = new List(numbered, lettered, symbolIndent);
- return list;
- }
-
- /**
- * 功能:創(chuàng)建列表中的項(xiàng)
- * @param content 列表項(xiàng)中的內(nèi)容
- * @param font 字體格式
- * @return listItem
- */
- public static ListItem createListItem(String content, Font font) {
- ListItem listItem = new ListItem(content, font);
- return listItem;
- }
-
- /**
- * 功能:創(chuàng)造字體格式
- * @param fontname
- * @param size 字體大小
- * @param style 字體風(fēng)格
- * @param color 字體顏色
- * @return Font
- */
- public static Font createFont(String fontname, float size, int style, BaseColor color) {
- Font font = FontFactory.getFont(fontname, size, style, color);
- return font;
- }
-
- /**
- * 功能: 返回支持中文的字體---仿宋
- * @param size 字體大小
- * @param style 字體風(fēng)格
- * @param color 字體 顏色
- * @return 字體格式
- */
- public static Font createCHineseFont(float size, int style, BaseColor color) {
- BaseFont bfChinese = null;
- try {
- bfChinese = BaseFont.createFont(CHARACTOR_FONT_CH_FILE,BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
- } catch (DocumentException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return new Font(bfChinese, size, style, color);
- }
-
- /**
- * 最后關(guān)閉PDF文檔
- */
- public void closeDocument() {
- if(document != null) {
- document.close();
- }
- }
-
-
- /**
- * 讀PDF文件,使用了pdfbox開源項(xiàng)目
- * @param fileName
- */
- public void readPDF(String fileName) {
- File file = new File(fileName);
- FileInputStream in = null;
- try {
- in = new FileInputStream(fileName);
- // 新建一個(gè)PDF解析器對(duì)象
- PDFParser parser = new PDFParser(in);
- // 對(duì)PDF文件進(jìn)行解析
- parser.parse();
- // 獲取解析后得到的PDF文檔對(duì)象
- PDDocument pdfdocument = parser.getPDDocument();
- // 新建一個(gè)PDF文本剝離器
- PDFTextStripper stripper = new PDFTextStripper();
- // 從PDF文檔對(duì)象中剝離文本
- String result = stripper.getText(pdfdocument);
- System.out.println("PDF文件的文本內(nèi)容如下:");
- 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) {
- }
- }
- }
- }
-
- /**
- * 測試pdf文件的創(chuàng)建
- * @param args
- */
- public static void main(String[] args) {
-
- String fileName = "E:\\tmp\\pdf\\test11.pdf"; //這里先手動(dòng)把絕對(duì)路徑的文件夾給補(bǔ)上。
- PDFUtil pdfUtil = new PDFUtil();
-
- Font chapterFont = PDFUtil.createCHineseFont(20, Font.BOLD, new BaseColor(0, 0, 255));//文章標(biāo)題字體
- Font sectionFont = PDFUtil.createCHineseFont(16, Font.BOLD, new BaseColor(0, 0, 255));//文章小節(jié)字體
- Font textFont = PDFUtil.createCHineseFont(10, Font.NORMAL, new BaseColor(0, 0, 0));//小節(jié)內(nèi)容字體
-
- pdfUtil.createDocument(fileName);
- Chapter chapter = PDFUtil.createChapter("糖尿病病例1", 1, 1, 0, chapterFont);
- Section section1 = PDFUtil.createSection(chapter, "病例聯(lián)系人信息", sectionFont,0);
- Phrase text1 = PDFUtil.createPhrase("如您手中有同類現(xiàn)成病例,在填寫完以上基礎(chǔ)信息后,傳病例附件",textFont);
- section1.add(text1);
-
- Section section2 = PDFUtil.createSection(chapter, "病例個(gè)人體會(huì)", sectionFont,0);
- Phrase text2 = PDFUtil.createPhrase("1.下載病例生成PDF文檔",textFont);
- // text2.setFirstLineIndent(20); //第一行空格距離
- section2.add(text1);
- section2.add(text2);
-
- List list = PDFUtil.createList(true, false, 20);
- String tmp = "還有什么能夠文檔。文檔是 PDF 文檔的所有元素的容器。 ";
- ListItem listItem1 = PDFUtil.createListItem(tmp,textFont);
- ListItem listItem2 = PDFUtil.createListItem("列表2",textFont);
- list.add(listItem1);
- list.add(listItem2);
- section2.add(list);
-
- pdfUtil.writeChapterToDoc(chapter);
- pdfUtil.closeDocument();
- }
- }
- package com.test.common.util;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
-
- import org.apache.pdfbox.pdfparser.PDFParser;
- import org.apache.pdfbox.pdmodel.PDDocument;
- import org.apache.pdfbox.util.PDFTextStripper;
-
- import com.itextpdf.text.BaseColor;
- import com.itextpdf.text.Chapter;
- import com.itextpdf.text.Document;
- import com.itextpdf.text.DocumentException;
- import com.itextpdf.text.Font;
- import com.itextpdf.text.FontFactory;
- import com.itextpdf.text.List;
- import com.itextpdf.text.ListItem;
- import com.itextpdf.text.PageSize;
- import com.itextpdf.text.Paragraph;
- import com.itextpdf.text.Phrase;
- import com.itextpdf.text.Rectangle;
- import com.itextpdf.text.Section;
- import com.itextpdf.text.pdf.BaseFont;
- import com.itextpdf.text.pdf.PdfWriter;
-
- /**
- * 功能 PDF讀寫類
- * @CreateTime 2011-4-14 下午02:44:11
- */
- public class PDFUtil {
-
- // public static final String CHARACTOR_FONT_CH_FILE = "SIMFANG.TTF"; //仿宋常規(guī)
- public static final String CHARACTOR_FONT_CH_FILE = "SIMHEI.TTF"; //黑體常規(guī)
-
- public static final Rectangle PAGE_SIZE = PageSize.A4;
- public static final float MARGIN_LEFT = 50;
- public static final float MARGIN_RIGHT = 50;
- public static final float MARGIN_TOP = 50;
- public static final float MARGIN_BOTTOM = 50;
- public static final float SPACING = 20;
-
-
- private Document document = null;
-
- /**
- * 功能:創(chuàng)建導(dǎo)出數(shù)據(jù)的目標(biāo)文檔
- * @param fileName 存儲(chǔ)文件的臨時(shí)路徑
- * @return
- */
- public void createDocument(String fileName) {
- File file = new File(fileName);
- FileOutputStream out = null;
- document = new Document(PAGE_SIZE, MARGIN_LEFT, MARGIN_RIGHT, MARGIN_TOP, MARGIN_BOTTOM);
- try {
- out = new FileOutputStream(file);
- // PdfWriter writer =
- PdfWriter.getInstance(document, out);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (DocumentException e) {
- e.printStackTrace();
- }
- // 打開文檔準(zhǔn)備寫入內(nèi)容
- document.open();
- }
-
- /**
- * 將章節(jié)寫入到指定的PDF文檔中
- * @param chapter
- * @return
- */
- public void writeChapterToDoc(Chapter chapter) {
- try {
- if(document != null) {
- if(!document.isOpen()) document.open();
- document.add(chapter);
- }
- } catch (DocumentException e) {
- e.printStackTrace();
- }
- }
-
- /**
- * 功能 創(chuàng)建PDF文檔中的章節(jié)
- * @param title 章節(jié)標(biāo)題
- * @param chapterNum 章節(jié)序列號(hào)
- * @param alignment 0表示align=left,1表示align=center
- * @param numberDepth 章節(jié)是否帶序號(hào) 設(shè)值=1 表示帶序號(hào) 1.章節(jié)一;1.1小節(jié)一...,設(shè)值=0表示不帶序號(hào)
- * @param font 字體格式
- * @return Chapter章節(jié)
- */
- public static Chapter createChapter(String title, int chapterNum, int alignment, int numberDepth, Font font) {
- Paragraph chapterTitle = new Paragraph(title, font);
- chapterTitle.setAlignment(alignment);
- Chapter chapter = new Chapter(chapterTitle, chapterNum);
- chapter.setNumberDepth(numberDepth);
- return chapter;
- }
-
- /**
- * 功能:創(chuàng)建某指定章節(jié)下的小節(jié)
- * @param chapter 指定章節(jié)
- * @param title 小節(jié)標(biāo)題
- * @param font 字體格式
- * @param numberDepth 小節(jié)是否帶序號(hào) 設(shè)值=1 表示帶序號(hào) 1.章節(jié)一;1.1小節(jié)一...,設(shè)值=0表示不帶序號(hào)
- * @return section在指定章節(jié)后追加小節(jié)
- */
- public static Section createSection(Chapter chapter, String title, Font font, int numberDepth) {
- Section section = null;
- if(chapter != null) {
- Paragraph sectionTitle = new Paragraph(title, font);
- sectionTitle.setSpacingBefore(SPACING);
- section = chapter.addSection(sectionTitle);
- section.setNumberDepth(numberDepth);
- }
- return section;
- }
-
- /**
- * 功能:向PDF文檔中添加的內(nèi)容
- * @param text 內(nèi)容
- * @param font 內(nèi)容對(duì)應(yīng)的字體
- * @return phrase 指定字體格式的內(nèi)容
- */
- public static Phrase createPhrase(String text,Font font) {
- Phrase phrase = new Paragraph(text,font);
- return phrase;
- }
-
- /**
- * 功能:創(chuàng)建列表
- * @param numbered 設(shè)置為 true 表明想創(chuàng)建一個(gè)進(jìn)行編號(hào)的列表
- * @param lettered 設(shè)置為true表示列表采用字母進(jìn)行編號(hào),為false則用數(shù)字進(jìn)行編號(hào)
- * @param symbolIndent
- * @return list
- */
- public static List createList(boolean numbered, boolean lettered, float symbolIndent) {
- List list = new List(numbered, lettered, symbolIndent);
- return list;
- }
-
- /**
- * 功能:創(chuàng)建列表中的項(xiàng)
- * @param content 列表項(xiàng)中的內(nèi)容
- * @param font 字體格式
- * @return listItem
- */
- public static ListItem createListItem(String content, Font font) {
- ListItem listItem = new ListItem(content, font);
- return listItem;
- }
-
- /**
- * 功能:創(chuàng)造字體格式
- * @param fontname
- * @param size 字體大小
- * @param style 字體風(fēng)格
- * @param color 字體顏色
- * @return Font
- */
- public static Font createFont(String fontname, float size, int style, BaseColor color) {
- Font font = FontFactory.getFont(fontname, size, style, color);
- return font;
- }
-
- /**
- * 功能: 返回支持中文的字體---仿宋
- * @param size 字體大小
- * @param style 字體風(fēng)格
- * @param color 字體 顏色
- * @return 字體格式
- */
- public static Font createCHineseFont(float size, int style, BaseColor color) {
- BaseFont bfChinese = null;
- try {
- bfChinese = BaseFont.createFont(CHARACTOR_FONT_CH_FILE,BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
- } catch (DocumentException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return new Font(bfChinese, size, style, color);
- }
-
- /**
- * 最后關(guān)閉PDF文檔
- */
- public void closeDocument() {
- if(document != null) {
- document.close();
- }
- }
-
-
- /**
- * 讀PDF文件,使用了pdfbox開源項(xiàng)目
- * @param fileName
- */
- public void readPDF(String fileName) {
- File file = new File(fileName);
- FileInputStream in = null;
- try {
- in = new FileInputStream(fileName);
- // 新建一個(gè)PDF解析器對(duì)象
- PDFParser parser = new PDFParser(in);
- // 對(duì)PDF文件進(jìn)行解析
- parser.parse();
- // 獲取解析后得到的PDF文檔對(duì)象
- PDDocument pdfdocument = parser.getPDDocument();
- // 新建一個(gè)PDF文本剝離器
- PDFTextStripper stripper = new PDFTextStripper();
- // 從PDF文檔對(duì)象中剝離文本
- String result = stripper.getText(pdfdocument);
- System.out.println("PDF文件的文本內(nèi)容如下:");
- 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) {
- }
- }
- }
- }
-
- /**
- * 測試pdf文件的創(chuàng)建
- * @param args
- */
- public static void main(String[] args) {
-
- String fileName = "E:\\tmp\\pdf\\test11.pdf"; //這里先手動(dòng)把絕對(duì)路徑的文件夾給補(bǔ)上。
- PDFUtil pdfUtil = new PDFUtil();
-
- Font chapterFont = PDFUtil.createCHineseFont(20, Font.BOLD, new BaseColor(0, 0, 255));//文章標(biāo)題字體
- Font sectionFont = PDFUtil.createCHineseFont(16, Font.BOLD, new BaseColor(0, 0, 255));//文章小節(jié)字體
- Font textFont = PDFUtil.createCHineseFont(10, Font.NORMAL, new BaseColor(0, 0, 0));//小節(jié)內(nèi)容字體
-
- pdfUtil.createDocument(fileName);
- Chapter chapter = PDFUtil.createChapter("糖尿病病例1", 1, 1, 0, chapterFont);
- Section section1 = PDFUtil.createSection(chapter, "病例聯(lián)系人信息", sectionFont,0);
- Phrase text1 = PDFUtil.createPhrase("如您手中有同類現(xiàn)成病例,在填寫完以上基礎(chǔ)信息后,傳病例附件",textFont);
- section1.add(text1);
-
- Section section2 = PDFUtil.createSection(chapter, "病例個(gè)人體會(huì)", sectionFont,0);
- Phrase text2 = PDFUtil.createPhrase("1.下載病例生成PDF文檔",textFont);
- // text2.setFirstLineIndent(20); //第一行空格距離
- section2.add(text1);
- section2.add(text2);
-
- List list = PDFUtil.createList(true, false, 20);
- String tmp = "還有什么能夠文檔。文檔是 PDF 文檔的所有元素的容器。 ";
- ListItem listItem1 = PDFUtil.createListItem(tmp,textFont);
- ListItem listItem2 = PDFUtil.createListItem("列表2",textFont);
- list.add(listItem1);
- list.add(listItem2);
- section2.add(list);
-
- pdfUtil.writeChapterToDoc(chapter);
- pdfUtil.closeDocument();
- }
- }
|