如何在JAVA中使用PDFBox从PDF创建图像 [英] How to create image from PDF using PDFBox in JAVA

查看:374
本文介绍了如何在JAVA中使用PDFBox从PDF创建图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从PDF的第一页创建图像.我正在使用PDFBox.在研究了网络之后,我发现了以下代码片段:

I want to create an image from first page of PDF . I am using PDFBox . After researching in web , I have found the following snippet of code :

public class ExtractImages
 {
    public static void main(String[] args)
    {
        ExtractImages obj = new ExtractImages();
            try 
            {
                obj.read_pdf();
            }

            catch (IOException ex)
            {
                System.out.println("" + ex);
            }

    }

    void read_pdf() throws IOException 
    {
            PDDocument document = null; 
            try 
            {
                document = PDDocument.load("H:\\ct1_answer.pdf");
            }
            catch (IOException ex)
            {
                System.out.println("" + ex);
            }

            List<PDPage>pages =  document.getDocumentCatalog().getAllPages();
            Iterator iter =  pages.iterator(); 

            int i =1;
            String name = null;

            while (iter.hasNext()) 
            {
                PDPage page = (PDPage) iter.next();
                PDResources resources = page.getResources();
                Map pageImages = resources.getImages();
                if (pageImages != null) 
                { 
                    Iterator imageIter = pageImages.keySet().iterator();
                    while (imageIter.hasNext()) {
                        String key = (String) imageIter.next();
                        PDXObjectImage image = (PDXObjectImage) pageImages.get(key);
                        image.write2file("H:\\image" + i);
                        i ++;
                    }
                }
            }

        }

 } 

在上面的代码中没有错误.但是这段代码的输出却什么也没有.我期望上面的代码将产生一系列图像,这些图像将保存在H驱动器中.但是从该代码生成的代码中没有图像.为什么 ?

In the above code there is no error . But the output of this code is nothing . I have expected that the above code will produce a series of image which will be saved in H drive . But there is no image in that code produced from this code . Why ?

推荐答案

在不失礼貌的情况下,以下是您发布的代码在其主要工作循环中所做的事情:

Without trying to be rude, here is what the code you posted does inside its main working loop:

PDPage page = (PDPage) iter.next();
PDResources resources = page.getResources();
Map pageImages = resources.getImages();

它正在从PDF文件中获取每个页面,从页面中获取资源,并提取嵌入的图像.然后将它们写入磁盘.

It's getting each page from the PDF file, getting the resources from the page, and extracting the embedded images. It then writes those to disk.

如果要成为合格的软件开发人员,则需要能够研究和阅读文档.对于Java,这意味着Javadocs.谷歌搜索PDPage(或明确地转到apache站点)打开

If you are to be a competent software developer you need to be able to research and read documentation. With Java, that means Javadocs. Googling PDPage (or explicitly going to the apache site) turns up the Javadoc for PDPage.

在该页面上,找到用于将PDPage转换为图像的方法convertToImage()的两个版本.问题解决了.

On that page you find two versions of the method convertToImage() for converting the PDPage to an image. Problem solved.

除了...

不幸的是,他们返回一个java.awt.image.BufferedImage,它基于您提出的其他问题是一个问题,因为您正在使用的Android平台不支持它.

Unfortunately, they return a java.awt.image.BufferedImage which based on other questions you have asked is a problem because it is not supported on the Android platform which is what you're working on.

简而言之,您无法使用Android上的Apache的PDFBox来完成您想做的事情.

In short, you can't use Apache's PDFBox on Android to do what you're trying to do.

在StackOverflow上搜索时,您会发现同一问题以不同的形式多次提出,这将导致您进入以下问题: https://stackoverflow.com/a/4779852/302916

Searching on StackOverflow you find this same question posed several times in different forms, which will lead you to this: https://stackoverflow.com/questions/4665957/pdf-parsing-library-for-android/4766335#4766335 with the following answer that would be of interest to you: https://stackoverflow.com/a/4779852/302916

不幸的是,即使上述回答说的也可以使用...也不是很友好的用户;我找不到如何"或文档.它也被标记为"alpha".对于胆小的人来说,这可能不是什么,因为它需要阅读并理解他们的代码才能开始使用它.

Unfortunately even the one that the aforementioned answer says will work ... is not very user friendly; there's no "How to" or docs that I can find. It's also labeled as "alpha". This is probably not something for the feint hearted as it's going to require reading and understanding their code to even start using it.

这篇关于如何在JAVA中使用PDFBox从PDF创建图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆