Java使用icepdf将pdf文件按页转成图片
程序员文章站
2024-03-03 22:05:58
本文实例为大家分享了java使用icepdf将pdf文件按页转成图片的具体代码,供大家参考,具体内容如下
maven icepdf包,这里过滤掉jai-core...
本文实例为大家分享了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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。