Aspose.word for java 去除水印问题 加执照

来源:互联网 发布:java aqs 编辑:程序博客网 时间:2024/04/30 14:54
  1. public class RepDocTemplate {  
  2.   
  3.     // 默认没有license,会有水印文字  
  4.     private boolean isLicense = false;  
  5.   
  6.     // 初始化日志  
  7.     private static final Logger log = LoggerFactory  
  8.             .getLogger(RepDocTemplate.class);  
  9.   
  10.     /** 
  11.      * 私有构造,用户初始化License 
  12.      */  
  13.     public RepDocTemplate() {  
  14.         InputStream is = RepDocTemplate.class.getClassLoader()  
  15.                 .getResourceAsStream("license.xml");  
  16.         License aposeLic = new License();  
  17.         try {  
  18.             aposeLic.setLicense(is);  
  19.             isLicense = true;  
  20.         } catch (Exception e) {  
  21.             log.error("word模板破解失败!", e);  
  22.         }  
  23.     }  
  24.   
  25.     /** 
  26.      * 替换内容的主要操作 
  27.      *  
  28.      * @param input 
  29.      * @param output 
  30.      * @param datas 
  31.      */  
  32.     public void replaceDocTem(String input, String output,  
  33.             HashMap<String, Object> datas) {  
  34.   
  35.         if (isLicense) {  
  36.             try {  
  37.                 Document doc = new Document(input);  
  38.                 // 遍历要替换的内容  
  39.                 Iterator<String> keys = datas.keySet().iterator();  
  40.                 while (keys.hasNext()) {  
  41.                     String key = keys.next();  
  42.                     String value = String.valueOf(datas.get(key));  
  43.   
  44.                     // 对显示值得修改  
  45.                     if (Tool.isNull(value)) {  
  46.                         value = "";  
  47.                     }  
  48.                     value = value.replace("\r\n"" ");  
  49.   
  50.                     // 要求替换的内容是完全匹配时的替换  
  51.                     doc.getRange().replace("$" + key + "$", value, truefalse);  
  52.                 }  
  53.   
  54.                 // 替换保存后的内容  
  55.                 doc.save(output);  
  56.             } catch (Exception e) {  
  57.                 log.error(e.getMessage(), e);  
  58.             }  
  59.         }  
  60.     }  
  61.   
  62. }