生成pdf:
public String savePdf(WordprocessingMLPackage wordMLPackage,Map<String,Object> data) throws Exception {
String pdfDir = getFilePath() + ".pdf";
File file = new File(pdfDir);
FileUtils.createDir(file);
//使用默认的FOSettings settings.setWmlPackage(wmlPackage);settings.setApacheFopMime("application/pdf");
OutputStream os = new java.io.FileOutputStream(file);
Docx4J.toPDF(wordMLPackage, os);
os.flush();
os.close();
if (logger.isDebugEnabled()) {
logger.debug("savepath:"+file.getAbsolutePath());
}
return pdfDir;
}
比如说,生成word的时候,可以指定word的上下左右页边距,但是生成pdf时,如何控制页边距,我却没有找到,隐约觉得FOSettings类里会支持,但始终没有找到。
同理,做word转pdf时,也是不知如何控制pdf的页边距。
word转pdf:
public String doc2pdf(String docxPath, String pdfPath) throws Exception{
OutputStream os = null;
try {
WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(new File(docxPath));
//设置字体
Mapper fontMapper = new IdentityPlusMapper();
String fontFamily = "SimSun";
PhysicalFont simsunFont = PhysicalFonts.get(fontFamily);
fontMapper.put(fontFamily, simsunFont);
mlPackage.setFontMapper(fontMapper);
os = new java.io.FileOutputStream(pdfPath);
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(mlPackage);
Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
}catch(Exception ex){
ex.printStackTrace();
}finally {
os.close();
}
return pdfPath;
}
如果有其他的第三方开源插件,jar可以在mvnrepository找到的,麻烦也推荐一下~~当前项目的word有点复杂
萌新刚刚入住社区,求各路大神回复......
No one knows? .....................What I know about docx4j......................Or docx4j does not know PDF itself Support