Docx4j的Word转PDF及下载PDF
2016-05-05 11:30
489 查看
现将html页面转为word文档在转为PDF(预览)或PDF文件下载
Word转PDF
/**
* docx文档转换为PDF
*
* @param docx docx文档
* @param pdfPath PDF文档存储路径
* @throws Exception 可能为Docx4JException, FileNotFoundException, IOException等
*/
public static void convertDocxToPDF(String docxPath, String pdfPath) throws Exception {
OutputStream os = null;
try {
WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(new File(docxPath));
Mapper fontMapper = new IdentityPlusMapper();
fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));
fontMapper.put("隶书", PhysicalFonts.get("LiSu"));
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 {
IOUtils.closeQuietly(os);
}
}
public void pdf(WordprocessingMLPackage wordMLPackage,OutputStream os)
throws Exception {
String regex = null;
FieldUpdater updater = new FieldUpdater(wordMLPackage);
updater.update(true);
// Set up font mapper (optional)
Mapper fontMapper = new IdentityPlusMapper();
wordMLPackage.setFontMapper(fontMapper);
PhysicalFont font
= PhysicalFonts.get("Arial Unicode MS");
// FO exporter setup (required)
// .. the FOSettings object
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(wordMLPackage);
// Document format:
// The default implementation of the FORenderer that uses Apache Fop will output
// a PDF document if nothing is passed via
// foSettings.setApacheFopMime(apacheFopMime)
// apacheFopMime can be any of the output formats defined in org.apache.fop.apps.MimeConstants eg org.apache.fop.apps.MimeConstants.MIME_FOP_IF or
// FOSettings.INTERNAL_FO_MIME if you want the fo document as the result.
//foSettings.setApacheFopMime(FOSettings.INTERNAL_FO_MIME);
// Specify whether PDF export uses XSLT or not to create the FO
// (XSLT takes longer, but is more complete).
// Don't care what type of exporter you use
Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
// Prefer the exporter, that uses a xsl transformation
// Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
// Prefer the exporter, that doesn't use a xsl transformation (= uses a visitor)
// .. faster, but not yet at feature parity
// Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_NONXSL);
// Clean up, so any ObfuscatedFontPart temp files can be deleted
// if (wordMLPackage.getMainDocumentPart().getFontTablePart()!=null) {
// wordMLPackage.getMainDocumentPart().getFontTablePart().deleteEmbeddedFontTempFiles();
// }
// This would also do it, via finalize() methods
updater = null;
foSettings = null;
wordMLPackage = null;
}
下载PDF
public void pdfFile(WordprocessingMLPackage wordMLPackage,OutputStream os)
throws Exception {
// Font regex (optional)
// Set regex if you want to restrict to some defined subset of fonts
// Here we have to do this before calling createContent,
// since that discovers fonts
String regex = null;
/* // Refresh the values of DOCPROPERTY fields
FieldUpdater updater = new FieldUpdater(wordMLPackage);
updater.update(true);*/
// Set up font mapper (optional)
Mapper fontMapper = new IdentityPlusMapper();
wordMLPackage.setFontMapper(fontMapper);
// .. example of mapping font Times New Roman which doesn't have certain Arabic glyphs
// eg Glyph "ي" (0x64a, afii57450) not available in font "TimesNewRomanPS-ItalicMT".
// eg Glyph "ج" (0x62c, afii57420) not available in font "TimesNewRomanPS-ItalicMT".
// to a font which does
PhysicalFont font
= PhysicalFonts.get("Arial Unicode MS");
String directory = "D:\\upload\\pdf";
String fileName = "OUT_ConvertInXHTMLURL.pdf";
File f = new File(directory,fileName);
if(f.exists()) {
// 文件已经存在,输出文件的相关信息
System.out.println(f.getAbsolutePath());
System.out.println(f.getName());
System.out.println(f.length());
} else {
// 先创建文件所在的目录
f.getParentFile().mkdirs();
}
File file = new File(directory+"/"+fileName);
OutputStream os34 = new java.io.FileOutputStream(file);
Docx4J.toPDF(wordMLPackage, os34);
os.flush();
os.close();
/* updater = null;*/
wordMLPackage = null;
}
Word转PDF
/**
* docx文档转换为PDF
*
* @param docx docx文档
* @param pdfPath PDF文档存储路径
* @throws Exception 可能为Docx4JException, FileNotFoundException, IOException等
*/
public static void convertDocxToPDF(String docxPath, String pdfPath) throws Exception {
OutputStream os = null;
try {
WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(new File(docxPath));
Mapper fontMapper = new IdentityPlusMapper();
fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));
fontMapper.put("隶书", PhysicalFonts.get("LiSu"));
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 {
IOUtils.closeQuietly(os);
}
}
public void pdf(WordprocessingMLPackage wordMLPackage,OutputStream os)
throws Exception {
String regex = null;
FieldUpdater updater = new FieldUpdater(wordMLPackage);
updater.update(true);
// Set up font mapper (optional)
Mapper fontMapper = new IdentityPlusMapper();
wordMLPackage.setFontMapper(fontMapper);
PhysicalFont font
= PhysicalFonts.get("Arial Unicode MS");
// FO exporter setup (required)
// .. the FOSettings object
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(wordMLPackage);
// Document format:
// The default implementation of the FORenderer that uses Apache Fop will output
// a PDF document if nothing is passed via
// foSettings.setApacheFopMime(apacheFopMime)
// apacheFopMime can be any of the output formats defined in org.apache.fop.apps.MimeConstants eg org.apache.fop.apps.MimeConstants.MIME_FOP_IF or
// FOSettings.INTERNAL_FO_MIME if you want the fo document as the result.
//foSettings.setApacheFopMime(FOSettings.INTERNAL_FO_MIME);
// Specify whether PDF export uses XSLT or not to create the FO
// (XSLT takes longer, but is more complete).
// Don't care what type of exporter you use
Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
// Prefer the exporter, that uses a xsl transformation
// Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
// Prefer the exporter, that doesn't use a xsl transformation (= uses a visitor)
// .. faster, but not yet at feature parity
// Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_NONXSL);
// Clean up, so any ObfuscatedFontPart temp files can be deleted
// if (wordMLPackage.getMainDocumentPart().getFontTablePart()!=null) {
// wordMLPackage.getMainDocumentPart().getFontTablePart().deleteEmbeddedFontTempFiles();
// }
// This would also do it, via finalize() methods
updater = null;
foSettings = null;
wordMLPackage = null;
}
下载PDF
public void pdfFile(WordprocessingMLPackage wordMLPackage,OutputStream os)
throws Exception {
// Font regex (optional)
// Set regex if you want to restrict to some defined subset of fonts
// Here we have to do this before calling createContent,
// since that discovers fonts
String regex = null;
/* // Refresh the values of DOCPROPERTY fields
FieldUpdater updater = new FieldUpdater(wordMLPackage);
updater.update(true);*/
// Set up font mapper (optional)
Mapper fontMapper = new IdentityPlusMapper();
wordMLPackage.setFontMapper(fontMapper);
// .. example of mapping font Times New Roman which doesn't have certain Arabic glyphs
// eg Glyph "ي" (0x64a, afii57450) not available in font "TimesNewRomanPS-ItalicMT".
// eg Glyph "ج" (0x62c, afii57420) not available in font "TimesNewRomanPS-ItalicMT".
// to a font which does
PhysicalFont font
= PhysicalFonts.get("Arial Unicode MS");
String directory = "D:\\upload\\pdf";
String fileName = "OUT_ConvertInXHTMLURL.pdf";
File f = new File(directory,fileName);
if(f.exists()) {
// 文件已经存在,输出文件的相关信息
System.out.println(f.getAbsolutePath());
System.out.println(f.getName());
System.out.println(f.length());
} else {
// 先创建文件所在的目录
f.getParentFile().mkdirs();
}
File file = new File(directory+"/"+fileName);
OutputStream os34 = new java.io.FileOutputStream(file);
Docx4J.toPDF(wordMLPackage, os34);
os.flush();
os.close();
/* updater = null;*/
wordMLPackage = null;
}
相关文章推荐
- GitHub入门之一:使用github下载项目
- Plug-in "net.sourceforge.jode" was unable to instantiate class "net.sourcefo
- 09-排序3 Insertion or Heap Sort
- Fragment与Activity相互传递数据:
- TreeMap--左旋右旋
- jAVA EE里什么是链式调用
- mercurial(hg)使用
- C#中实现并发的几种方法的性能测试
- 浅谈 Swift 中的泛型
- 实例讲解Linux中cd命令切换目录的使用技巧
- centos 7 红帽认证 sshd服务搭建
- ajax+php数据增加查询获取删除
- HDU 1194 Beat the Spread! 【水题+细心】
- linux编程获取本机IP地址的三种方法
- vSphere 6.0 部署OVF模板失败,解决方法
- Java同步——可见性和原子性
- c++实验5—求和
- 设计模式之外观模式
- Extjs插入图片
- [No00008B]远程桌面发送“Ctrl+Alt+Delete”组合键调用任务管理器