需要的依赖和仓库地址
e-iceblue spire.doc.free5.2.0 com.e-iceblue e-iceblue https://repo.e-iceblue.cn/repository/maven-public/
直接上代码
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import com.aspose.cells.License;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class WordToPdf {
public static void main(String[] args) {
doc2pdf("C:\Users\admin\Desktop\1.docx","E:\a\excel转pdf\1.pdf");
}
public static boolean getLicense() {
boolean result = false;
try {
String license = "rn" +
" rn" +
" rn" +
" Aspose.Total for Java rn" +
" Aspose.Words for Java rn" +
" rn" +
" Enterprise rn" +
" 20991231 rn" +
" 20991231 rn" +
" 8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7 rn" +
" rn" +
" sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU= rn" +
" ";
InputStream is = new ByteArrayInputStream(license.getBytes("UTF-8"));
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static boolean doc2pdf(String inPath, String outPath) {
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return false;
}
Document document = new Document();
document.loadFromFile(inPath);
document.saveToFile(outPath, FileFormat.PDF);
return true;
}
}
用接口测试,代码如下
@PostMapping("/doc2pdf")
public String doc2pdf(MultipartFile file ) throws IllegalStateException, IOException{
String fileName = file.getOriginalFilename();
String localFilePath = StrUtil.appendIfMissing("/data/test", "/") + fileName;
file.transferTo(new File(localFilePath));
WordToPdf.doc2pdf(localFilePath, "/data/test/" + fileName.split("\.")[0] + ".pdf");
return "success可喜可贺";
}



