使用pdfbox实现pdf转image

使用soffice的命令把pdf转image的话,默认只能转一页的,这个有点费劲,于是也不打算沿用soffice的方案了,改用pdfbox来实现。

maven

        
            org.apache.pdfbox
            pdfbox
            2.0.4
        

        
            org.apache.pdfbox
            pdfbox-tools
            2.0.4
        

转换

public static List convertToImage(File file) throws IOException {
        PDDocument document = PDDocument.load(file);
        PDFRenderer pdfRenderer = new PDFRenderer(document);
        List bufferedImageList = new ArrayList<>();

        for (int page = 0;page

concat

public static BufferedImage concat(BufferedImage[] images) throws IOException {
        int heightTotal = 0;
        for(int j = 0; j < images.length; j++) {
            heightTotal += images[j].getHeight();
        }

        int heightCurr = 0;
        BufferedImage concatImage = new BufferedImage(images[0].getWidth(), heightTotal, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = concatImage.createGraphics();
        for(int j = 0; j < images.length; j++) {
            g2d.drawImage(images[j], 0, heightCurr, null);
            heightCurr += images[j].getHeight();
        }
        g2d.dispose();

        return concatImage;
}

小结

这样基本就大功告成了,不足的地方是性能太低,有待优化。

你可能感兴趣的:(使用pdfbox实现pdf转image)