icepdf和pdfbox转pdf文档为图片

Posted -llf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
            }
        }

以上是关于icepdf和pdfbox转pdf文档为图片的主要内容,如果未能解决你的问题,请参考以下文章

pdfboxicepdf工具实现pdf转图片

用pdfbox或icepdf转换PDF为图片时,中文乱码问题

pdfbox或icepdf转换PDF为图片时,中文乱码处理

使用pdfbox分页保存pdf为图片

(转)PDF转图片工具类

icepdf将PDF电子发票转换成PNG图片出现金额错行(java)?