1、引入jar包:
<dependency> <groupId>org.ice;/groupId> <artifactId>icepdf-core</artifactId> <version>4.1.1</version> <exclusions> <exclusion> <groupId>javax.media</groupId> <artifactId>jai-core</artifactId> </exclusion> </exclusions> </dependency>注意:如果在pom中无法下载对应的jar包,可以在maven中将该包上传到nexus,或者直接引入本地jar包,如果是引入本地jar包,pom中需添加如下配置:
<build> <plugins> <plugin> <groupId>org.;/groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!-- 允许加入额外的jar包 --> <includeSystemScope>true</includeSystemScope> </configuration> </plugin> </plugins> </build> </build>同时,将引入的jar包指向导入的位置:
<dependency> <groupId>org.ice;/groupId> <artifactId>icepdf-core</artifactId> <version>4.1.1</version> <scope>system</scope> <systemPath>${basedir}/src/main/lib;/systemPath> <exclusions> <exclusion> <groupId>javax.media</groupId> <artifactId>jai-core</artifactId> </exclusion> </exclusions> </dependency>结构如下图:
2、代码实现:
package com.cookie.util; import org.ice; import org.ice; import org.ice; import org.ice; import org.ice; import org.; import org.Factory; import javax.imageio.IIOImage; import javax.imageio.ImageIO; import javax.imageio.ImageWriter; import javax.imageio.; import java.awt.image.BufferedImage; import java.io.*; import java.; import java.u; /** * author : cxq * Date : 2019/8/12 */ public class Pdf2ImgUtil { private static Logger logger = LoggerFac); public static final String FILETYPE_JPG = "jpg"; /** * * 将指定的pdf文件转换为指定路径的图片 * @param filepath 原文件路径,例如d:/te * @param imagepath 图片生成路径,例如 d:/test/ * @param zoom 缩略图显示倍数,1表示不缩放,0.3则缩小到30% * */ public static void tranfer(String filepath, String imagepath, float zoom) throws PDFException, PDFSecurityException, IOException { Document document = null; float rotation = 0f; document = new Document(); document.setFile(filepath); int maxPages = document.getPageTree().getNumberOfPages(); for (int i = 0; i < maxPages; i++) { BufferedImage img = (BufferedImage) document.getPageImage(i, Gra, Page.BOUNDARY_CROPBOX, rotation, zoom); Iterator iter = ImageIO.getImageWritersBySuffix(FILETYPE_JPG); ImageWriter writer = (ImageWriter) i(); File outFile = new File(imagepath + new File(filepath).getName() + "_" + new DecimalFormat("0").format(i+1) + "." + FILETYPE_JPG); FileOutputStream out = new FileOutputStream(outFile); ImageOutputStream outImage = ImageIO.createImageOutputStream(out); wri(outImage); wri(new IIOImage(img, null, null)); } Sy("转换完成"); } public static void main(String[] args) throws PDFException, PDFSecurityException, IOException { tranfer("d:/1.pdf", "d:/", 1); } }运行main方法,结果如下图: