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

时间:2013-05-17 09:00:10

标签: 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创建在服务器上使用的不仅仅是五十人。

1 个答案:

答案 0 :(得分:4)

在您的服务器上安装open office。它会将“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();
    }

}