结论:1、iText几乎找不到如何PDF转图片的信息,但能找到获取到PDF里面的图片并保存下来的信息;2、PDF box满大街都是参考代码(下面会附上一个作为参考);3、收费的库使用起来更简单,但就是要收费,比如spire.pdf库,土豪可参考使用。
PDF box参考代码:
1、引用库:
<dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>X.X.XX</version> </dependency>
2、PDF转图片并写到word里面去:
NiceXWPFDocument document = new NiceXWPFDocument(); PDDocument pdfDoc = PDDocument.load(new File(pdfFilePath)); PDFRenderer pdfRenderer = new PDFRenderer(pdfDoc); for (int pageIndex = 0; pageIndex < pdfDoc.getNumberOfPages(); ++pageIndex) { BufferedImage image = pdfRenderer.renderImageWithDPI(pageIndex, 300); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image, "png", baos); baos.flush(); XWPFParagraph provincialInfoPic = document.createParagraph(); XWPFRun provincialInfoPicRun = provincialInfoPic.createRun(); provincialInfoPicRun.addPicture(new ByteArrayInputStream(baos.toByteArray()), Document.PICTURE_TYPE_PNG, "", Units.toEMU(400),Units.toEMU(600)); }