java操作PDF(PDFBOX和Itext框架)

jopen 11年前發布 | 94K 次閱讀 PDF工具包 PDFBox iText

java有很多可以操作pdf的框架,pdfbox和itext就是其中的兩種

pdfbox有如下作用

提取文本,包括Unicode字符。
和Jakarta Lucene等文本搜索引擎的整合過程十分簡單。
加密/解密PDF文檔。

從PDF和XFDF格式中導入或導出表單數據。
向已有PDF文檔中追加內容。
將一個PDF文檔切分為多個文檔。
覆蓋PDF文檔

下面是一個使用pdfbox的測試程序

public class PdfBoxTest {

    public void getText(String file) throws Exception{
        //是否排序
        boolean sort = false;
        //pdf文件名
        String pdfFile = file;
        //輸入文本文件名稱
        String textFile = null;
        //編碼方式
        String encoding = "UTF-8";
        //開始提取頁數
        int startPage = 1;
        //結束提取頁數
        int endPage = Integer.MAX_VALUE;
        //文件輸入流,輸入文本文件
        Writer output = null;
        //內存中存儲的PDF Document
        PDDocument document = null;

        try{
            try{
                //首先當作一個URL來加載文件,如果得到異常再從本地系統裝載文件
                URL url = new URL(pdfFile);
                document = PDDocument.load(url);
                String fileName = url.getFile();

                if(fileName.length() > 4){
                    //以原來pdf名稱來命名新產生的txt文件
                    File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");
                    textFile = outputFile.getName();
                }
            }catch(Exception e){
                //如果作為URL裝載得到異常則從文件系統裝載
                document = PDDocument.load(pdfFile);
                if(pdfFile.length() > 4){
                    textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";
                }
            }
            //文件輸出流,寫入文件到textFile
            output = new OutputStreamWriter(new FileOutputStream(textFile),encoding);
            //PDFTextStripper來提取文本
            PDFTextStripper stripper = new PDFTextStripper();
            //設置是否排序
            stripper.setSortByPosition(sort);
            //設置起始頁
            stripper.setStartPage(startPage);
            //設置結束頁
            stripper.setEndPage(endPage);
            //調用PDFTextStripper的writeText提取并輸出文本
            stripper.writeText(document, output);
        }finally{
            if(output != null){
                output.close();
            }
            if(document != null){
                document.close();
            }
        }
    }
    /** *//**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        PdfBoxTest test = new PdfBoxTest();
        try{
            test.getText("E://test.pdf");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
iText是著名的開放源碼的站點sourceforge一個項目,是用于生成PDF文檔的一個java類庫。通過iText不僅可以生成PDF或rtf的文檔,而且可以將XML、Html文件轉化為PDF文件。 

下面是一個使用itext生成pdf的例子

public class ITextTest {
    public static void main(String args[]){
        writePdf();
    }

    public static void writePdf(){
        Document document = new Document();
        try {
            PdfWriter.getInstance(document, new FileOutputStream("Helloworld.pdf"));
        } catch (DocumentException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        document.open();
        try {
            document.add(new Paragraph("Hello World"));
        } catch (DocumentException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        document.close();
    }

}

默認的iText字體設置不支持中文字體,需要下載遠東字體包iTextAsian.jar,否則不能往PDF文檔中輸出中文字體。通過下面的代碼就可以在文檔中使用中文了: 


BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); 
com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL);
Paragraph pragraph=new Paragraph("你好", FontChinese);   

參考文檔:http://yuleihome.iteye.com/blog/181348

http://www.cnblogs.com/hejycpu/archive/2009/01/19/1378380.html

http://gohands.iteye.com/blog/160534

總結一下,本文采用pdfbox和itext分別演示了如何讀取pdf和生成pdf的簡單方法。

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!