美麗涵涵童裝店--說我博客名字,給你們打折!
          隨筆 - 82  文章 - 266  trackbacks - 0
          <2009年6月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011


          點擊這里給楊愛友發(fā)消息
          美麗涵涵童裝店
          說我博客名字,給你們打折!

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章檔案

          好友的BLOG

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          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 {

           /**
            * 很多應(yīng)用程序要求動態(tài)生成 PDF 文檔。這類應(yīng)用程序包括銀行生成用于電子郵件投遞的客戶報表,到讀者購買特定圖書章節(jié)并以 PDF
            * 格式接收這些文檔。例子羅列下去是很多的。在本文中,將使用 iText Java 庫生成 PDF
            * 文檔,并引導(dǎo)您完成一個示例應(yīng)用程序,以使您能夠更好地理解和使用 iText。 iText 是 Lowagie.com 站點(請參閱
            * 參考資料)免費提供的 Java 庫。iText 庫的功能很強大,支持 HTML、RTF 和 XML 文檔的生成,此外還能夠生成 PDF
            * 文檔。可以從多種字體中選擇文檔中所使用的字體。同時,iText 的結(jié)構(gòu)允許使用相同的代碼生成以上任意類型的文檔。
            * http://www.lowagie.com/iText/ iText API:近距離觀察 com.lowagie.text.Document
            * 是生成 PDF
            * 的主要的類。它是需要使用的第一個類。一旦開始創(chuàng)建文檔,將需要一個寫入器向文檔中寫入內(nèi)容。com.lowagie.text.pdf.PdfWriter
            * 就是一個 PDF 寫入器。下面列出了通常需要使用的類: com.lowagie.text.Paragraph —— 這個類表示一個縮進的段落。
            * com.lowagie.text.Chapter —— 這個類表示 PDF 文檔中的章節(jié)。使用 Paragraph 作為題目并使用 int
            * 作為章節(jié)號碼來創(chuàng)建它。 com.lowagie.text.Font ——
            * 這個類包含了全部的字體規(guī)范,例如字體、大小、樣式和顏色。各種字體都在這個類中聲明為靜態(tài)常數(shù)。 com.lowagie.text.List ——
            * 這個類表示一個列表,按順序包含許多 ListItems。 com.lowagie.text.Table ——
            * 這個類表示包含單元格的表,單元格有序地排列在矩陣中。
            */

           /**
            * 寫PDF文件,展示了PDF文檔、章節(jié)、小節(jié)、字體、段落、表格、列表的使用 最后展示如何使用寫入中文。
            *
            * @param fileName
            */
           public void writePDF(String fileName) {
            File file = new File(fileName);
            FileOutputStream out = null;

            try {
             // (1)實例化文檔對象
             // 第一個參數(shù)是頁面大小。接下來的參數(shù)分別是左、右、上和下頁邊距。
             Document document = new Document(PageSize.A4, 50, 50, 50, 50);

             // (2)創(chuàng)建寫入器
             // 第一個參數(shù)是對文檔對象的引用,第二個參數(shù)是輸出的文件,將out和document連接起來
             out = new FileOutputStream(file);
             PdfWriter writer = PdfWriter.getInstance(document, out);
             // 打開文檔準備寫入內(nèi)容
             document.open();

             // (3)下面創(chuàng)建章節(jié)對象
             // 首先創(chuàng)建段落對象,作為章節(jié)的標題。FontFactory用于指定段落的字體。
             Font font = FontFactory.getFont(FontFactory.HELVETICA, 18,
               Font.BOLDITALIC, new Color(0, 0, 255));
             Paragraph chapter1_title = new Paragraph("Chapter 1", font);
             // 創(chuàng)建了一個章節(jié)對象,標題為"Chapter 1"
             Chapter chapter1 = new Chapter(chapter1_title, 1);
             // 將編號級別設(shè)為 0 就不會在頁面上顯示章節(jié)編號
             chapter1.setNumberDepth(0);

             // (4)創(chuàng)建小節(jié)對象
             // 創(chuàng)建小節(jié)對象的標題
             font = FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD,
               new Color(255, 0, 0));
             Paragraph section1_title1 = new Paragraph("Section 1 of Chapter 1",
               font);
             // 創(chuàng)建一個小節(jié)對象,標題為"This is Section 1 in Chapter 1",屬于chapter1。
             Section section1 = chapter1.addSection(section1_title1);

             // (5)往小節(jié)中寫文本內(nèi)容
             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)往小節(jié)中寫表格
             // 創(chuàng)建表格對象
             Table table = new Table(5, 5);
             // 設(shè)置表格邊框顏色
             table.setBorderColor(new Color(220, 255, 100));
             // 設(shè)置單元格的邊距間隔等
             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();
             // 添加表的內(nèi)容
             for (int rowNum = 1; rowNum < 5; rowNum++) {
              for (int colNum = 0; colNum < 5; colNum++) {
               cell = new Cell("value-" + rowNum + "-" + colNum);
               table.addCell(cell);
              }
             }
             // 將表格對象添加到小節(jié)對象中
             section1.add(table);

             // (7)添加列表
             // 列表包含一定數(shù)量的 ListItem。可以對列表進行編號,也可以不編號。
             // 將第一個參數(shù)設(shè)置為 true 表明想創(chuàng)建一個進行編號的列表;
             // 第二個參數(shù)設(shè)置為true表示列表采用字母進行編號,為false則用數(shù)字進行編號;
             // 第三個參數(shù)為列表內(nèi)容與編號之間的距離。
             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);
             // 將列表對象添加到小節(jié)對象中
             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)將章節(jié)對象加入到文檔中
             document.add(chapter1);

             // (10)關(guān)閉文檔
             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 {
               // 關(guān)閉輸出文件流
               out.close();
              } catch (IOException e1) {
              }
             }
            }
           }

           /**
            * 讀PDF文件,使用了pdfbox開源項目,新的版本已經(jīng)支持中文了。 上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() + "的文本內(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) {
              }
             }
            }
           }

           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 楊愛友 閱讀(2800) 評論(0)  編輯  收藏 所屬分類: java相關(guān)技術(shù)
          美麗涵涵童裝店
          親,說我博客名字,給你們打折!
          主站蜘蛛池模板: 河曲县| 刚察县| 龙陵县| 建昌县| 南澳县| 余姚市| 铜梁县| 兴化市| 宜州市| 鞍山市| 夏津县| 宣恩县| 巴东县| 枝江市| 雅安市| 佛冈县| 宝丰县| 张北县| 靖远县| 丰台区| 马龙县| 岳西县| 九台市| 江口县| 黎川县| 祁连县| 庐江县| 罗平县| 昆山市| 汉源县| 塔城市| 横峰县| 东方市| 汉沽区| 乌拉特后旗| 巍山| 繁峙县| 东山县| 浦城县| 肥西县| 江山市|