Java word转pdf

最近遇到需要动态处理word文档事情,使用过一些java的框架,比如Spire.doc免费的有转换页面限制。后面发现了LibeOffice开源项目,一个免费的office办公软件。虽然支持java操作,但是网上资料不全面。为了实现word转pdf。开启百度、谷歌模式。以下内容在window上操作。

参考

  • https://wiki.openoffice.org/wiki/Documentation/DevGuide
  • https://www.libreofficechina.org/python-libreoffice-api-hello-world/
  • https://www.cnblogs.com/qlqwjy/p/9846904.html

读取加转换

  • 1.下载LibeOffice,地址 https://www.libreoffice.org/
  • 2.创建java工程,需要导入LibeOffice 的SDK。在LibeOffice的安装目录xxx\program\classes下
  • 3.开始编码,这里发现读取word的内容的占位符后再修改,原word内容的样式会清空。不建议用这个来动态填写数据。可以使用Apache POI来修改。
import com.sun.star.beans.PropertyValue;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.comp.helper.BootstrapException;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.text.XText;
import com.sun.star.text.XTextDocument;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

/**
 * 
 */
public class ReadDemo {

    public static void main(String[] args) throws Exception {
        // get the remote office component context
        XComponentContext xRemoteContext = Bootstrap.bootstrap();
        if (xRemoteContext == null) {
            System.err.println("ERROR: Could not bootstrap default Office.");
        }

        XMultiComponentFactory xRemoteServiceManager = xRemoteContext.getServiceManager();

        Object desktop = xRemoteServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", xRemoteContext);

        XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktop);

        String fileUrl = "file:///C:/Users/Administrator/Desktop/test.docx";
        PropertyValue[] loadProps = new PropertyValue[0];
        PropertyValue s1 = new PropertyValue();
        s1.Name = "FilterName";
        s1.Value = "MS Word 2007 XML";

        XComponent xWriterComponent = xComponentLoader.loadComponentFromURL(fileUrl, "_blank", 0, loadProps);
        XTextDocument xTextDocument = UnoRuntime.queryInterface(XTextDocument.class,xWriterComponent);
        XText xText = xTextDocument.getText();

        //重新设置内容,不会沿用原模板文字样式
        //String content = xText.getString();
        //xText.setString(content);

        com.sun.star.frame.XStorable xStore = (com.sun.star.frame.XStorable)UnoRuntime.queryInterface (com.sun.star.frame.XStorable.class, xWriterComponent);

        String url = "file:///C:/Users/Administrator/Desktop/test1.pdf";
        PropertyValue[] saveProps = new PropertyValue[1];
        s1.Name = "FilterName";
        s1.Value = "writer_pdf_Export";
        saveProps[0] = s1;
        //保存pdf
        xStore.storeToURL(url,saveProps);

        //保存word
        url = "file:///C:/Users/Administrator/Desktop/test1.docx";
        s1.Name = "FilterName";
        s1.Value = "MS Word 2007 XML";
        saveProps[0] = s1;
        xStore.storeToURL(url,saveProps);

        xWriterComponent.dispose();

    }
}

只转换word文档

在使用LibeOffice SDK修改word文档的占位符后出现样式失效。就尝试使用LibeOffice做PDF转换。这里使用了java来触发命令行操作。其它语言同理
代码如下

/**
 * https://www.cnblogs.com/qlqwjy/p/9846904.html
 */
public class CmdDemo {

    public static void main(String[] args) throws IOException {
        String command = "C:\\Program Files\\LibreOffice\\program\\soffice.bin --headless --convert-to pdf:writer_pdf_Export C:\\Users\\Administrator\\Desktop\\xxx.docx --outdir C:\\Users\\Administrator\\Desktop";
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(command);
        InputStream errorStream = process.getErrorStream();
        InputStream inputStream = process.getInputStream();
        OutputStream outputStream = process.getOutputStream();


        StringBuilder sb = new StringBuilder();
        byte[] bytes1 = new byte[1024];

        while (errorStream.read(bytes1) > -1){
            sb.append(new String(bytes1));
        }

        String s1 = sb.toString();
        sb.substring(0,sb.length());

        while (inputStream.read(bytes1) > -1){
            sb.append(new String(bytes1));
        }

        String s2 = sb.toString();
        sb.substring(0,sb.length());

        System.out.println();
        process.destroy();
        runtime.exit(-1);
    }
}

你可能感兴趣的:(Java word转pdf)