JAVA中将PDF文件转成图片文件并将背景色设置为透明

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;

public class PdfToImage {

	public static void main(String args[]) {
		
		/*RandomAccessFile类在创建对象时,除了指定文件本身,还需要指定一个mode参数,该参数指定RandomAccessFile的访问模式,该参数有如下四个值:
	    r:以只读方式打开指定文件。如果试图对该RandomAccessFile指定的文件执行写入方法则会抛出IOException
	    rw:以读取、写入方式打开指定文件。如果该文件不存在,则尝试创建文件
	    rws:以读取、写入方式打开指定文件。相对于rw模式,还要求对文件的内容或元数据的每个更新都同步写入到底层存储设备,默认情形下(rw模式下),是使用buffer的,只有cache满的或者使用RandomAccessFile.close()关闭流的时候才真正的写到文件
	    rwd:与rws类似,只是仅对文件的内容同步更新到磁盘,而不修改文件的元数据*/
		RandomAccessFile raf = null;
		try {
			// 源文件路径
			File sourceFile = new File("D:\\Temp\\PDF\\signNew.pdf");
			// 生成文件的路径
			File destinationFile = new File(sourceFile.getPath().replace(".pdf", ""));
			List<String> outPath = new ArrayList<String>();
			raf = new RandomAccessFile(sourceFile, "r");
			FileChannel channel = raf.getChannel();
			ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
			PDFFile pdf = new PDFFile(buf);
			for (int p = 1; p <= pdf.getNumPages(); p++) {
				PDFPage page = pdf.getPage(p);
				int width = 817;
				int height = 618;
				Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
				BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
				Graphics2D g2d  = bufferedImage.createGraphics();
				Image image = page.getImage(width, height, rect, null, true, true);
				g2d.drawImage(image, 0, 0, null);
				//释放对象
				g2d.dispose();
				String destpath = destinationFile + "" + p + ".png";
				File imageFile = new File(destinationFile + "" + p + ".bmp");
				ImageIO.write(bufferedImage, "bmp", imageFile);
				// 一下代码是将生成的图片背景设置为透明
				BufferedImage temp =  ImageIO.read(imageFile);//取得图片
	            int imgHeight = temp.getHeight();//取得图片的长和宽
	            int imgWidth = temp.getWidth();
	            int c = temp.getRGB(3, 3);
	            //新建一个类型支持透明的BufferedImage
	            BufferedImage bi = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_4BYTE_ABGR);
	            for(int i = 0; i < imgWidth; ++i)//把原图片的内容复制到新的图片,同时把背景设为透明
	            {
	                for(int j = 0; j < imgHeight; ++j)
	                {
	                    if(temp.getRGB(i, j) == c)
	                        bi.setRGB(i, j, c & 0x00ffffff);//这里把背景设为透明
	                    else
	                        bi.setRGB(i, j, temp.getRGB(i, j));
	                }
	            }
	            ImageIO.write(bi, "png", new File(destpath));
	            // 把生成图片放进集合
				outPath.add(destpath);
			}
			System.out.println(outPath);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
            if (raf != null) {
                try {
					raf.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
            }
         }
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值