java实现word转pdf第二种方法

来源:互联网 发布:局域网建域名 编辑:程序博客网 时间:2024/06/05 10:17

之前说过的第一种方法 在我的项目中出现了问题 生产上就是word转pdf没有汉字显示,只有字母和数字。

因为代码中有选择字体

public int registerDirectories(){
   int i = 0;
       i += registerDirectory("c:/windows/Fonts");
       i += registerDirectory("c:/winnt/fonts");
       i += registerDirectory("d:/windows/fonts");
       i += registerDirectory("d:/winnt/fonts");
       i += registerDirectory("/usr/share/X11/fonts", true);
       i += registerDirectory("/usr/X/lib/X11/fonts", true);
       i += registerDirectory("/usr/openwin/lib/X11/fonts", true);
       i += registerDirectory("/usr/share/fonts", true);
       i += registerDirectory("/usr/X11R6/lib/X11/fonts", true);
       i += registerDirectory("/Library/Fonts");
       i += registerDirectory("/System/Library/Fonts");
       i += registerDirectory(System.getenv("LICENSE_HOME"), true);
       return i;


}
} 找了很多原因没有找出来,程序是没有问题的,开始以为是linux环境中没有安装中文字体的原因,后来安装了字体还是一样,运营同事说可能跟服务器版本有关,现在服务器版本太低了。没有办法呀,就网上找看看有没有其他的方法。于是有这下面这种方法。

直接可以用的,代码如下:

import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.InputStream;  
import java.io.OutputStream;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
   
import org.apache.commons.collections.MapUtils;  
import org.apache.poi.xwpf.converter.pdf.PdfConverter;  
import org.apache.poi.xwpf.converter.pdf.PdfOptions;  
import org.apache.poi.xwpf.usermodel.XWPFDocument;  
import org.apache.poi.xwpf.usermodel.XWPFParagraph;  
import org.apache.poi.xwpf.usermodel.XWPFRun;  
import org.apache.poi.xwpf.usermodel.XWPFTable;  
import org.apache.poi.xwpf.usermodel.XWPFTableCell;  
import org.apache.poi.xwpf.usermodel.XWPFTableRow;  
import org.slf4j.Logger;  
import org.slf4j.LoggerFactory;  
   
import fr.opensagres.xdocreport.utils.StringUtils;  
   
/** 
 * @author Rocca 
 * 
 */  
public class DocxToPdf2 {  
   
    protected static final Logger logger = LoggerFactory.getLogger(DocxToPdf2.class);  
       
    
    public static void main(String[] args) throws Exception{  
        String filepath = "D:/yan.docx";  
        String outpath = "D:/yan.pdf";   
           
        InputStream source = new FileInputStream(filepath);  
        OutputStream target = new FileOutputStream(outpath);  
        Map<String, String> params = new HashMap<String, String>();  
           
           
        PdfOptions options = PdfOptions.create();  
           
        wordConverterToPdf(source, target, options, params);  
    }  
   
    public static void convertPdf(String docxFilePath,String pdfFilePath) throws Exception{
    InputStream source = new FileInputStream(docxFilePath);  
        OutputStream target = new FileOutputStream(pdfFilePath);  
        Map<String, String> params = new HashMap<String, String>();  
           
           
        PdfOptions options = PdfOptions.create();  
           
        wordConverterToPdf(source, target, options, params); 
    }
    /** 
     * 将word文档, 转换成pdf, 中间替换掉变量 
     * @param source 源为word文档, 必须为docx文档 
     * @param target 目标输出 
     * @param params 需要替换的变量 
     * @throws Exception 
     */  
    public static void wordConverterToPdf(InputStream source,  
            OutputStream target, Map<String, String> params) throws Exception {  
        wordConverterToPdf(source, target, null, params);      
    }  
   
    /** 
     * 将word文档, 转换成pdf, 中间替换掉变量 
     * @param source 源为word文档, 必须为docx文档 
     * @param target 目标输出 
     * @param params 需要替换的变量 
     * @param options PdfOptions.create().fontEncoding( "windows-1250" ) 或者其他 
     * @throws Exception 
     */  
    public static void wordConverterToPdf(InputStream source, OutputStream target,   
            PdfOptions options,  
            Map<String, String> params) throws Exception {  
         XWPFDocument doc = new XWPFDocument(source);  
         paragraphReplace(doc.getParagraphs(), params);  
         for (XWPFTable table : doc.getTables()) {  
           for (XWPFTableRow row : table.getRows()) {  
               for (XWPFTableCell cell : row.getTableCells()) {  
                   paragraphReplace(cell.getParagraphs(), params);  
               }  
           }  
       }  
       PdfConverter.getInstance().convert(doc, target, options);  
    }  
       
    /** 替换段落中内容 */  
    private static void paragraphReplace(List<XWPFParagraph> paragraphs, Map<String, String> params) {  
        if (MapUtils.isNotEmpty(params)) {  
            for (XWPFParagraph p : paragraphs){  
                for (XWPFRun r : p.getRuns()){  
                    String content = r.getText(r.getTextPosition());  
                    logger.info(content);  
                    if(StringUtils.isNotEmpty(content) && params.containsKey(content)) {  
                        r.setText(params.get(content), 0);  
                    }  
                }  
            }  
        }  
    }  
       
}