如何将word转pdf文件

  • 作者: 凯哥Java(公众号:凯哥Java)
  • 工作小总结
  • 时间:2023-05-25 10:15
  • 1488人已阅读
简介 网上许多实现我都看了一下,如JobConverter+OpenOffice   (Windows系统下),SaveAsPDFandXPS+jacob(Windows操作系统下,电脑里有office),或者纯POI解决(jar繁琐易冲突)。(这三种方法可以去地址看一下)最终采用了aspose.words的方法实现(此为商业jar,我用的破解,正版实在太贵了!),实现颇为

🔔🔔好消息!好消息!🔔🔔

 如果您需要注册ChatGPT,想要升级ChatGPT4。凯哥可以代注册ChatGPT账号代升级ChatGPT4

有需要的朋友👉:微信号 kaigejava2022

网上许多实现我都看了一下,如JobConverter + OpenOffice     ( Windows系统下 ) ,SaveAsPDFandXPS + jacob (Windows操作系统下,电脑里有office),或者纯POI解决(jar繁琐易冲突)。(这三种方法可以去地址看一下)

最终采用了aspose.words的方法实现(此为商业jar,我用的破解,正版实在太贵了!),实现颇为简单。下附代码

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

/**
 * Created by 凯哥java
 */
public class Word2PdfUtil {
    public static void main(String[] args) {
        doc2pdf("/Users/zhangzhiqiang/Downloads/Java_echars_word/csWord.docx","/Users/zhangzhiqiang/Downloads/Java_echars_word/word_to_pdf.pdf");
    }
    public static boolean getLicense() {
        boolean result = false;
        try {
            //引用当前项目中的license.xml
            InputStream is = Word2PdfUtil.class.getResourceAsStream("/license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    public static void doc2pdf(String inPath, String outPath) {
        //验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicense()) {
            return;
        }
        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,
            doc.save(os, SaveFormat.PDF);
            // EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
————————————————
版权声明:本文为CSDN博主「xiaoMMM94」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xiaoMMM94/article/details/93172199

相关jar包及破解地址

上述方法实测完美实现服务端生成echars图片插入word文档并保存,以及word转pdf功能。


TopTop