<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Cyh的博客

    Email:kissyan4916@163.com
    posts - 26, comments - 19, trackbacks - 0, articles - 220

    操作PDF文件

    Posted on 2009-05-30 23:38 啥都寫點 閱讀(1012) 評論(1)  編輯  收藏 所屬分類: J2SE
    關鍵技術:
    • 一個Excel文檔從大到小可以分成如下幾個要素:文檔、章節、小節、段落、表格、列表。
    • com.lowagie.text.Document表示PDF文檔。必須為它創建一個PDF寫入器,即com.lowagie.text.pdf.Pdfwriter對象,寫入器的作用是將Document對象與目標文件關聯起來。調用Document的open方法便打開與目標文件的連接;Document的add方法為文檔添加章節。
    • com.lowagie.text.Chapter表示PDF文檔中的章節。它的setTitle方法設置章節的標題;setNumberDepth方法設置小節的編號級別;add方法為小節中添加內容,可以是段落、表格、列表等。
    • com.lowagie.text.Paragraph表示PDF文檔中的段落。可以指定段落的對齊方式、字體等屬性。
    • com.lowagie.text.Table表示PDF文檔中的表格。通過它的一系列set方法可以設置表格的樣式,比如邊框大小、顏色等;addCell方法為表格添加單元格,單元格是com.lowagie.text.Cell對象。
    • com.lowagie.text.List表示PDF文檔中的列表。com.lowagie.text.ListItem表示列表中項。通過List的add方法將列表項添加到列表中。
      使用pdfbox的類庫讀取PDF文檔的關鍵技術點如下:
    • org.pdfbox.pdfparser.PDFParser用于解析PDF文檔,它的parse方法對PDF文件輸入流進行解析;getPDDocument方法獲得解析后得到的PDF文檔對象,是一個org.pdfbox.pdmodel.PDDocument對象。
    • org.pdfbox.util.PDFTextStripper是分析PDF文檔對象中文本的工具類,它的getText方法能夠提取PDF文檔對象中包含的文本。

    package book.io;

    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;

    /**
     * 很多應用程序要求動態生成 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 —— 這個類表示包含單元格的表,單元格有序地排列在矩陣中。
     
    */
    public class PDFFile {

        
    /**
         * 寫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, 50505050);

                
    //(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(00255));
                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(25500));
                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(55);
                
    //設置表格邊框顏色
                table.setBorderColor(new Color(220255100));
                
    //設置單元格的邊距間隔等
                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。可以對列表進行編號,也可以不編號。
                
    // 將第一個參數設置為 true 表明想創建一個進行編號的列表;
                
    // 第二個參數設置為true表示列表采用字母進行編號,為false則用數字進行編號;
                
    // 第三個參數為列表內容與編號之間的距離。
                List list = new List(truefalse20);
                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("simfang.ttf", BaseFont.IDENTITY_H, BaseFont.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() + "生成失?。?/span>" + 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) {
            PDFFile pdf 
    = new PDFFile();
            String fileName 
    = "C:/temp/tempPDF.pdf";
            pdf.writePDF(fileName);
            pdf.readPDF(fileName);
        }
    }


                                                                                                           --    學海無涯
            

    Feedback

    # re: 操作PDF文件  回復  更多評論   

    2012-05-12 10:06 by ly.wolf
    stripper.getText(pdfdocument);
    這裡提示錯誤:
    "Object reference not set to an instance of an object."





    ly-msn@msn.com
    主站蜘蛛池模板: 亚洲大片免费观看| 亚洲最大的成网4438| 亚洲综合精品第一页| 波多野结衣免费在线| 久久精品国产亚洲AV大全| 一级特黄aa毛片免费观看| 国产亚洲综合网曝门系列| 一级毛片在播放免费| 亚洲裸男gv网站| 一本久久免费视频| 久久久久无码专区亚洲av| 久久国产一片免费观看| 亚洲国产另类久久久精品| 美女视频黄a视频全免费网站色窝| 国产亚洲精久久久久久无码AV | 亚洲国产精品线观看不卡| 精品熟女少妇a∨免费久久| 亚洲福利电影一区二区?| 久久国产免费福利永久| 日本亚洲精品色婷婷在线影院| 夫妻免费无码V看片| 羞羞视频免费网站含羞草| 亚洲日本在线观看视频| 美女视频黄的免费视频网页| 久久亚洲精品无码aⅴ大香| 中文字幕无码视频手机免费看| 亚洲国产成人无码AV在线| 亚洲人成人无码网www国产| 成全高清在线观看免费| 亚洲日本在线播放| 午夜精品在线免费观看| 成人午夜影视全部免费看| 亚洲综合精品香蕉久久网97| 午夜影视在线免费观看| av午夜福利一片免费看久久| 久久亚洲精品成人AV| 日韩毛片无码永久免费看| 最近免费中文字幕MV在线视频3| 亚洲三级视频在线观看| 免费午夜爽爽爽WWW视频十八禁| 日本高清免费观看|