Java --pdf转图片遇到的问题(java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!none)

错误提示:

java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!none
	at sun.awt.image.IntegerInterleavedRaster.getDataElements(IntegerInterleavedRaster.java:219)
	at java.awt.image.BufferedImage.getRGB(BufferedImage.java:988)
	at com.haiyi.Test.changePDFToImage(Test.java:153)
	at com.haiyi.Test.main(Test.java:45)

代码内容(自己写的测试):

	public static void main(String[] args) {		
		List<String> address = new ArrayList<String>();
		address.add(0, "E:\\1.pdf");
		address.add(1, "E:\\2.pdf");
		System.out.println("address:"+address);
		List<String> fileName = new ArrayList<String>();
		fileName.add(0, "1.pdf");
		fileName.add(1, "2.pdf");
		System.out.println("fileName:"+fileName);
		List<String> pngs = new ArrayList<String>();
		try {
			pngs = changePDFToImage(address,fileName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(pngs.size() != 0){
			System.out.println("pngs-----------"+pngs);
			System.out.println("pngs.toString()"+pngs.toString());
		}else{
			System.out.println("none");
		}
	}
	
	/**
	 * 将远程服务器传回来的pdf文件转换成图片
	 * @param address 返回该图片在本地服务器保存的地址
	 * @return
	 * @throws Exception
	 */
	public static List<String> changePDFToImage(List<String> address,List<String> fileName) throws Exception{
		File floder =new File("E:\\images/dzfp");
		//如果文件夹不存在则创建   
		if  (!floder .exists()  && !floder .isDirectory())     
		{      
			floder.mkdir();   
		}
		List<String> path = new ArrayList<String>();
		File dzfpImgPath = new File("E:\\images/dzfp/jpg");
		//如果文件夹不存在则创建   
		if  (!dzfpImgPath .exists()  && !dzfpImgPath .isDirectory())     
		{      
			dzfpImgPath .mkdir();   
		}
        // 总宽度
        int width = 0;
        // 保存一张图片中的RGB数据
        int[] singleImgRGB;
        int shiftHeight = 0;
        //保存每张图片的像素值
        BufferedImage imageResult = null;		
		for(int i = 0;i < address.size();i ++){
			PDDocument doc = null;
			String path1 = "";
			doc = PDDocument.load(new File(floder+ "/" + fileName.get(i)));
			System.out.println(doc.toString());
            PDFRenderer renderer = new PDFRenderer(doc);
            System.out.println(renderer.toString());
			//获取文件名称
			fileName.set(i, fileName.get(i).substring(0,fileName.get(i).length()-4));
            for (int j = 0, len = doc.getNumberOfPages(); j < len; j++) {
                BufferedImage image = renderer.renderImageWithDPI(j, 300);
                int imageHeight = image.getHeight();
                int imageWidth = image.getWidth();
                //计算高度和偏移量
                if (i == 0) {
                    //使用第一张图片宽度;
                    width = imageWidth;
                    //保存每页图片的像素值
                    imageResult = new BufferedImage(width, imageHeight * len, BufferedImage.TYPE_INT_RGB);
                   
                } else {
                    // 计算偏移高度
                    shiftHeight += imageHeight;
                }
                singleImgRGB = image.getRGB(0, shiftHeight, width, imageHeight, null, 0, width);
                // 写入流中
                imageResult.setRGB(0, 0, width, imageHeight, singleImgRGB, 0, width);
				File outFile = new File(dzfpImgPath + "/"+fileName.get(i)+j+".jpg"); 
				path1 ="E:\\images/dzfp/jpg/"+fileName.get(i)+j+".jpg";
				path.add(path1);
                ImageIO.write(imageResult, "jpg", outFile);
            }
            doc.close();
		}
		System.out.println("path:"+path);
		return path;
	}

异常出现在这里:

singleImgRGB = image.getRGB(0, shiftHeight, width, imageHeight, null, 0, width);

shiftHeight 对应查看doc,第2个参数startY,是起始页高
在这里插入图片描述
异常的具体表述:
多张pdf转换,第一张可以正常通过,第二张时出现异常

我理解的是这样子

  • 如果是多页pdf转换时才使用错误示例中的方法,第二张startY的根据第一张图的height(imageHeight)增加。
  • 如果是单页pdf转换,而且是多个单页pdf发转换,无需要将height增加,增加了反而会溢出。

测试了一下得出结论:
这里出现错误的原因是,多个单页的pdf合成图片,所以将startY改为0,即可解决问题。

更正代码:

`singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值