java实现word转pdf

1. 使用jacob进行转换

注意:

  1. 使用jacob需要将.dll文件放入JDK的bin目录下和JRE的bin目录下
  2. jacob的jar包版本需要和.dll文件的版本一致(jacob1.18下载(密码: evsp))
  3. 仅支持windows系统
  4. 需要有相关的软件,例如wps和office
public class PdfUtil {
	private static final int wdFormatPDF = 17;
	private static final int xlTypePDF = 0;
	private static final int ppSaveAsPDF = 32;

    /**
     * word转换为pdf
     * @param inputFile
     * @param pdfFile
     * @return
     */
	public static boolean word2PDF(String inputFile, String pdfFile) {
		try {
			// 打开word应用程序
			ActiveXComponent app = new ActiveXComponent("Word.Application");
			// 设置word不可见
			app.setProperty("Visible", false);
			// 获得word中所有打开的文档,返回Documents对象
			Dispatch docs = app.getProperty("Documents").toDispatch();
			// 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
			Dispatch doc = Dispatch.call(docs, "Open", inputFile, false, true)
					.toDispatch();
            // 调用Document对象的SaveAs方法,将文档保存为pdf格式,word保存为pdf格式宏,值为17
			Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF);
			// 关闭文档
			Dispatch.call(doc, "Close", false);
			// 关闭word应用程序
			app.invoke("Quit", 0);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

    /**
     * excel转换为pdf
     * @param inputFile
     * @param pdfFile
     * @return
     */
	public static boolean excel2PDF(String inputFile, String pdfFile) {
		try {
			ActiveXComponent app = new ActiveXComponent("Excel.Application");
			app.setProperty("Visible", false);
			Dispatch excels = app.getProperty("Workbooks").toDispatch();
			Dispatch excel = Dispatch.call(excels, "Open", inputFile, false,
					true).toDispatch();
			Dispatch.call(excel, "ExportAsFixedFormat", xlTypePDF, pdfFile);
			Dispatch.call(excel, "Close", false);
			app.invoke("Quit");
			return true;
		} catch (Exception e) {
			return false;
		}
	}

    /**
     * ppt转换为pdf
     * @param inputFile
     * @param pdfFile
     * @return
     */
	public static boolean ppt2PDF(String inputFile, String pdfFile) {
		try {
			ActiveXComponent app = new ActiveXComponent(
					"PowerPoint.Application");
			Dispatch ppts = app.getProperty("Presentations").toDispatch();
 
			Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, true,// 只读
					true,// Untitled指定文件是否有标题
					false// WithWindow指定文件是否可见
					).toDispatch();
 
			Dispatch.call(ppt, "SaveAs", pdfFile, ppSaveAsPDF);
 
			Dispatch.call(ppt, "Close");
 
			app.invoke("Quit");
			return true;
		} catch (Exception e) {
			return false;
		}
	}

    public static void main(String[] args) {
        word2PDF("C:\\Users\\Administrator\\Desktop\\测试.docx","C:\\Users\\Administrator\\Desktop\\测试doc.pdf");
        excel2PDF("C:\\Users\\Administrator\\Desktop\\测试.xlsx","C:\\Users\\Administrator\\Desktop\\测试xlsx.pdf");
        ppt2PDF("C:\\Users\\Administrator\\Desktop\\测试.pptx","C:\\Users\\Administrator\\Desktop\\测试ppt.pdf");
    }
}

2. 使用aspose进行转换

注意:

  1. 需要引入aspose-words-15.8.0-jdk16.jar包
  2. 需要引入注册码,否则会出现水印问题
  3. 此方法可以在linux和windows系统上均可使用

(1)新建一个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>23dcc79f-44ec-4a23-be3a-03c1632404e9</SerialNumber>
</Data>
<Signature>0nRuwNEddXwLfXB7pw66G71MS93gW8mNzJ7vuh3Sf4VAEOBfpxtHLCotymv1PoeukxYe31K441Ivq0Pkvx1yZZG4O1KCv3Omdbs7uqzUB4xXHlOub4VsTODzDJ5MWHqlRCB1HHcGjlyT2sVGiovLt0Grvqw5+QXBuinoBY0suX0=</Signature>
</License>

(2)java代码:

public class WordToPdfUtil {

    /**
     * 获取注册信息
     * @return
     */
    public static boolean getLicense() {
        boolean result = false;
        try {
            // 读取license.xml文件
            File file = new File("src/license.xml");
            InputStream is = new FileInputStream(file);
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * word转pdf
     * @param inPath    文件输入路径
     * @param outPath   文件输出路径
     */
    public static void doc2pdf(String inPath, String outPath) throws IOException {
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicense()) {
            return;
        }

        File file = new File(outPath);
        FileOutputStream os = new FileOutputStream(file);
        try {
            long old = System.currentTimeMillis();
            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("共耗时:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            os.close();
        }
    }

    public static void main(String[] args) throws IOException {
        doc2pdf("C:\\Users\\Administrator\\Desktop\\测试测试.docx","C:\\Users\\Administrator\\Desktop\\测试测试.pdf");
    }
}

参考自:
https://www.cnblogs.com/jssj/p/11808161.html
https://blog.youkuaiyun.com/weixin_42970101/article/details/97796346

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值