最近在项目中遇到了word转pdf的需求,目前使用过两个方法,一个是openOffice,另一个是使用jacob进行转换。为什么用了两个方法呢,最开始用到openOffice,网上大多推荐也是用这种模式,但是实际中发现部分文档在转换时会出现格式错误问题,就开始另寻方案。用过国产永中office转换,效果也不错,但是要收费,又另寻他法,发现了jacob方法,针对word转pdf,用jacob可保持较高还原度。
openoffice使用方法:
引入maven
<dependency> <groupId>org.openoffice</groupId> <artifactId>unoil</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>juh</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-local</artifactId> <version>4.3.0</version> </dependency> <dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-core</artifactId> <version>4.3.0</version> </dependency> <dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-spring-boot-starter</artifactId> <version>4.3.0</version> </dependency>
额外需要引入一个 jodconverter-2.2.2.jar 文件,可百度下载
方法调用:
private String wordStartToPdf(String wordPath, String pdfPath) { String error = ""; try { //如果是远程调用,使用0.0.0.0。本机调试使用127.0.0.1 OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1",8200); // OpenOfficeConnection connection = new SocketOpenOfficeConnection("0.0.0.0",8100); connection.connect(); File inputFile = new File(wordPath); File outputFile = new File(pdfPath); DocumentConverter converter = new OpenOfficeDocumentConverter(connection); try{ converter.convert(inputFile, outputFile); } catch (Exception e){ error = "文件转换失败1,"+e; log.error("文件转换失败,详情:",e); } } catch (ConnectException e) { error = "文件转换失败,"+e; log.error("文件转换失败,详情:",e); } return error; }
jacob转pdf:
引入 jacob.jar 文件
方法调用:
public class ToPdfController { private static final int wdFormatPDF = 17; // PDF 格式 private static final int xlTypePDF = 0; // xls格式 @GetMapping(value = "/test") public String testHello(){ return "HELLO WORLD"; } @GetMapping("/start") public void startPdf(){ // String old = "C:\\Users\\EDY\\Desktop\\kkfileview\\demo.pptx"; // String old = "C:\\Users\\EDY\\Desktop\\kkfileview\\demo.xlsx"; String old = "C:\\Users\\EDY\\Desktop\\kkfileview\\demo.docx"; String newPdf = "C:\\Users\\EDY\\Desktop\\kkfileview\\new.pdf"; trans(old,newPdf); } private void trans(String sfileName, String toFileName) { System.out.println("------开始转换------"); String suffix = getFileSufix(sfileName); File file = new File(sfileName); if (!file.exists()) { System.out.println("文件不存在!"); } if (suffix.equals("pdf")) { System.out.println("PDF not need to convert!"); } if (suffix.equals("doc") || suffix.equals("docx") || suffix.equals("txt")) { word2PDF(sfileName, toFileName); } else if (suffix.equals("ppt") || suffix.equals("pptx")) { ppt2PDF(sfileName, toFileName); } else if (suffix.equals("xls") || suffix.equals("xlsx")) { excel2PDF(sfileName, toFileName); } else { System.out.println("文件格式不支持转换!"); } } private void ppt2PDF(String srcFilePath, String pdfFilePath) { ActiveXComponent app = null; Dispatch ppt = null; boolean result = true; try { ComThread.InitSTA(); app = new ActiveXComponent("PowerPoint.Application"); Dispatch ppts = app.getProperty("Presentations").toDispatch(); // 因POWER.EXE的发布规则为同步,所以设置为同步发布 ppt = Dispatch.call(ppts, "Open", srcFilePath, true, // ReadOnly true, // Untitled指定文件是否有标题 false// WithWindow指定文件是否可见 ).toDispatch(); Dispatch.call(ppt, "SaveAs", pdfFilePath, 32); // ppSaveAsPDF为特定值32 System.out.println("转换文档到 PDF..." + pdfFilePath); result = true; // set flag true; } catch (ComFailException e) { result = false; } catch (Exception e) { result = false; } finally { if (ppt != null) { Dispatch.call(ppt, "Close"); } if (app != null) { app.invoke("Quit"); } ComThread.Release(); } } private void excel2PDF(String inputFile, String pdfFile) { ActiveXComponent app = null; Dispatch excel = null; boolean result = true; try { app = new ActiveXComponent("Excel.Application"); app.setProperty("Visible", false); Dispatch excels = app.getProperty("Workbooks").toDispatch(); excel = Dispatch.call(excels, "Open", inputFile, false, true).toDispatch(); Dispatch.call(excel, "ExportAsFixedFormat", xlTypePDF, pdfFile); System.out.println("打开文档..." + inputFile); System.out.println("转换文档到 PDF..." + pdfFile); result = true; } catch (Exception e) { result = false; } finally { if (excel != null) { Dispatch.call(excel, "Close"); } if (app != null) { app.invoke("Quit"); } } } private void word2PDF(String sfileName, String toFileName) { long start = System.currentTimeMillis(); ActiveXComponent app = null; Dispatch doc = null; boolean result = true; try { app = new ActiveXComponent("Word.Application"); app.setProperty("Visible", new Variant(false)); Dispatch docs = app.getProperty("Documents").toDispatch(); doc = Dispatch.call(docs, "Open", sfileName).toDispatch(); System.out.println("打开文档..." + sfileName); System.out.println("转换文档到 PDF..." + toFileName); File tofile = new File(toFileName); if (tofile.exists()) { tofile.delete(); } Dispatch.call(doc, "SaveAs", toFileName, wdFormatPDF); long end = System.currentTimeMillis(); System.out.println("转换完成..用时:" + (end - start) + "ms."); result = true; } catch (Exception e) { // TODO: handle exception System.out.println("========Error:文档转换失败:" + e.getMessage()); result = false; } finally { Dispatch.call(doc, "Close", false); System.out.println("关闭文档"); if (app != null) { app.invoke("Quit", new Variant[] {}); } } ComThread.Release(); } private String getFileSufix(String fileName) { int splitIndex = fileName.lastIndexOf("."); return fileName.substring(splitIndex + 1); } }