Java 如何将Word文档转换为PDF?
如何将Word文档转换为PDF,其中文档包含各种内容,例如表格。尝试使用iText时,原始文档与转换后的PDF看起来不同。是否有一个开源API/库,而不是调用一个可执行文件,我可以使用它?这是一个相当困难的任务,如果你想要完美的结果(不使用Word是不可能的),就更难了。因此,我相信,在纯Java中为你做这一切的开源API的数量是零(更新:我错了,见下文) 您的基本选择如下:Java 如何将Word文档转换为PDF?,java,pdf,ms-word,Java,Pdf,Ms Word,如何将Word文档转换为PDF,其中文档包含各种内容,例如表格。尝试使用iText时,原始文档与转换后的PDF看起来不同。是否有一个开源API/库,而不是调用一个可执行文件,我可以使用它?这是一个相当困难的任务,如果你想要完美的结果(不使用Word是不可能的),就更难了。因此,我相信,在纯Java中为你做这一切的开源API的数量是零(更新:我错了,见下文) 您的基本选择如下: 使用JNI/a C#web服务/etc脚本MS Office(100%完美结果的唯一选项) 使用可用的API脚本Open
使用ApachePOI读取Word文档,使用iText编写PDF。完全免费,100%Java,但也有一些。我同意将OpenOffice列为具有Java API的word/pdf文档高保真导入/导出工具的海报,它也可以跨平台工作。OpenOffice导入/导出过滤器功能非常强大,在转换为各种格式(包括PDF)的过程中保留了大部分格式。与直接学习OpenOffice API相比,它更容易实现增值。由于UNO API的风格和与崩溃相关的错误,这可能是一项挑战。为此,您可以使用JODConverter。它可用于在不同office格式之间转换文档。例如:
您可以使用Cloudmersive本机Java库。它每月最多可免费转换50000次,并且根据我的经验,它的保真度比其他基于iText或ApachePOI的方法高得多。这些文档实际上看起来与Microsoft Word中的文档一样,而Microsoft Word对我来说是关键。顺便说一句,它还可以将XLSX、PPTX和遗留文档、XLS和PPT转换为PDF 代码如下所示,首先添加导入:
import com.cloudmersive.client.invoker.ApiClient;
import com.cloudmersive.client.invoker.ApiException;
import com.cloudmersive.client.invoker.Configuration;
import com.cloudmersive.client.invoker.auth.*;
import com.cloudmersive.client.ConvertDocumentApi;
然后转换文件:
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
File inputFile = new File("/path/to/input.docx"); // File to perform the operation on.
try {
byte[] result = apiInstance.convertDocumentDocxToPdf(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentDocxToPdf");
e.printStackTrace();
}
您可以从门户网站免费获得一份电子邮件。使用call Office Word是100%完美的解决方案。但它只支持Windows平台,因为需要安装Office Word
public void convertDocx2pdf(String docxFilePath) {
File docxFile = new File(docxFilePath);
String pdfFile = docxFilePath.substring(0, docxFilePath.lastIndexOf(".docx")) + ".pdf";
if (docxFile.exists()) {
if (!docxFile.isDirectory()) {
ActiveXComponent app = null;
long start = System.currentTimeMillis();
try {
ComThread.InitMTA(true);
app = new ActiveXComponent("Word.Application");
Dispatch documents = app.getProperty("Documents").toDispatch();
Dispatch document = Dispatch.call(documents, "Open", docxFilePath, false, true).toDispatch();
File target = new File(pdfFile);
if (target.exists()) {
target.delete();
}
Dispatch.call(document, "SaveAs", pdfFile, 17);
Dispatch.call(document, "Close", false);
long end = System.currentTimeMillis();
logger.info("============Convert Finished:" + (end - start) + "ms");
} catch (Exception e) {
logger.error(e.getLocalizedMessage(), e);
throw new RuntimeException("pdf convert failed.");
} finally {
if (app != null) {
app.invoke("Quit", new Variant[] {});
}
ComThread.Release();
}
}
}
}现在已经是2019年了,我真不敢相信还没有最简单、最方便的方法将Java世界中最流行的Micro$oft Word文档转换为Adobe PDF格式 我几乎尝试了以上提到的每一种方法,我发现满足我需求的最好的也是唯一的方法就是使用OpenOffice或LibreOffice。实际上我不太清楚它们之间的区别,似乎它们都提供了
soffice
命令行
我的要求是:
doc-to-pdf转换器
,但它缺乏维护,上次更新发生在4年前,我不会使用无人维护的解决方案Xdocreport
似乎是一个很有前途的选择,但它只能转换docx
,而不能转换作为命令的doc
二进制文件
Runtime.getRuntime().exec("soffice --convert-to pdf -outdir . /path/some.doc");
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-Internal</artifactId>
<version>8.0.0</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
<version>8.0.0</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-MOXy</artifactId>
<version>8.0.0</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-export-fo</artifactId>
<version>8.0.0</version>
</dependency>
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.docx4j.Docx4J;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
public class DocToPDF {
public static void main(String[] args) {
try {
InputStream templateInputStream = new FileInputStream("D:\\\\Workspace\\\\New\\\\Sample.docx");
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(templateInputStream);
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
String outputfilepath = "D:\\\\Workspace\\\\New\\\\Sample.pdf";
FileOutputStream os = new FileOutputStream(outputfilepath);
Docx4J.toPDF(wordMLPackage,os);
os.flush();
os.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.ToPdfParameterList;
public class WordToPDF {
public static void main(String[] args) {
//Create Document object
Document doc = new Document();
//Load the file from disk.
doc.loadFromFile("Sample.docx");
//create an instance of ToPdfParameterList.
ToPdfParameterList ppl=new ToPdfParameterList();
//embeds full fonts by default when IsEmbeddedAllFonts is set to true.
ppl.isEmbeddedAllFonts(true);
//set setDisableLink to true to remove the hyperlink effect for the result PDF page.
//set setDisableLink to false to preserve the hyperlink effect for the result PDF page.
ppl.setDisableLink(true);
//Set the output image quality as 40% of the original image. 80% is the default setting.
doc.setJPEGQuality(40);
//Save to file.
doc.saveToFile("output/ToPDF.pdf",FileFormat.PDF);
}
}