
目前市面上存在很多pdf转换成为文档格式的软件和在线转换的服务,但毫无一例外都是要开通会员,不开通会员就会有各种各样的限制,比如加水印、限制转换页数或者说不允许导出等等,那么作为程序猿有没有方式实现pdf转换陈伟各种格式的方法呢?回答是肯定的。下面来看下通过java如何实现对pdf的各种操作
介绍组件PDFBOX在Java开源组件有一个PDFBox,可以针对PDF进行操作,它主要功能有提取pdf文本内容,pdf的合并和拆分,将pdf转换成为图片,将图片转换成为pdf,对pdf进行签名等等。

要实现这些功能,首先要引入pdfbox包,目前最新版本为3.03
<dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.26</version> </dependency>实现图片转pdfpublic ImageToPdfConverter { public static void main(String[] args) { // 图片文件路径列表 List<String> imagePaths = new ArrayList<>(); imagePaths.add("d:\\\\pdf\\1.png"); imagePaths.add("d:\\\\pdf\\2.png"); imagePaths.add("d:\\\\pdf\\3.png"); imagePaths.add("d:\\\\pdf\\4.png"); imagePaths.add("d:\\\\pdf\\5.png"); // 添加更多图片路径... // 输出PDF文件路径 String outputPdfPath = "d:\\\\pdf\\output.pdf"; try { convertImagesToPdf(imagePaths, outputPdfPath); System.out.println("PDF文件生成成功!"); } catch (IOException e) { System.out.println("生成PDF文件时发生错误:" + e.getMessage()); e.printStackTrace(); } } public static void convertImagesToPdf(List<String> imagePaths, String outputPdfPath) throws IOException { // 创建PDF文档 PDDocument document = new PDDocument(); // 遍历图片路径列表 for (String imagePath : imagePaths) { // 加载图片 File imageFile = new File(imagePath); PDImageXObject image = PDImageXObject.createFromFile(imageFile.getAbsolutePath(), document); // 创建新页面 PDPage page = new PDPage(); document.addPage(page); // 在页面上绘制图片,根据图片大小进行自适应 PDPageContentStream contentStream = new PDPageContentStream(document, page); contentStream.drawImage(image, 0, 0, page.getMediaBox().getWidth(), page.getMediaBox().getHeight()); contentStream.close(); } // 保存PDF文件 document.save(outputPdfPath); document.close(); }}主要是用PDImageXObject.createFromFile将图片转换成为Pdf的一个对象,在是用drawImage方法将图片绘制到pdf文件中去。
实现pdf文件的拆分public PdfSplitter { public static void main(String[] args) { // 输入PDF文件路径 String inputPdfPath = "d:\\pdf\\output.pdf"; // 输出文件夹路径 String outputFolderPath = "d:\\pdf\\output"; try { splitPdf(inputPdfPath, outputFolderPath); System.out.println("PDF文件拆分成功!"); } catch (IOException e) { System.out.println("拆分PDF文件时发生错误:" + e.getMessage()); e.printStackTrace(); } } public static void splitPdf(String inputPdfPath, String outputFolderPath) throws IOException { // 加载PDF文档 PDDocument document = PDDocument.load(new File(inputPdfPath)); // 获取PDF文档中的页数 int numPages = document.getNumberOfPages(); // 遍历每一页 for (int i = 0; i < numPages; i++) { // 创建新的PDF文档 PDDocument newDocument = new PDDocument(); // 从原始文档中提取当前页 PDPage page = document.getPage(i); // 将当前页添加到新文档中 newDocument.addPage(page); // 生成新的PDF文件路径 String outputPdfPath = outputFolderPath + "/page_" + (i + 1) + ".pdf"; // 保存新的PDF文件 newDocument.save(outputPdfPath); // 关闭新文档 newDocument.close(); } // 关闭原始文档 document.close(); }}主要思路是通过对原有pdf的文件读取,然后在将pdf页按照新的拆分页面,pdf的合并的思路也是差不多。
实现pdf转word可以使用pdfbox对pdf的内容进行提取,然后在通过poi组件写入word文档中,但这种方式对于开发而然有点繁琐,在这里推荐另外一个组件aspose-pdf,这个组件的功能非常强大,基本可以实现pdf的各种格式文档之间的转换。且代码非常简洁。
1、导入aspose-pdf的jar包
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-pdf</artifactId> <version>22.7</version> </dependency>2、实现代码
public PdfToWord { public static void main(String[] args) throws IOException { pdf2doc("E:\\demo.pdf"); } //pdf转doc public static void pdf2doc(String pdfPath) { long old = System.currentTimeMillis(); try { //新建一个word文档 String wordPath=pdfPath.substring(0,pdfPath.lastIndexOf("."))+".docx"; FileOutputStream os = new FileOutputStream(wordPath); //doc是将要被转化的word文档 Document doc = new Document(pdfPath); //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换 doc.save(os, SaveFormat.DocX); os.close(); //转化用时 long now = System.currentTimeMillis(); System.out.println("Pdf 转 Word 共耗时:" + ((now - old) / 1000.0) + "秒"); } catch (Exception e) { System.out.println("Pdf 转 Word 失败..."); e.printStackTrace(); } }}主要是使用Document的save方法。
当然这个组件如果是未授权版本的话,只能转换几页并且有水印,但是网上也有一些进行破解方式。使用24.9版本亲测有效。详见:https://www.jb51.net/article/248416.htm