`
lozpeng
  • 浏览: 38824 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

PDFRenderer 生成图片

    博客分类:
  • Java
UP 
阅读更多
将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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics