前言:
如今兄弟们对“javaice”大致比较注重,各位老铁们都想要分析一些“javaice”的相关知识。那么小编也在网络上收集了一些有关“javaice””的相关知识,希望看官们能喜欢,兄弟们快快来学习一下吧!1、引入jar包:
<dependency> <groupId>org.icepdf.os</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.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!-- 允许加入额外的jar包 --> <includeSystemScope>true</includeSystemScope> </configuration> </plugin> </plugins></build></build>
同时,将引入的jar包指向导入的位置:
<dependency> <groupId>org.icepdf.os</groupId> <artifactId>icepdf-core</artifactId> <version>4.1.1</version> <scope>system</scope> <systemPath>${basedir}/src/main/lib/icepdf-core-4.1.1.jar</systemPath> <exclusions> <exclusion> <groupId>javax.media</groupId> <artifactId>jai-core</artifactId> </exclusion> </exclusions></dependency>
结构如下图:
2、代码实现:
package com.cookie.util;import org.icepdf.core.exceptions.PDFException;import org.icepdf.core.exceptions.PDFSecurityException;import org.icepdf.core.pobjects.Document;import org.icepdf.core.pobjects.Page;import org.icepdf.core.util.GraphicsRenderingHints;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import javax.imageio.IIOImage;import javax.imageio.ImageIO;import javax.imageio.ImageWriter;import javax.imageio.stream.ImageOutputStream;import java.awt.image.BufferedImage;import java.io.*;import java.text.DecimalFormat;import java.util.Iterator;/** * author : cxq * Date : 2019/8/12 */public class Pdf2ImgUtil { private static Logger logger = LoggerFactory.getLogger(Pdf2ImgUtil.class); public static final String FILETYPE_JPG = "jpg"; /** * * 将指定的pdf文件转换为指定路径的图片 * @param filepath 原文件路径,例如d:/test/test.pdf * @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, GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX, rotation, zoom); Iterator iter = ImageIO.getImageWritersBySuffix(FILETYPE_JPG); ImageWriter writer = (ImageWriter) iter.next(); 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); writer.setOutput(outImage); writer.write(new IIOImage(img, null, null)); } System.out.println("转换完成"); } public static void main(String[] args) throws PDFException, PDFSecurityException, IOException { tranfer("d:/1.pdf", "d:/", 1); }}
运行main方法,结果如下图:
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。