一、需要3个jar包。
aspose-words-15.8.0.jar itext2_rq.jar report4.jar
二、一个xml文件去水印用,该文件放在resources路径下
License.xml
<License>
<Data>
<Products>
<Product>Aspose.Total for Java</Product>
<Product>Aspose.Words for Java</Product>
</Products>
<EditionType>Enterprise</EditionType>
<SubscriptionExpiry>20991231</SubscriptionExpiry>
<LicenseExpiry>20991231</LicenseExpiry>
<SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
</Data>
<Signature>
sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=
</Signature>
</License>
三、上代码
package com.example.demo.controller;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
* @author lq
* @ 2021-07-19
*/
@Controller
@RequestMapping("/interFilePost")
public class WordToPdfNew {
public static void main(String[] args) {
try {
doc22pdf("F:\\a.doc", "F:\\a.pdf");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 去除水印
private static void getLicense() {
try (InputStream is = WordToPdfNew.class.getClassLoader().getResourceAsStream("License.xml")) {
License license = new License();
license.setLicense(is);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void doc22pdf(String inPath, String outPath) {
// 验证License 若不验证则转化出的pdf文档会有水印产生
getLicense();
try {
long old = System.currentTimeMillis();
// 新建一个空白pdf文档
File file = new File(outPath);
FileOutputStream os = new FileOutputStream(file);
// Address是将要被转化的word文档
Document doc = new Document(inPath);
// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
doc.save(os, SaveFormat.PDF);
long now = System.currentTimeMillis();
System.out.println(outPath+"生成共耗时:" + ((now - old) / 1000.0) + "秒");
} catch (Exception e) {
e.printStackTrace();
// throw new BDException("word转pdf失败");
}
}
}