Java使用icepdf将pdf文件按页转成图片
作者:天外游心
这篇文章主要为大家详细介绍了Java使用icepdf将pdf文件按页转成图片,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Java使用icepdf将pdf文件按页转成图片的具体代码,供大家参考,具体内容如下
Maven icepdf包,这里过滤掉jai-core
<dependency> <groupId>org.icepdf.os</groupId> <artifactId>icepdf-core</artifactId> <version>6.1.2</version> <exclusions> <exclusion> <groupId>javax.media</groupId> <artifactId>jai-core</artifactId> </exclusion> </exclusions> </dependency>
代码如下
import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.imageio.IIOImage; import javax.imageio.ImageIO; import javax.imageio.ImageWriter; import javax.imageio.stream.ImageOutputStream; import org.apache.commons.logging.LogFactory; 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; public class test20160929 { 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("000").format(i) + "." + 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:/test/test.pdf", "d:/test/", 1); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。