Java Word 转 Pdf

Java Word 转 Pdf

前言:  

     平常项目中遇到需要将word转换为pdf,我们一般利用的是jacob.jar组件,这个组件利用的是微软office组件,但有时项目需要部署到liunx下,liunx中没有office,我们可以利用apache poi实现这一功能。


代码:

[java]  view plain  copy
  1. import java.io.FileInputStream;  
  2. import java.io.FileOutputStream;  
  3. import java.io.InputStream;  
  4. import java.io.OutputStream;  
  5. import java.util.HashMap;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8.    
  9. import org.apache.commons.collections.MapUtils;  
  10. import org.apache.poi.xwpf.converter.pdf.PdfConverter;  
  11. import org.apache.poi.xwpf.converter.pdf.PdfOptions;  
  12. import org.apache.poi.xwpf.usermodel.XWPFDocument;  
  13. import org.apache.poi.xwpf.usermodel.XWPFParagraph;  
  14. import org.apache.poi.xwpf.usermodel.XWPFRun;  
  15. import org.apache.poi.xwpf.usermodel.XWPFTable;  
  16. import org.apache.poi.xwpf.usermodel.XWPFTableCell;  
  17. import org.apache.poi.xwpf.usermodel.XWPFTableRow;  
  18. import org.slf4j.Logger;  
  19. import org.slf4j.LoggerFactory;  
  20.    
  21. import fr.opensagres.xdocreport.utils.StringUtils;  
  22.    
  23. /** 
  24.  * @author Rocca 
  25.  * 
  26.  */  
  27. public class WordPdfUtils {  
  28.    
  29.     protected static final Logger logger = LoggerFactory.getLogger(WordPdfUtils.class);  
  30.        
  31.     public static void main(String[] args) throws Exception{  
  32.         String filepath = "E:/Spring_Security_安全权限管理手册.docx";  
  33.         String outpath = "E:/test.pdf";   
  34.            
  35.         InputStream source = new FileInputStream(filepath);  
  36.         OutputStream target = new FileOutputStream(outpath);  
  37.         Map<String, String> params = new HashMap<String, String>();  
  38.            
  39.            
  40.         PdfOptions options = PdfOptions.create();  
  41.            
  42.         wordConverterToPdf(source, target, options, params);  
  43.     }  
  44.    
  45.     /** 
  46.      * 将word文档, 转换成pdf, 中间替换掉变量 
  47.      * @param source 源为word文档, 必须为docx文档 
  48.      * @param target 目标输出 
  49.      * @param params 需要替换的变量 
  50.      * @throws Exception 
  51.      */  
  52.     public static void wordConverterToPdf(InputStream source,  
  53.             OutputStream target, Map<String, String> params) throws Exception {  
  54.         wordConverterToPdf(source, target, null, params);      
  55.     }  
  56.    
  57.     /** 
  58.      * 将word文档, 转换成pdf, 中间替换掉变量 
  59.      * @param source 源为word文档, 必须为docx文档 
  60.      * @param target 目标输出 
  61.      * @param params 需要替换的变量 
  62.      * @param options PdfOptions.create().fontEncoding( "windows-1250" ) 或者其他 
  63.      * @throws Exception 
  64.      */  
  65.     public static void wordConverterToPdf(InputStream source, OutputStream target,   
  66.             PdfOptions options,  
  67.             Map<String, String> params) throws Exception {  
  68.          XWPFDocument doc = new XWPFDocument(source);  
  69.          paragraphReplace(doc.getParagraphs(), params);  
  70.          for (XWPFTable table : doc.getTables()) {  
  71.            for (XWPFTableRow row : table.getRows()) {  
  72.                for (XWPFTableCell cell : row.getTableCells()) {  
  73.                    paragraphReplace(cell.getParagraphs(), params);  
  74.                }  
  75.            }  
  76.        }  
  77.        PdfConverter.getInstance().convert(doc, target, options);  
  78.     }  
  79.        
  80.     /** 替换段落中内容 */  
  81.     private static void paragraphReplace(List<XWPFParagraph> paragraphs, Map<String, String> params) {  
  82.         if (MapUtils.isNotEmpty(params)) {  
  83.             for (XWPFParagraph p : paragraphs){  
  84.                 for (XWPFRun r : p.getRuns()){  
  85.                     String content = r.getText(r.getTextPosition());  
  86.                     logger.info(content);  
  87.                     if(StringUtils.isNotEmpty(content) && params.containsKey(content)) {  
  88.                         r.setText(params.get(content), 0);  
  89.                     }  
  90.                 }  
  91.             }  
  92.         }  
  93.     }  
  94.        
  95. }  

原博文地址:

http://my.oschina.net/mingpeng/blog/337198。

备注:

   1.word的后缀必须为docx格式。
   2.我在使用中发现有些jar包需要特定版本,代码才能正常运行。

所使用jar包:

apache poi 3.8;com.lowagie.text-2.1.7.jar;commons-collections-3.2.1.jar;fr.opensagres.xdocreport.itext.extension-1.0.4.jar;ooxml-schemas-1.1.jar;org.apache.poi.xwpf.converter.core-1.0.4.jar;org.apache.poi.xwpf.converter.pdf-1.0.4.jar
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值