利用openoffice将doc、docx转为pdf

1. 需要用的软件

   OpenOffice   ,  JodConverter 

2.启动OpenOffice的服务

    我到网上查如何利用OpenOffice进行转码的时候,都是需要先用cmd启动一个soffice服务,启动的命令是:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"。
    但是实际上,对于我的项目,进行转码只是偶尔进行,然而当OpenOffice的转码服务启动以后,该进程(进程名称是soffice.exe)会一直存在,并且大约占100M的内存,感觉非常浪费。于是我就想了一个办法,可以将执行该服务的命令直接在Java代码里面调用,然后当转码完成的时候,直接干掉这个进程。在后面的JAVA代码里面会有解释。
    所以,实际上,这第2步可以直接跳过


3.将JodConverter相关的jar包添加到项目中
    将JodConverter解压缩以后,把lib下面的jar包全部添加到项目中
   注意:安装openoffice


4. 下面就是重点喽,详见Java代码解析

[java]  view plain  copy
  1. package cn;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.IOException;  
  7. import java.io.InputStreamReader;  
  8.   
  9. import com.artofsolving.jodconverter.DocumentConverter;  
  10. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;  
  11. import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;  
  12. import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;  
  13. /** 
  14.  * office转化为pdf 
  15.  * pdf转化为swf文件 
  16.  * @author Administrator 
  17.  * 
  18.  */  
  19. public class Converter {    
  20.       private static String openOfficePath = "E:\\安装软件\\openoffice\\date";  //openoffice软件的安装路径  
  21.   
  22.       
  23.     /**  
  24.      * 将Office文档转换为PDF. 运行该函数需要用到OpenOffice和jodconverter-2.2.2 
  25.      * <pre>  
  26.      * 方法示例:  
  27.      * String sourcePath = "F:\\office\\source.doc";  
  28.      * String destFile = "F:\\pdf\\dest.pdf";  
  29.      * Converter.office2PDF(sourcePath, destFile);  
  30.      * </pre>  
  31.      *   
  32.      * @param sourceFile  
  33.      *            源文件, 绝对路径. 可以是Office2003-2007全部格式的文档, Office2010的没测试. 包括.doc,  
  34.      *            .docx, .xls, .xlsx, .ppt, .pptx等. 示例: F:\\office\\source.doc  
  35.      * @param destFile  
  36.      *            目标文件. 绝对路径. 示例: F:\\pdf\\dest.pdf  
  37.      * @return 操作成功与否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置错误; 如果返回 0,  
  38.      *         则表示操作成功; 返回1, 则表示转换失败  
  39.      */    
  40.     public static int office2PDF(String sourceFile, String destFile) {    
  41.         try {    
  42.             File inputFile = new File(sourceFile);    
  43.             if (!inputFile.exists()) {    
  44.                 return -1;// 找不到源文件, 则返回-1    
  45.             }    
  46.     
  47.             // 如果目标路径不存在, 则新建该路径    
  48.             File outputFile = new File(destFile);    
  49.             if (!outputFile.getParentFile().exists()) {    
  50.                 outputFile.getParentFile().mkdirs();    
  51.             }    
  52.     
  53.             String OpenOffice_HOME = openOfficePath;//这里是OpenOffice的安装目录    
  54.             // 如果从文件中读取的URL地址最后一个字符不是 '\',则添加'\'    
  55.             if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '\\') {    
  56.                 OpenOffice_HOME += "\\";    
  57.             }    
  58.             // 启动OpenOffice的服务    
  59.             String command = OpenOffice_HOME    
  60.                     + "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";    
  61.             Process pro = Runtime.getRuntime().exec(command);    
  62.             // connect to an OpenOffice.org instance running on port 8100    
  63.             OpenOfficeConnection connection = new SocketOpenOfficeConnection(    
  64.                     "127.0.0.1"8100);    
  65.             connection.connect();    
  66.     
  67.             // convert    
  68.             DocumentConverter converter = new OpenOfficeDocumentConverter(    
  69.                     connection);    
  70.             converter.convert(inputFile, outputFile);    
  71.     
  72.             // close the connection    
  73.             connection.disconnect();    
  74.             // 关闭OpenOffice服务的进程    
  75.             pro.destroy();    
  76.     
  77.             return 0;    
  78.         } catch (FileNotFoundException e) {    
  79.             e.printStackTrace();    
  80.             return -1;    
  81.         } catch (IOException e) {    
  82.             e.printStackTrace();    
  83.         }    
  84.     
  85.         return 1;    
  86.     }    
  87.         
  88.     public static void main(String []args) throws Exception {    
  89.           
  90.         String sourcePath = "C:\\Users\\Administrator\\Desktop\\1\\分组情况一览表.xls";   
  91.         String destFile = "C:\\Users\\Administrator\\Desktop\\1\\dest.pdf";   
  92.         int flag = Converter.office2PDF(sourcePath, destFile);   
  93.         if (flag == 1) {  
  94.             System.out.println("转化失败");  
  95.         }else if(flag == 0){  
  96.             System.out.println("转化成功");  
  97.         }else {  
  98.             System.out.println("找不到源文件, 或url.properties配置错误");            
  99.         }  
  100.     }    
  101. }    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在Spring Boot中使用OpenOffice将Word转换PDF可以通过以下步骤实现: 1. 首先,确保已经安装了OpenOffice软件,并且OpenOffice服务已经启动。 2. 在Spring Boot项目的pom.xml文件中添加对OpenOffice JODConverter的依赖: ```xml <dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-spring-boot-starter</artifactId> <version>4.2.2</version> </dependency> ``` 3. 创建一个Controller类,用于接收Word文件并将其转换PDF。可以使用`JodConverter`类来实现转换功能。以下是一个简单的示例: ```java import org.jodconverter.DocumentConverter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @RestController public class ConvertController { @Autowired private DocumentConverter documentConverter; @PostMapping("/convert") public String convertToPdf(@RequestParam("file") MultipartFile file) { try { // 将上传的Word文件转换PDF File inputFile = File.createTempFile("input", ".docx"); file.transferTo(inputFile); File outputFile = File.createTempFile("output", ".pdf"); documentConverter.convert(inputFile).to(outputFile).execute(); // 返回转换后的PDF文件路径 return outputFile.getAbsolutePath(); } catch (Exception e) { e.printStackTrace(); return "转换失败"; } } } ``` 4. 运行Spring Boot应用程序,并使用POST请求将Word文件上传到`/convert`接口。接口将返回转换后的PDF文件路径。 这样,你就可以使用Spring Boot和OpenOffice将Word文件转换PDF了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值