一、汇入PDF处理的包
阿里云仓库搜索icepdf-core依赖,找到合适的版本,汇入pom.xml档案,
<dependency> <groupId>org.icepdf.os</groupId> <artifactId>icepdf-core</artifactId> <version>6.1.2</version> </dependency>
二、PDF转图片存盘
1、读取目标PDF档案;
2、创建Document物件,通过getNumberOfPages方法获取PDF页数;
3、回圈创建BufferedImage物件,读取每页PDF,并以图片流的方式输出,将图片写入指定路径,
import org.icepdf.core.pobjects.Document; import org.icepdf.core.pobjects.Page; import org.icepdf.core.util.GraphicsRenderingHints; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; public class PDFToImageUtil { /*获取pdf文件名*/ public String getPDFName(String filePath){ File fileOrigin = new File(filePath); String fileName = fileOrigin.getName(); fileName = fileName.substring(0,fileName.lastIndexOf(".")); return fileName; } /*pdf转图片*/ public List<String> Pdf2Img(String filePath,String imagePath) throws Exception { String fileName = getPDFName(filePath); File dir = new File(imagePath); if(!dir.exists()){ boolean b = dir.mkdir(); if(!b){ return null; } } Document document = new Document(); document.setFile(filePath); float scale = 2.5f;//缩放比例 float rotation = 0f;//旋转角度 List<String> filePaths = new ArrayList<String>(); for(int i = 0; i<document.getNumberOfPages();i++){ BufferedImage bufferedImage = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX,rotation,scale); try { File file = new File(imagePath+"/"+fileName+new Date().getTime() +".png"); filePaths.add(file.getPath()); ImageIO.write(bufferedImage,"png",file); }catch (IOException e){ e.printStackTrace(); } bufferedImage.flush(); } document.dispose(); return filePaths; } }
4、生成图片后,方法会将图片的全路径存入List并回传,
三、拼接图片
1、获取所有图片路径,回圈遍历图片,用BufferedImage物件存盘;
2、获取每张图片的RGB,用阵列存盘;
3、以每张图片的左上角为起始坐标,根据纵向坐标不断累加的原理,将RGB拼接;
4、回圈完成后,对最终拼接完成的BufferedImage物件写入指定位置生成拼接后的图片,
import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.List; import javax.imageio.ImageIO; public class PDFToMergeImageUtil { public String Pdf2MergeImg(String filePath,String imagePath) throws Exception { PDFToImageUtil pdfToImageUtil = new PDFToImageUtil(); List<String> filePaths = pdfToImageUtil.Pdf2Img(filePath, imagePath); String fileName = pdfToImageUtil.getPDFName(filePath); BufferedImage destImage = null; for(int i =0 ; i < filePaths.size() - 1;i++){ BufferedImage img1 = null; BufferedImage img2 = null; if(i == 0){ img1 = getBufferedImage(filePaths.get(i)); img2 = getBufferedImage(filePaths.get(i+1)); }else{ img1 = destImage; img2 = getBufferedImage(filePaths.get(i+1)); } // 从图片中读取RGB int[] imageArrayOne = new int[img1.getWidth() * img1.getHeight()]; imageArrayOne = img1.getRGB(0, 0, img1.getWidth(), img1.getHeight(), imageArrayOne, 0, img1.getWidth()); // 逐行扫描影像中各个像素的RGB到阵列中 int[] imageArrayTwo = new int[img2.getWidth() * img2.getHeight()]; imageArrayTwo = img2.getRGB(0, 0, img2.getWidth(), img2.getHeight(), imageArrayTwo, 0, img2.getWidth()); destImage = new BufferedImage(img1.getWidth(), img1.getHeight()+img2.getHeight(), BufferedImage.TYPE_INT_RGB); destImage.setRGB(0, 0, img1.getWidth(), img1.getHeight(), imageArrayOne, 0, img1.getWidth()); // 设定上半部分或左半部分的RGB destImage.setRGB(0, img1.getHeight(), img2.getWidth(), img2.getHeight(), imageArrayTwo, 0, img2.getWidth()); // 设定下半部分的RGB } File fileAll = new File(imagePath+"/"+fileName+".jpg"); ImageIO.write(destImage,"png",fileAll); destImage.flush(); return fileAll.getPath(); } /** * @param fileUrl * 档案绝对路径或相对路径 * @return 读取到的快取影像 * @throws IOException * 路径错误或者不存在该档案时抛出IO例外 */ private BufferedImage getBufferedImage(String fileUrl) throws IOException { File f = new File(fileUrl); return ImageIO.read(f); } }
四、测验
import org.jeecg.business.utils.PDFToMergeImageUtil; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.io.IOException; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class Pdf2Image { @Value(value = "${path.imagePath}") private String imagePath; @Test public void test1() throws Exception { PDFToMergeImageUtil pdf = new PDFToMergeImageUtil(); String listImg = pdf.Pdf2MergeImg("D://java//maven.pdf",imagePath); System.out.println(listImg); } }
五、总结
总的来说就是通过BufferedImage的读写及RGB渲染实作,如果PDF档案的页数较多时,可能会出现堆存储器不够的情况,
0 评论