aspose将word转为pdf

时间:2025-04-11 13:24:27

aspose文件转换功能非常方便,文件也不会出现乱码,内容丢失的情况。

相关jar和下载地址:/download/qq_31674229/31839253

import ;
import ;
import ;

import ;
import ;
import ;
import ;


/**
 * @author qianshijiang
 * @since 2021-10-12
 */
public class Word2PdfUtil {
 
    public static void main(String[] args) {
    	Word2PdfUtil.doc2pdf("D://", "D://");
    }
 
    public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = ().getResourceAsStream(""); // 应放在..\WebRoot\WEB-INF\classes路径下
            License aposeLic = new License();
            (is);
            result = true;
        } catch (Exception e) {
            ();
        }
        return result;
    }

    /**
     * 将word转换为pdf
     * @param inPath word存储路径
     * @param outPath pdf保存路径
     */
    public static void doc2pdf(String inPath, String outPath) {
        if (!getLicense() || (inPath) || (outPath)) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return;
        }
        try {
            long old = ();
            File file = new File(outPath); // 新建一个空白pdf文档
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document(inPath); // Address是将要被转化的word文档
            (os, );// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
                                         // EPUB, XPS, SWF 相互转换
            long now = ();
            ("Word转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
        } catch (Exception e) {
            ();
        }
    }


    /**
     * 将word转为pdf
     * @param input 需要转换的word
     * @param saveFile 保存pdf文件
     */
    public static void doc2pdf(InputStream input, File saveFile) {
        if (!getLicense() || input==null || saveFile==null) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return;
        }
        try {
            long old = ();
            FileOutputStream os = new FileOutputStream(saveFile);
            Document doc = new Document(input); // Address是将要被转化的word文档
            (os, );// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
            // EPUB, XPS, SWF 相互转换
            long now = ();
            ("Word转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
        } catch (Exception e) {
            ();
        }
    }
    
    
    
}