はじめに
JavaでPDFを操作するライブラリの中で,「PDFBox」に触れてみる特長は
- PDFファイルからのテキストの抽出
 - PDFファイルの結合
 - PDFファイルの暗号・複合化
 - 検索エンジン Lucene の組み込み
 - FDFデータの埋め込み
 - イメージをPDFに変換
 - PDFからのイメージ取得
 
準備
Apache PDFBox - Download からダウンロード※今回は,「pdfbox-1.4.0.jar」
使ってみる
PDFファイルの読み込み1 |       FileInputStream pdfStream = new FileInputStream(readFile); | 
2 |       PDFParser pdfParser = new PDFParser(pdfStream); | 
3 |       pdfParser.parse(); // 分析 | 
4 |       PDDocument pdf =     pdfParser.getPDDocument(); | 
PDFファイルの書き込み
1 |       FileOutputStream stream = new FileOutputStream(writeFile); | 
2 |       COSWriter writer = new COSWriter(stream); | 
3 |       writer.write(pdf); | 
PDFからイメージを抽出
1 |       PDDocumentCatalog docCatalog =       pdf.getDocumentCatalog(); | 
2 |       PDAcroForm acroForm =     docCatalog.getAcroForm(); | 
3 |       PDField field = acroForm.getField(name); // フィールド取得 | 
4 |       if (field != null) { | 
5 |           field.setValue(value); // フィールドに埋め込み | 
6 |       } | 
PDFファイルの読み込み
01 |       FileInputStream pdfStream = new FileInputStream(readFile); | 
02 |       PDFParser pdfParser = new PDFParser(pdfStream); | 
03 |       pdfParser.parse(); // 分析 | 
04 |       PDDocument pdf =     pdfParser.getPDDocument(); | 
05 |       int imageCounter = 1; | 
06 |       List pages =     pdf.getDocumentCatalog().getAllPages(); | 
07 |       Iterator iter =   pages.iterator(); | 
08 |       while (iter.hasNext()) { // 全ページからイメージを抽出 | 
09 |           PDPage page = (PDPage)   iter.next(); | 
10 |           PDResources resources =     page.getResources(); | 
11 |           Map images =   resources.getImages(); | 
12 |           if (images != null) { | 
13 |               Iterator imageIter =     images.keySet().iterator(); | 
14 |               while (imageIter.hasNext()) { | 
15 |                   String key = (String)   imageIter.next(); | 
16 |                   PDXObjectImage image = (PDXObjectImage)       images.get(key); | 
17 |                   String name = key + "-" + imageCounter; | 
18 |                   imageCounter++; | 
19 |                   System.out.println("Writing image:" + name); | 
20 |                   image.write2file(name); // ファイル出力 | 
21 |               } | 
22 |           } | 
23 |       } | 
【参考】
◇JAVA開発メモ
JavaでPDFの文章を抽出するには、「PDFBox」 というAPIを用いる.
・JavaでPDFから文章を抽出
【結果】
�全文英語PDF ⇒ 抽出できた
(文章が、途中からの抽出だった。コンソールのバッファサイズの問題かも←あるのか?)
�全文日本語PDF ⇒ 抽出できた
�パワーポイントファイルをPDFファイル化したもの ⇒ 抽出できた
まだ、よくわからないが、PDFファイルのサイズ や 文字量の上限があるのかもしれない.
※インポートは「pdfbox-1.2.1-src.zip」ではバグが発生するので、「pdfbox-app-1.2.1.jar」を選択する.
                
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
・JavaでPDFから文章を抽出
【結果】
�全文英語PDF ⇒ 抽出できた
(文章が、途中からの抽出だった。コンソールのバッファサイズの問題かも←あるのか?)
�全文日本語PDF ⇒ 抽出できた
�パワーポイントファイルをPDFファイル化したもの ⇒ 抽出できた
まだ、よくわからないが、PDFファイルのサイズ や 文字量の上限があるのかもしれない.
※インポートは「pdfbox-1.2.1-src.zip」ではバグが発生するので、「pdfbox-app-1.2.1.jar」を選択する.
01 |       import java.io.*; | 
02 |       
03 |       import org.apache.pdfbox.pdfparser.PDFParser; | 
04 |       import org.apache.pdfbox.pdmodel.PDDocument; | 
05 |       import org.apache.pdfbox.util.PDFTextStripper; | 
06 |       
07 |       public class ExtractPDF { | 
08 |           static String extractText(String   filePath){ | 
09 |               ByteArrayOutputStream out = null; | 
10 |               try { | 
11 |                   FileInputStream pdfStream = new FileInputStream(filePath); | 
12 |                   PDFParser parser = new PDFParser(pdfStream); | 
13 |                   parser.parse(); | 
14 |                   PDDocument pdf =   parser.getPDDocument(); | 
15 |                   PDFTextStripper stripper = new PDFTextStripper(); | 
16 |                   out = new ByteArrayOutputStream(); | 
17 |                   stripper.writeText(pdf, new BufferedWriter(newOutputStreamWriter(out))); | 
18 |               } catch (FileNotFoundException e) { | 
19 |                   e.printStackTrace(); | 
20 |               } catch (IOException e) { | 
21 |                   e.printStackTrace(); | 
22 |               } | 
23 |               return out.toString(); | 
24 |           } | 
25 |           public static void main(String[] args){ | 
26 |               String pdfFile = "〇〇.pdf"; | 
27 |               String data=null; | 
28 |               data = extractText(pdfFile); | 
29 |               System.out.println(data); | 
30 |           } | 
31 |       } | 
0 件のコメント:
コメントを投稿