序
使用soffice的命令把pdf转image的话,默认只能转一页的,这个有点费劲,于是也不打算沿用soffice的方案了,改用pdfbox来实现。
maven
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-tools</artifactId>
<version>2.0.4</version>
</dependency>
转换
public static List<BufferedImage> convertToImage(File file) throws IOException {
PDDocument document = PDDocument.load(file);
PDFRenderer pdfRenderer = new PDFRenderer(document);
List<BufferedImage> bufferedImageList = new ArrayList<>();
for (int page = 0;page<document.getNumberOfPages();page++){
BufferedImage img = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
bufferedImageList.add(img);
}
document.close();
return bufferedImageList;
}
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;
}
小结
这样基本就大功告成了,不足的地方是性能太低,有待优化。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。