icepdf和pdfbox转pdf文档为图片

icepdf转pdf文档为图片

首先导入icepdf jar包或maven

pdfPath为pdf文件路径、pdfimgpsth为图片保存的路径

    public static void icePdfImg(String pdfPath,String pdfimgpsth,HttpServletRequest request){
        Document document=new Document();
        try{
            document.setFile(pdfPath);
            float scale=1f;
            float rotation=0f;
            for(int i=0;i<document.getNumberOfPages();i++){
                BufferedImage image=(BufferedImage)document.getPageImage(i,
                        GraphicsRenderingHints.SCREEN,Page.BOUNDARY_CROPBOX,
                        rotation,scale);
                RenderedImage rendImage=image;
                File outFile = new File(pdfimgpsth+"/imgpdf-"+i+".png");
                ImageIO.write(rendImage,"png",outFile);
                image.flush();
            }
            document.dispose();
        }
        catch(Exception e){
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

pdfbox 转pdf文档为图片,但是不好用,没有icepdf好用

    public static final float DEFAULT_DPI = 105;
    public static final String DEFAULT_FORMAT = "jpg";

    public static byte[] getBytes(String filePath) {
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }
    
    
    public static void pdfToImage2(String pdfPath,HttpServletRequest request) throws IOException {
        try {
                int width = 0;
                int[] singleImgRGB;
                int shiftHeight = 0;
                BufferedImage imageResult = null;
                PDDocument pdDocument = PDDocument.load(new File(pdfPath));
                PDFRenderer renderer = new PDFRenderer(pdDocument);
                int pageLength = pdDocument.getNumberOfPages();
                int totalCount = pdDocument.getNumberOfPages() / pageLength + 1;
                for (int m = 0; m < totalCount; m++) {
                    for (int i = 0; i < pageLength; i++) {
                        int pageIndex = i + (m * pageLength);
                        if (pageIndex == pdDocument.getNumberOfPages()) {
                            break;
                        }
                        BufferedImage image = renderer.renderImageWithDPI(pageIndex, 96, ImageType.RGB);
                        int imageHeight = image.getHeight();
                        int imageWidth = image.getWidth();
                        if (i == 0) {
                            width = imageWidth;
                            if ((pdDocument.getNumberOfPages() - m * pageLength) < pageLength) {
                                imageResult = new BufferedImage(width, imageHeight * (pdDocument.getNumberOfPages() - m * pageLength), BufferedImage.TYPE_INT_RGB);
                            } else {
                                imageResult = new BufferedImage(width, imageHeight * pageLength, BufferedImage.TYPE_INT_RGB);
                            }
                        } else {
                            shiftHeight += imageHeight;
                        }
                        singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);
                        imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width);
                    }
                    @SuppressWarnings("deprecation")
                    String pt = request.getRealPath("/")+"outpdf.jpg";
                    File outFile = new File(pt);
                    ImageIO.write(imageResult, "jpg", outFile);
                    shiftHeight = 0;
                }
                pdDocument.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

posted @ 2020-01-02 17:50  _Lawrence  阅读(875)  评论(0编辑  收藏  举报