2016-12-29 4 views
-4

私のコードは、すべてのPDFファイルから画像を抽出していません。それはいくつかのPDFファイルのために良い作品です。私を助けてください。 ExtractImages.java私のコードはすべてのPDFファイルから画像を抽出していません

package pdttotextconvertor; 

import java.io.IOException; 
import com.itextpdf.text.DocumentException; 
import com.itextpdf.text.pdf.PdfReader; 
import com.itextpdf.text.pdf.parser.PdfReaderContentParser; 

/** 
* Extracts images from a PDF file. 
*/ 
public class ExtractImages { 

    /** 
    * PDF to extract images from 
    */ 
    public static final String SOURCE_PDF = "C:/Latest Maven Code/pdttotextconvertor/src/main/resources/LC DPF example 1.pdf"; 

    /** 
    * Parses a PDF and extracts all the images. 
    * 
    * @param filename the source PDF 
    * @param destination the directory to save images 
    */ 
    public void extractImages(String filename, String destination) 
      throws IOException, DocumentException { 
     System.out.println("Processing PDF at " + filename); 
     System.out.println("Saving images to " + destination); 

     PdfReader reader = new PdfReader(filename); 
     PdfReaderContentParser parser = new PdfReaderContentParser(reader); 
     ImageRenderListener listener = new ImageRenderListener(destination + "/Img%s.%s"); 
     for (int i = 1; i <= reader.getNumberOfPages(); i++) { 
      parser.processContent(i, listener); 
     } 
     reader.close(); 
    } 

    /** 
    * Main method. 
    * 
    * @param args no arguments needed 
    * @throws DocumentException 
    * @throws IOException 
    */ 
    public static void main(String[] args) throws IOException, DocumentException { 
     String sourcePDF = SOURCE_PDF; 
     String destination = "target/images"; 
     if (args.length > 0) { 
      sourcePDF = args[0]; 
      if (args.length > 1) { 
       destination = args[1]; 
      } 
     } 

     new ExtractImages().extractImages(sourcePDF, destination); 
    } 
} 

ImageRenderListener.java http://www.filedropper.com/lcdpfexample23

は、事前に感謝を助けてくださいで動作していない

package scannedPdfConvertor; 

import java.io.FileOutputStream; 
import java.io.IOException; 

import com.itextpdf.text.pdf.parser.ImageRenderInfo; 
import com.itextpdf.text.pdf.parser.PdfImageObject; 
import com.itextpdf.text.pdf.parser.RenderListener; 
import com.itextpdf.text.pdf.parser.TextRenderInfo; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

/** 
* Saves images to a directory. 
* 
* @author mnguyen 
*/ 
public class ImageRenderListener implements RenderListener { 

    /** 
    * The directory path to store images. 
    */ 
    protected String path; 

    /** 
    * Creates a RenderListener that will look for images. 
    */ 
    public ImageRenderListener(String path) { 
     this.path = path; 
    } 

    /** 
    * @see com.itextpdf.text.pdf.parser.RenderListener#beginTextBlock() 
    */ 
    public void beginTextBlock() { 
    } 

    /** 
    * @see com.itextpdf.text.pdf.parser.RenderListener#endTextBlock() 
    */ 
    public void endTextBlock() { 
    } 

    /** 
    * @see com.itextpdf.text.pdf.parser.RenderListener#renderImage(
    * com.itextpdf.text.pdf.parser.ImageRenderInfo) 
    */ 
    public void renderImage(ImageRenderInfo renderInfo) { 
     try { 
      String filename; 
      FileOutputStream os; 
      PdfImageObject image = renderInfo.getImage(); 
      if (image == null) { 
       return; 
      } 
      filename = String.format(path, renderInfo.getRef().getNumber(), image.getFileType()); 
      System.out.println("Writing image to file: " + filename); 
      os = new FileOutputStream(filename); 
      os.write(image.getImageAsBytes()); 
      os.flush(); 
      os.close(); 
     } catch (IOException e) { 
      Logger.getLogger(ImageRenderListener.class.getName()).log(Level.SEVERE, null, e); 
     } 
    } 

    /** 
    * @see com.itextpdf.text.pdf.parser.RenderListener#renderText(
    * com.itextpdf.text.pdf.parser.TextRenderInfo) 
    */ 
    public void renderText(TextRenderInfo renderInfo) { 

    } 
} 

のPDFファイルへのリンク: はここに私のコードです。

+1

あなたのpdfファイルを共有してください。あなたの質問がiTextについてのものであれば、pdfboxで質問にタグを付けないでください。 –

+2

あなたが助けを求めているので、「私のコードは時々動作しません」とコメントを外したコードを投稿するだけでいいわけではありません。あなたの問題がどこで発生するのか、あなたは何か考えていますか?あなたのログには情報が含まれていますか?あなたのコードはエラーなく完了しますか?など –

+2

私の推測では、OPはラスターイメージのためにベクトルイメージを間違えるか(彼のコードは* Image XObjects *のみを抽出する)、iTextでサポートされていないイメージに遭遇する(私はわからないが、JBIG2サポートされていません; JBIG2をiTextでPDFに変換することはできますが、私はそこに道があるとは思わない)。 –

答えて

1

OPで共有されているサンプルドキュメント "LC DPF Example 2.3.pdf"には、ビットマップイメージもテキストも含まれていません。ベクトルグラフィックスだけが含まれています。

など。最初の行の "Letter of Page"の "P"という文字で実際に描かれているのは、これらの(m)ove、(l)ine、(c)urve、close(h)、および(f)は、病気の命令は:(それがあるだけで、画像やテキストをビットマップにリッスンとして)

71.16 804.07 m 
71.16 806.83 l 
72.12 806.83 l 
72.72 806.83 73.2 806.83 73.56 806.83 c 
73.8 806.95 74.04 807.07 74.28 807.19 c 
74.52 807.43 74.76 807.67 74.88 807.91 c 
75 808.27 75.12 808.63 75.12 809 c 
75.12 809.59 75 810.07 74.76 810.43 c 
74.4 810.79 74.04 811.03 73.68 811.15 c 
73.44 811.15 72.84 811.27 72 811.27 c 
69.72 811.27 l 
69.72 804.07 l 
71.16 804.07 l 
h 
71.88 810.07 m 
72.36 810.07 72.72 809.95 72.84 809.95 c 
73.08 809.95 73.32 809.83 73.44 809.71 c 
73.56 809.47 73.68 809.23 73.68 809 c 
73.68 808.75 73.56 808.63 73.56 808.51 c 
73.44 808.27 73.2 808.15 73.08 808.15 c 
72.84 808.03 72.48 808.03 71.88 808.03 c 
71.16 808.03 l 
71.16 810.07 l 
71.88 810.07 l 
h 
f 

したがって、RenderListenerインタフェースOPの実装には、何かを見つけることができません。

ExtRenderListenerを実装して、パスの作成とレンダリングの指示を追加的に聞きます。残念ながら、それは彼に上記の指示に相当するものを与えてくれるだけで、 "この充満したパスは文字 'P'のようには見えません。

したがって、彼はそのような文字パス認識を自分で実装するか、代わりにOCRを使用する必要があります。

関連する問題