2014-08-29 48 views
0

我试过了一个PDF页面来图像,但是只是提取了PDF页面中的每个图像。而不是页面图像。如何获取PDF页面的图像(包含文本)。不是PDF图像中的图像

下面的代码:

public class ExtractionPDFtoThumbImgs { 

    static String filePath = "/Users/tmdtjq/Downloads/PDFTest/test.pdf"; 
    static String outputFilePath = "/Users/tmdtjq/Downloads/PDFTest/pageimages"; 

    public static void change(File inputFile, File outputFolder) throws IOException { 
     //TODO check the input file exists and is PDF 
     //TODO for the treatment of PDF encrypted 
     PDDocument doc = null; 
     try { 
      doc = PDDocument.load(inputFile); 
      List<PDPage> allPages = doc.getDocumentCatalog().getAllPages(); 
      for (int i = 0; i <allPages.size(); i++) { 
       PDPage page = allPages.get(i); 
       page.convertToImage(); 
       BufferedImage image = page.convertToImage(); 
       ImageIO.write(image, "jpg", new File(outputFolder.getAbsolutePath() + File.separator + (i + 1) + ".jpg")); 
      } 
     } finally { 
      if (doc != null) { 
       doc.close(); 
      } 
     } 
    } 

    public static void main(String[] args) { 
     File inputFile = new File(ExtractionPDFtoThumbImgs.filePath); 
     File outputFolder = new File(ExtractionPDFtoThumbImgs.outputFilePath); 
     if(!outputFolder.exists()){ 
      outputFolder.mkdirs(); 
     } 
     try { 
      ExtractionPDFtoThumbImgs.change(inputFile, outputFolder); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

以上PDF页面代码中提取图像。不转换PDF页面中的图像(包含文本)。

是否有转换工具(PDF页面图像)或转换PDFBox类?

请建议如何获取PDF页面(包含文本)的图像。不要在PDF页面中获取图像。

before converting

after converting

+0

请分享这样的PDF。方法'convertToImage'应该没问题,但它可能有缺陷。 – mkl 2014-08-29 15:30:47

+0

我不明白这个问题。 convertToImage()是要走的路。有时部分文本可能会丢失,这是type1字体的问题。这是在未发布(但非常好)2.0版本中解决的。 – 2014-08-29 19:21:31

回答

0

尝试pdftocairo,它是poppler的一部分。

我使用ImageMagick将PDF转换为图像,而且它依赖于Ghostscript可有时是挑剔你喂养它,所以它被击中或错过了PDF ...

到目前为止pdftocairo一直固体。

http://poppler.freedesktop.org