Java——pdf首页转图片制作缩略图

public static final String FILETYPE_JPG = "jpg";
/**
     * 将指定pdf文件的首页转换为指定路径的缩略图
     *@param filepath 原文件路径,例如d:/test.pdf
     *@param imagepath 图片生成路径,例如 d:/test-1.jpg
     *@param zoom     缩略图显示倍数,1表示不缩放,0.3则缩小到30%
     */
public void tranfer(String filepath, String imagepath, float zoom) throws IOException, PDFException, PDFSecurityException ,InterruptedException{
    // ICEpdf document class
    Document document = null;
    float rotation = 0f;
    document = new Document();
    document.setFile(filepath);
    // maxPages = document.getPageTree().getNumberOfPages();
    BufferedImage img = (BufferedImage) document.getPageImage(0, GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX, rotation, zoom);
    Iterator iter = ImageIO.getImageWritersBySuffix(FILETYPE_JPG);
    ImageWriter writer = (ImageWriter) iter.next();
    File outFile = new File(imagepath);
    FileOutputStream out = new FileOutputStream(outFile);
    ImageOutputStream outImage = ImageIO.createImageOutputStream(out);
    writer.setOutput(outImage);
    writer.write(new IIOImage(img, null, null));
    System.out.println("pdf转换成功!!!");
}

猜你喜欢

转载自blog.csdn.net/yilia_jia/article/details/82662969
今日推荐