概述
- Aspose.Words是一個(gè)商業(yè).NET類庫(kù),可以使得應(yīng)用程序處理大量的文件任務(wù)挎春。Aspose.Words支持Doc,Docx豆拨,RTF直奋,HTML,OpenDocument施禾,PDF脚线,XPS,EPUB和其他格式弥搞。使用Aspose.Words可以在不使用Microsoft.Word的情況下生成邮绿、修改、轉(zhuǎn)換和打印文檔攀例。
在項(xiàng)目中使用Aspose.Words有以下好處
- 其豐富的功能特性主要有以下4個(gè)方面:
1)格式轉(zhuǎn)換船逮。Aspose.Words具有高質(zhì)量的文件格式轉(zhuǎn)換功能,可以和Doc粤铭,OOXL挖胃,RTF,TXT等格式互相轉(zhuǎn)換。
2)文檔對(duì)象模型酱鸭。通過(guò)豐富的API以編程方式訪問(wèn)所有的文檔元素和格式吗垮,允許創(chuàng)建,修改凹髓,提取烁登,復(fù)制,分割蔚舀,加入防泵,和替換文件內(nèi)容。
3)文件渲染蝗敢。可以在服務(wù)器端轉(zhuǎn)換整個(gè)文檔或者頁(yè)面為PDF足删,XPS寿谴,SWF格式,同樣可以轉(zhuǎn)換文檔頁(yè)面為圖像格式失受,或者.NET Graphics對(duì)象讶泰,這些功能和Microsoft.Word是一樣的。
4)報(bào)表拂到』臼穑可以從對(duì)象或者數(shù)據(jù)源填充模版生成文件。
使用組件
- 用到的jar包
aspose-words-18.5.0718-jdk16.jar - 用到的文件
license.xml
使用域插入替換文件
-
模板中插入域
插入——>文檔部件——>域
-
結(jié)果
- 代碼
// 參數(shù)1 request請(qǐng)求 參數(shù)2 想要替換的數(shù)據(jù)(map中鍵名要和模板文件中域名相同) 參數(shù)3 文件名 根據(jù)自己實(shí)際需求調(diào)整
private File createFile2(HttpServletRequest request,PageData dataMap,String fileName) throws Exception {
//獲取文件的路徑,根據(jù)要讀取文件路徑自己完成不過(guò)多贅述
String path = PathUtil.getClassResources() + "ftl/template/我的模板文件.doc";
File file = new File(path);
//拼接生成文件的路徑
String tpath = request.getServletContext().getRealPath("/uploadFiles/uploadFile/");
File filepath = new File(tpath);
if (!filepath.exists()) {
filepath.mkdirs();
}
//要生成的文件地址 注意如想生成pdf,此處為.pdf后綴,其他文件寫法相同,這里用png舉例
FileOutputStream out1 = new FileOutputStream(tpath + "/" +fileName + ".png");
File targetFile = new File(tpath + "/"+fileName + ".png");
//調(diào)用去水印的方法 讀取license.xml文件
AsposeUtil.setWordsLicense1();
Document document = new Document(new FileInputStream(file));
String text = document.getText();
DocumentBuilder builder = new DocumentBuilder(document);
LinkedHashMap<String, Object> map = new LinkedHashMap(dataMap);
if (map != null && !map.isEmpty()) {
Set set = map.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
String next = String.valueOf(it.next());//獲取模板中的域名
String s = String.valueOf(map.get(next));//通過(guò)域名獲取想要在該域名處替換的數(shù)據(jù)
//向域中添加圖片
if(next.equals("image")){
builder.moveToMergeField(next);
builder.insertImage(new FileInputStream(new File(s)), 80, 80);//設(shè)置圖片的寬高,
}
//普通文本的數(shù)據(jù)替換
if (builder.moveToMergeField(next)) {
builder.write(String.valueOf(s));
}
}
}
//注意SaveFormat的格式要與上面生成格式保持一致
document.save(out1, SaveFormat.PNG);
out1.flush();
out1.close();
return targetFile;
}
使用書(shū)簽插入替換文件
-
模板中插入書(shū)簽
插入——>書(shū)簽
- 代碼與域類似,不做過(guò)多贅述,文章最下方有官方文檔
//跳轉(zhuǎn)到書(shū)簽
builder.moveToBookmark("書(shū)簽名");
格式轉(zhuǎn)換
- License驗(yàn)證去水印
private static InputStream inputStream = null;
//當(dāng)前創(chuàng)建工具類名AsposeUtil
private static Logger logger = Logger.getLogger(AsposeUtil.class);
/**
* 獲取License的輸入流
*
* @return
*/
private static InputStream getLicenseInput() {
if (inputStream == null) {
ClassLoader contextClassLoader =AsposeUtil.class.getClassLoader();
try {
inputStream =contextClassLoader.getResourceAsStream("license.xml");
} catch (Exception e) {
logger.error("license not found!", e);
}
}
return inputStream;
}
public static boolean setWordsLicense() {
InputStream licenseInput = getLicenseInput();
if (licenseInput != null) {
try {
com.aspose.words.License aposeLic = new com.aspose.words.License();
// 倆種都可以,任選其一,這里用InputStream
// aposeLic.setLicense("這里是你license.xml的路徑");
aposeLic.setLicense(licenseInput);
return aposeLic.getIsLicensed();
} catch (Exception e) {
logger.error("set words license error!", e);
}
}
return false;
}
結(jié)束語(yǔ)
- 暫時(shí)我用到只有這些,如果有不足之處希望各位大手批評(píng)指正兄旬。
- 有需要word文檔需要表格的有關(guān)操作可以參考:http://www.cnblogs.com/wuhuacong/archive/2012/08/30/2662961.html
- 官方文檔地址:https://docs.aspose.com/display/wordsnet/Converting+a+Document
- 相關(guān)文件
https://pan.baidu.com/s/1nMnJsyxz57Bjri7QH5uTkw
提取碼:f7py