快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

和记娱乐下载官方平台:轻松使用apache pdfbox将pdf文件生成图片



近期在项目中应用到了大年夜量的报表开拓,必要将html页面中的表格内容导出到pdf word excel和图片,前三者都对照好实现。唯独后者天生图片应用ImageIo操作时天生的图片有点惨不忍睹。颠末大年夜量google后发明,pdfbox这个组件不错,可以将pdf文件轻松天生图片。这不问题办理了,但在应用历程中不然,受到了很多致命性的袭击。pdfbox在处置惩罚中文pdf的时刻就会体现的对照脆弱点。但对英文版的pdf导出图片,那是杠杠的。只管这样,照样记录一下,终究这方面的资料很少。我险些搜遍了全部google,baidu才汇集到那么一点点资料。这里跟大年夜家分享下。

所依附的JAR:

commons-logging-1.1.1.jar

fontbox-1.2.1.jar

pdfbox-1.2.1.jar

示例代码:

/**//*

* Licensed to the Apache Software Foundation (ASF) under one or more* contributor license agreements.See the NOTICE file distributed with

* this work for additional information regarding copyright ownership.* The ASF licenses this file to You under the Apache License, Version 2.0

* (the "License"); you may not use this file except in compliance with* the License.You may obtain a copy of the License a和记娱乐下载官方平台t

**http://www.apache.org/licenses/LICENSE-2.0

** Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and* limitations under the License.

*/ package com.future.pdfbox.image;

import java.awt.i和记娱乐下载官方平台mage.BufferedImage;

import java.io.File;import java.io.FileOutputStream;

import java.io.IOException;import java.util.Iterator;

import java.util.List;

import javax.imageio.IIOImage;import javax.imageio.ImageIO;

import javax.imageio.ImageWriter;import javax.imageio.stream.ImageOutputStream;

import org.apache.pdfbox.pdmodel.PDDocument;

import org.apache.pdfbox.pdmodel.PDPage;

public class ExtractImages{

public static void main(String[] args) throws IOException {PDDocument doc = PDDocument.load("F:\\1.pdf");

int pageCount 和记娱乐下载官方平台=和记娱乐下载官方平台 doc.getPageCount();System.out.println(pageCount);

List pages = doc.getDocumentCatalog().getAllPages();for(int i=0;i

PDPage page = (PDPage)pages.get(i);BufferedImage ima和记娱乐下载官方平台ge = page.convertToImage();

Iterator iter = ImageIO.getImageWritersBySuffix("jpg");ImageWriter writer = (ImageWriter)iter.next();

File outFile = new File("C:/"+i+".jpg");FileOutputStream out = new FileOutputStream(outFile);

ImageOutputStream outImage = ImageIO.createImageOutputStream(out);writer.setOutput(outImage);

writer.write(new IIOImage(image,null,null));}

doc.close();System.out.println("over");

}

}

您可能还会对下面的文章感兴趣: