通过Apache PDFBox将MS Office文档添加到PDF

Mir*_*rco 6 java apache pdf ms-office

我正在使用Apache PDFBox(http://pdfbox.apache.org/)从任意数量的文件(包括图像和其他PDF)创建PDF.现在我需要将MS Office文档(Word,Excel和Outlook MSG)添加到PDF.这些文件几乎可以包含每个Office版本,因此不会授予该文件是新的office文件(例如docx)或旧文件(例如doc).

有没有办法只用免费工具做到这一点?我的第一个想法是使用Apache POI(http://poi.apache.org/)读取每个文件的contnet,并将该文件重新创建为新的PDF页面,但这可能会变得非常昂贵,因为此PDF创建用于超过五十人的服务器.

KhA*_*aAb 4

在您的服务器上安装开放式办公室。它将把“docx,doc”文档转换为“.pdf”。

package naveed.workingfiles;

import java.io.*;
import com.artofsolving.jodconverter.openoffice.connection.*;
import com.artofsolving.jodconverter.openoffice.converter.*;
import com.artofsolving.jodconverter.*;

public class DocToPdf {

    public static void main(String[] args) throws Exception {

        //Creating the instance of OpenOfficeConnection and 
        //passing the port number to SocketOpenOfficeConnection constructor 
        OpenOfficeConnection con = new SocketOpenOfficeConnection(8100);

        //making the connection with openoffice server
        con.connect();

        // making the object of doc file and pdf file
        File inFile = new File("sample.docx");

        //this is the final converted pdf file
        File outFile = new File("sample.pdf");

        //making the instance 
        DocumentConverter converter = new OpenOfficeDocumentConverter(con);

        //passing both files objects
        converter.convert(inFile, outFile);

        con.disconnect();
    }

}
Run Code Online (Sandbox Code Playgroud)