这篇文章主要为大家详细介绍了如何在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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。