利用LibreOffice转换ppt、doc转化pdf

利用LibreOffice转换ppt、doc转化pdf

 

LibreOffice下载地址:   http://www.libreoffice.org/download/libreoffice-fresh/

jodconverter jar包下载:http://sourceforge.net/projects/jodconverter/files/JODConverter/

 

 

 

import java.io.File;  
    import java.io.FileNotFoundException;  
    import java.io.IOException;  
    import java.net.ConnectException;  
    import java.util.Date;  
      
    import com.artofsolving.jodconverter.DocumentConverter;  
    import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;  
    import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;  
    import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;  
      
    public class Office2PDF {  
      
   
        public static int office2PDF(String sourceFile, String destFile) {  
          
            OpenOfficeConnection connection = null;  
            try {  
                File inputFile = new File(sourceFile);  
                if (!inputFile.exists()) {  
                    return -1;//文件不存在  
                }  
      
                //文件夹不存在创建目录  
                File outputFile = new File(destFile);  
                if (!outputFile.getParentFile().exists()) {  
                    outputFile.getParentFile().mkdirs();  
                }  
      
               
                // connect to an OpenOffice.org instance running on port 8100  
                connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);  
                connection.connect();  
      
                // convert  
                DocumentConverter converter = new OpenOfficeDocumentConverter(  
                        connection);  
                converter.convert(inputFile, outputFile);  
      
                return 0;  
 
            } catch (ConnectException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }finally{  
                if(connection!=null){  
                    // close the connection  
                    connection.disconnect();  
                }                
            }  
      
            return 1;  
        }  
          
        public static void main(String[] args){  
            Date startDate = new Date();  
            String sourceFile = "d:\\code\\alipay.ppt";  
            String destFile = "d:\\code\\change2.pdf";  
            System.out.println(Office2PDF.office2PDF(sourceFile, destFile));  
            Date endDate = new Date();  
            System.out.println("the cost time is "+(endDate.getTime()-startDate.getTime()));  
        }  
    }      

 

先运行

soffice --headless --accept="socket,host=127.0.0.1,port=8100;urp;"

soffice 在LibreOffice安装目录下

 

如果出现下面的异常

 com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException: conversion failed: could not load input document

尽量使用最新的LibreOffice

 

转载于:https://www.cnblogs.com/vc60er/p/4045586.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用LibreOfficeJava API来实现将LibreOffice文档转换PDF文档。以下是实现的步骤: 1. 首先需要确保LibreOffice已经安装在系统中,并且已经配置好了环境变量。同时需要下载并安装LibreOfficeJava API。 2. 在Java程序中引入LibreOfficeJava API所在的jar包。 3. 使用以下代码将文档转换PDF文档: ``` import com.sun.star.beans.PropertyValue; import com.sun.star.frame.XComponentLoader; import com.sun.star.lang.XComponent; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XComponentContext; public class ConvertToPDF { public static void main(String[] args) { XComponentContext xContext = null; try { //获取LibreOffice的上下文 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); XComponentLoader xLoader = UnoRuntime.queryInterface(XComponentLoader.class, xContext.getServiceManager().createInstanceWithContext("com.sun.star.frame.Desktop", xContext)); //设置待转换文档的路径和文件名 String fileToConvert = "file:///C:/example.docx"; //设置转换后的PDF文档的路径和文件名 String pdfFile = "file:///C:/example.pdf"; //设置转换参数 PropertyValue[] conversionProperties = new PropertyValue[2]; conversionProperties[0] = new PropertyValue(); conversionProperties[0].Name = "Hidden"; conversionProperties[0].Value = Boolean.TRUE; conversionProperties[1] = new PropertyValue(); conversionProperties[1].Name = "FilterName"; conversionProperties[1].Value = "writer_pdf_Export"; //打开待转换文档 XComponent xComponent = xLoader.loadComponentFromURL(fileToConvert, "_blank", 0, conversionProperties); //将文档转换PDF格式 PropertyValue[] storeProperties = new PropertyValue[3]; storeProperties[0] = new PropertyValue(); storeProperties[0].Name = "FilterName"; storeProperties[0].Value = "writer_pdf_Export"; storeProperties[1] = new PropertyValue(); storeProperties[1].Name = "Overwrite"; storeProperties[1].Value = Boolean.TRUE; storeProperties[2] = new PropertyValue(); storeProperties[2].Name = "Hidden"; storeProperties[2].Value = Boolean.TRUE; UnoRuntime.queryInterface(XStorable.class, xComponent).storeToURL(pdfFile, storeProperties); //关闭文档 UnoRuntime.queryInterface(XCloseable.class, xComponent).close(Boolean.TRUE); } catch (Exception e) { e.printStackTrace(); } finally { if (xContext != null) { com.sun.star.uno.Runtime.getRuntime(xContext).freeUnusedLibraries(); } } } } ``` 4. 运行程序即可将LibreOffice文档转换PDF文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值