将PDF文档中的页面生成图片,导出。
/**浏览页面,生成页面的图片字节数据
*
* @param fileName 文档名称及路径
* @param page
* @param type
* @return
*/
public byte[] ViewPage(String fileName, int page, String type) {
if(fileName==null||fileName.length()<=0)
return null;
// if(!fileName.endsWith("pdf")||!fileName.endsWith("PDF"))
// return null;
try
{
BitImgType imgType = BitImgType.valueOf(type.toUpperCase());
if(imgType==null)return null;
PDFFile pdfFile = this.getPdfFile(fileName);
if(pdfFile==null)
return null;
PDFPage pdfPage = pdfFile.getPage(page);
if(pdfPage==null)return null;
//get the width and height for the doc at the default zoom
Rectangle rect = new Rectangle(0,0,
(int)pdfPage.getBBox().getWidth(),
(int)pdfPage.getBBox().getHeight());
//generate the image
Image img = pdfPage.getImage(
rect.width, rect.height, //width & height
rect, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
BufferedImage bImg =(BufferedImage)img;
ImageIO.write(bImg, imgType.toString(), baos);
return baos.toByteArray();
}
catch(Exception ex)
{
ex.printStackTrace();
}
return null;
}
/**建立PDF文档读取类
*
* @param filePath PDF文件的路径
* @return null 或者PDFFile instance
*/
private PDFFile getPdfFile(String filePath)
{
try
{
File file = new File(filePath);
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new PDFFile(buf);
return pdffile;
}
catch(Exception ex)
{
ex.printStackTrace();
}
return null;
}
下面的代码从PDFRenderer 的example而来
public class PDFViewer {
public static void setup() throws IOException {
//set up the frame and panel
JFrame frame = new JFrame("PDF Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PagePanel panel = new PagePanel();
frame.add(panel);
frame.pack();
frame.setVisible(true);
//load a pdf from a byte buffer
String urlPath = "E:\\ArcGIS 应用\\ESRI技术白皮书\\ESRIData&Maps9.3.pdf";
File file = new File(urlPath);
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY,
0, channel.size());
PDFFile pdffile = new PDFFile(buf);
// show the first page
PDFPage page = pdffile.getPage(0);
panel.showPage(page);
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
PDFViewer.setup();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}}
- 大小: 104.7 KB
分享到:
相关推荐
web下使用itext renderer生成pdf(pdf带图片,文字可选),机器上如果安装了pdf阅读器就能直接打开,改造一下就能直接将pdf显示到页面,pdf可带图片,并且文字可选
flying-saucer-pdf 生成pdf解决图片问题 解决中文问题
0.使用了 jpedal、pdfbox、PDFRenderer、icepdf,前3个包和代码都有,最后个只有包 1.pdf转图片常用的四种方式,包和代码全有(icepdf只有包),注释也很清晰, 2.是做个老项目时找来的,要对jdk1.5用,所以基本下载...
renderer.createPDF(os); System.out.println("======转换成功!"); os.close(); os.flush(); } public static void main(String[] args) { ItextUtil itextUtil = new ItextUtil(); String html = ""; ...
而目前开源的组件中,Itext的确是一个First Choice,如果各位单纯是做把图片转成PDF或者自己写了Velocity或者FreeMarker模板生成了HTML是非常推荐直接用Itext来进行的。而如果,大家像我这样已经有前人写好了HTML...
PDF格式扫描版,全书分为24章,共817页。2011年1月出版。 全书压缩打包成4部分,这是第3部分 注:本系列图书的第I、II卷再版时均相应改名为《xxx开发实例大全》(基础卷)及(提高卷),但内容基本无变化,需要的童鞋可...
GPU 加速的拜耳模式去马赛克 ...有关重建技术的概述,请参见bayer_viewer_ogl/doc/bayer_renderer.pdf 。 有关实现细节和性能分析,请参阅http://msmolens.github.io/bayer-pattern-demosaicing/ 。