使用 Java 将 docx 文件转换为 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41118633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert docx file into PDF with Java
提问by Ferguson
I'am looking for some "stable" method to convert DOCX file from MS WORD into PDF. Since now I have used OpenOffice installed as listener but it often hangs. The problem is that we have situations when many users want to convert SXW,DOCX files into PDF at the same time. Is there some other possibility? I tryed with examples from this site: https://angelozerr.wordpress.com/2012/12/06/how-to-convert-docxodt-to-pdfhtml-with-java/but the output result is not good (converted documents have errors and layout is quite modified).
我正在寻找一些“稳定”的方法来将 DOCX 文件从 MS WORD 转换为 PDF。从现在起我使用 OpenOffice 安装作为侦听器,但它经常挂起。问题是我们有很多用户想同时将 SXW、DOCX 文件转换为 PDF 的情况。还有其他可能吗?我尝试了这个网站的例子:https: //angelozerr.wordpress.com/2012/12/06/how-to-convert-docxodt-to-pdfhtml-with-java/但输出结果不好(转换文件有错误,布局相当修改)。
here is "source" docx document:
here is document converted with docx4j with some exception text inside document. Also the text in upper right corner is missing.
这是使用 docx4j 转换的文档,文档中有一些异常文本。右上角的文字也不见了。
this one is PDF created with OpenOffice as converter from docx to pdf. Some text is missing "upper right corner"
这是使用 OpenOffice 创建的 PDF 作为从 docx 到 pdf 的转换器。有些文字缺少“右上角”
Is there some other option to convert docx into pdf with Java?
是否有其他选项可以使用 Java 将 docx 转换为 pdf?
回答by KishanCS
There are lot of methods to do conversion One of the used method is using POI and DOCX4j
有很多方法可以进行转换 使用的方法之一是使用 POI 和 DOCX4j
InputStream is = new FileInputStream(new File("your Docx PAth"));
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.load(is);
List sections = wordMLPackage.getDocumentModel().getSections();
for (int i = 0; i < sections.size(); i++) {
wordMLPackage.getDocumentModel().getSections().get(i)
.getPageDimensions();
}
Mapper fontMapper = new IdentityPlusMapper();
PhysicalFont font = PhysicalFonts.getPhysicalFonts().get(
"Comic Sans MS");//set your desired font
fontMapper.getFontMappings().put("Algerian", font);
wordMLPackage.setFontMapper(fontMapper);
PdfSettings pdfSettings = new PdfSettings();
org.docx4j.convert.out.pdf.PdfConversion conversion = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(
wordMLPackage);
//To turn off logger
List<Logger> loggers = Collections.<Logger> list(LogManager
.getCurrentLoggers());
loggers.add(LogManager.getRootLogger());
for (Logger logger : loggers) {
logger.setLevel(Level.OFF);
}
OutputStream out = new FileOutputStream(new File("Your OutPut PDF path"));
conversion.output(out, pdfSettings);
System.out.println("DONE!!");
This works perfect and even tried on multiple DOCX files.
这工作完美,甚至尝试了多个 DOCX 文件。