gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:21:54 24 4
gpt4 key购买 nike

我正在使用 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 创建在服务器上被超过五十人使用.

最佳答案

在您的服务器上安装 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();
}

}

关于java - 通过 Apache PDFBox 将 MS Office 文档添加到 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16605211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com