美文网首页
Java实现pdf转图片

Java实现pdf转图片

作者: runewbie | 来源:发表于2019-11-17 20:40 被阅读0次

    pdf转图片
    创建一个maven项目
    添加icepdf依赖

    <!-- https://mvnrepository.com/artifact/org.icepdf.os/icepdf-core -->
    <dependency>
          <groupId>org.icepdf.os</groupId>
          <artifactId>icepdf-core</artifactId>
          <version>6.2.2</version>
      </dependency>
    

    代码实现如下:

    import java.awt.image.BufferedImage;
    import java.awt.image.RenderedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import org.icepdf.core.pobjects.Document;
    import org.icepdf.core.util.GraphicsRenderingHints;
    
    /**
     * PDF 转 图片
     */
    public class IcePdf {
        public static void pdf2Pic(String pdfPath, String path) throws Exception {
            Document document = new Document();
            document.setFile(pdfPath);
            //缩放比例
            float scale = 2.5f;
            //旋转角度
            float rotation = 0f;
    
            for (int i = 0; i < document.getNumberOfPages(); i++) {
                BufferedImage image = (BufferedImage)
                        document.getPageImage(i, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
                RenderedImage rendImage = image;
                try {
                    String imgName = i + ".png";
                    System.out.println(imgName);
                    File file = new File(path + imgName);
                    ImageIO.write(rendImage, "png", file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                image.flush();
            }
            document.dispose();
        }
        public static void main(String[] args) throws Exception {
            String filePath = "D:\\Code\\pdf2pic\\src\\main\\resources\\检测报告.pdf";
            pdf2Pic(filePath, "D:\\Code\\pdf2pic\\src\\main\\resources\\");
        }
    }  
    

    相关文章

      网友评论

          本文标题:Java实现pdf转图片

          本文链接:https://www.haomeiwen.com/subject/rgluvctx.html