pdfbox创建pdf_如何在JAVA中使用PDFBox从PDF创建图像

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);

}

Listpages = 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 ++;

}

}

}

}

}

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();

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.

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.

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

Except ...

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.

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

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值