本工具类是基于aspose技术实现,由于aspose是付费技术,这里使用的许可认证方式支持。
创建license.xml许可文件
<?xml version="1.0" encoding="UTF-8" ?><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>
引入aspose包
由于版本原因,可以下载aspose-words-16.8.0-jdk16.jar,然后引入到项目。如果是企业项目可以引入到公司私服仓库。
因为这里不允许上传附件,所有资料上传到了资源中,需要的可以去下载。已经设置0积分下载,如果后期动态调整了积分可以留言我重新设置积分。
https://download.csdn.net/download/sinat_32366329/87173726
工具类
package org.leaf.word.to.pdf.aspose;import com.aspose.words.Document;import com.aspose.words.License;import com.aspose.words.SaveFormat;import java.io.*;/** * 本工具类是基于aspose技术实现,由于aspose是付费技术,这里使用的许可认证方式支持 */public class Doc2PdfUtil { /** * 创建license.xml文件,文件内容如下: * <?xml version="1.0" encoding="UTF-8" ?> * <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> */ private static void initLicense() throws Exception { InputStream is = Doc2PdfUtil.class.getClassLoader().getResourceAsStream("\\license.xml"); License aposeLic = new License(); aposeLic.setLicense(is); } public static void doc2Pdf(String docPath, String pdfPath) throws Exception { initLicense(); try (FileInputStream fis = new FileInputStream(docPath); FileOutputStream fos = new FileOutputStream(pdfPath)) { Document doc = new Document(fis); doc.save(fos, SaveFormat.PDF); } } public static void doc2Pdf(String docPath, OutputStream os) throws Exception { initLicense(); try (InputStream is = new FileInputStream(docPath)) { Document doc = new Document(is); doc.save(os, SaveFormat.PDF); } } public static void doc2Pdf(InputStream is, String pdfPath) throws Exception { initLicense(); try (OutputStream os = new FileOutputStream(pdfPath)) { Document doc = new Document(is); doc.save(os, SaveFormat.PDF); } } public static void doc2Pdf(InputStream is, OutputStream os) throws Exception { initLicense(); Document doc = new Document(is); doc.save(os, SaveFormat.PDF); } public static void main(String[] args) throws Exception { doc2Pdf("F:\\idea-workspace\\my_source\\word-document\\src\\main\\resources\\Git学习指南笔记.doc", "F:\\idea-workspace\\my_source\\word-document\\src\\main\\resources\\Git学习指南笔记.pdf"); doc2Pdf("F:\\idea-workspace\\my_source\\word-document\\src\\main\\resources\\Git学习指南笔记.doc", new FileOutputStream("F:\\idea-workspace\\my_source\\word-document\\src\\main\\resources\\Git学习指南笔记1.pdf")); doc2Pdf(new FileInputStream("F:\\idea-workspace\\my_source\\word-document\\src\\main\\resources\\Git学习指南笔记.doc"), new FileOutputStream("F:\\idea-workspace\\my_source\\word-document\\src\\main\\resources\\Git学习指南笔记2.pdf")); }}