6

我正在使用 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/ )读取每个文件的内容并将文件重新创建为新的 PDF 页面,但这可能会变得非常昂贵,因为此 PDF 创建用于五十多人的服务器。

4

1 回答 1

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();
    }

}
于 2013-05-17T09:11:38.390 回答