package gcjsxh.util;import java.awt.p_w_picpath.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.util.Iterator;import javax.p_w_picpathio.IIOImage;import javax.p_w_picpathio.ImageIO;import javax.p_w_picpathio.ImageWriter;import javax.p_w_picpathio.stream.ImageOutputStream;import org.icepdf.core.pobjects.Document;import org.icepdf.core.pobjects.Page;import org.icepdf.core.util.GraphicsRenderingHints;public class CapturePdfFirstPageUtil { /** * 生成一本书的缩略图 * @param inputFile 需要生成缩略图的书籍的完整路径 * @param outputFile 生成缩略图的放置路径 */ public static void generateBookIamge(String inputFile, String outputFile) { Document document = null; try { float rotation = 0f; //缩略图显示倍数,1表示不缩放,0.5表示缩小到50% float zoom = 0.8f; document = new Document(); document.setFile(inputFile); // maxPages = document.getPageTree().getNumberOfPages(); BufferedImage p_w_picpath = (BufferedImage)document.getPageImage(0, GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX, rotation, zoom); Iterator iter = ImageIO.getImageWritersBySuffix("jpg"); ImageWriter writer = (ImageWriter)iter.next(); FileOutputStream out = new FileOutputStream(new File(outputFile)); ImageOutputStream outImage = ImageIO.createImageOutputStream(out); writer.setOutput(outImage); writer.write(new IIOImage(p_w_picpath, null, null)); } catch(Exception e) { System.out.println( "to generate thumbnail of a book fail : " + inputFile ); System.out.println( e ); } } public static void main(String[]args){ CapturePdfFirstPageUtil.generateBookIamge("C:\\Users\\work\\Desktop\\spring2.pdf","C:\\Users\\work\\Desktop\\captureImage.jpg") ; } }
icepdf jar包地址:
注意上面引入jar包时不要引错了哦