码迷,mamicode.com
首页 > 其他好文 > 详细

icepdf和pdfbox转pdf文档为图片

时间:2020-01-02 18:49:39      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:close   The   图片   hint   +=   warnings   _for   sre   system   

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();
            }
        }

icepdf和pdfbox转pdf文档为图片

标签:close   The   图片   hint   +=   warnings   _for   sre   system   

原文地址:https://www.cnblogs.com/-llf/p/12134299.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!