温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

雪山飞鹄

温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

BlogJava 首页 新随笔 联系 聚合 管理
  215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
        近期在项目中使用到了大量的报表开发,需要将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 at
 *
 *      
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.image.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<pages.size();i++){
            PDPage page 
= (PDPage)pages.get(i); 
            BufferedImage image 
= 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"); 
    }


}


        
posted on 2010-07-23 08:46 雪山飞鹄 阅读(11303) 评论(7)  编辑  收藏 所属分类: javase

Feedback

# re: 轻松使用apache pdfbox将pdf文件生成图片 2010-07-23 09:25 fengzl
html转pdf word excel和图片不是那么容易的吧  回复  更多评论
  

# re: 轻松使用apache pdfbox将pdf文件生成图片 2010-07-23 13:19 cxh8318
对于中文的pdf支持是脆弱点吗?我看压根就不支持嘛  回复  更多评论
  

# re: 轻松使用apache pdfbox将pdf文件生成图片 2010-07-23 13:31 雪山飞鹄
@cxh8318
我说过了,对于中文pdf目前那是相当的脆弱,网上搜了,说是这是源代码的bug,期待下一个版本能够改进过来,但对英文版的pdf支持绝对完美,我在测试的时候将英文版的spring参考手册全部顺利生成了jpg图片,虽然控制台有警告但不碍事的。比一般的工具软件强悍多了。  回复  更多评论
  

# re: 轻松使用apache pdfbox将pdf文件生成图片 2010-07-23 20:19 cxh8318
恩,对英文pdf转换确实很强  回复  更多评论
  

# re: 轻松使用apache pdfbox将pdf文件生成图片[未登录] 2010-07-26 09:21 conjs
我可以转,HTML DOC,XLS,PPT,PDF,JPG 都可以转  回复  更多评论
  

# re: 轻松使用apache pdfbox将pdf文件生成图片 2013-05-27 18:28 acmersch
内存溢出是什么问题  回复  更多评论
  

# re: 轻松使用apache pdfbox将pdf文件生成图片[未登录] 2014-07-24 17:21 h
姓名,,转出来变成 姓姓,其他也是这样。。重复第一个字  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: