问题描述
我正在寻找一些稳定方法将DOCX文件从MS WORD转换为PDF。从现在开始我已经将OpenOffice作为监听器使用,但它经常挂起。问题是我们遇到许多用户想要同时将SXW,DOCX文件转换为PDF的情况。还有其他可能吗?我尝试了这个网站的例子:
这里是使用docx4j转换的文档,文档中包含一些异常文本。此外,右上角的文字也不见了。
是否还有其他选项可以将docx转换为PDF格式的pdf?
有很多方法可以进行转换
其中一种使用的方法是使用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); //设置你想要的字体
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);
//关闭记录器
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 !!);
这种方法很完美,甚至可以尝试多个DOCX文件。
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).
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.
this one is PDF created with OpenOffice as converter from docx to pdf. Some text is missing "upper right corner"
Is there some other option to convert docx into pdf with Java?
There are lot of methods to do conversionOne of the used method is using POI and 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.
这篇关于使用Java将docx文件转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!