java实现Word文档转换PDF文档

2022-05-16 00:00:00 java 文档 转换

最近需要实现在java语言Word文档转成PDF文档的功能,做了一下调研,最后使用aspose-words实现了该功能。

注意,aspose-words 为商业软件,本文仅是使用方法的demo,使用的 jar包 aspose-words-15.8.jar 和 授权文件license.xml 均来源于网络,请勿在商业环境使用。

本文示例代码和jar下载:https://download.csdn.net/download/u012775558/48709768?spm=1003.2166.3001.6637.1

实现转换需要如下3个文件

  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>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
    </Data>
    <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=
    </Signature>
</License>
  1. aspose-words-15.8.jar
  2. java工具类
package org.example;


import com.aspose.words.*;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * word转pdf
 *
 *
 * @author Juveniless
 * @create 2021-10-12 下午 02:18
 **/
public class WordToPdfUtil {
  static Map<String, String> fontsMapping = new HashMap<>();

  static {
    try {
      InputStream is = WordToPdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
      License license = new License();
      license.setLicense(is);
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println("授权文件[license.xml]加载失败!");
    }
  }


  /**
   * 字体名称映射
   * Windows 中文字体与英文名称对照表 参考
   * http://www.360doc.com/content/14/0520/09/8463843_379246571.shtml
   */
  static {
    fontsMapping.put("黑体", "SimHei");
    fontsMapping.put("宋体", "SimSun");
    fontsMapping.put("楷体_GB2312", "KaiTi_GB2312");
    fontsMapping.put("仿宋_GB2312", "FangSong_GB2312");
    fontsMapping.put("新宋体", "NSimSun");
    fontsMapping.put("仿宋", "FangSong");
    fontsMapping.put("楷体", "KaiTi");
    fontsMapping.put("微软正黑", "Microsoft JhengHei");
    fontsMapping.put("微软雅黑", "Microsoft YaHei");
    fontsMapping.put("细明体", "MingLiU");
    fontsMapping.put("标楷体", "DFKai-SB");
    fontsMapping.put("新细明体", "PMingLiU");
    fontsMapping.put("华文宋体", "STSong");
    fontsMapping.put("华文中宋", "STZhongsong");
    fontsMapping.put("华文仿宋", "STFangsong");
    fontsMapping.put("华文彩云", "STCaiyun");
    fontsMapping.put("华文琥珀", "STHupo");
    fontsMapping.put("华文隶书", "STLiti");
    fontsMapping.put("华文行楷", "STXingkai");
    fontsMapping.put("华文新魏", "STXinwei");
    fontsMapping.put("华文细黑", "STXihei");
    fontsMapping.put("华文楷体", "STKaiti");
    fontsMapping.put("方正舒体", "FZShuTi");
    fontsMapping.put("方正姚体", "FZYaoti");
    fontsMapping.put("幼圆", "YouYuan");
    fontsMapping.put("隶书", "LiSu");

  }

  public static void execute(String wordPath, String pdfPath) {
    File file = new File(pdfPath);
    FileOutputStream os = null;
    try {
      os = new FileOutputStream(file);
      Document doc = new Document(wordPath);
      // 设置字体
      for (Run run : (Iterable<Run>) doc.getChildNodes(NodeType.RUN, true)) {
        Font font = run.getFont();
        try {
          String newFontName = fontsMapping.get(font.getName());
          if (newFontName != null) {
            run.getFont().setName(newFontName);
          }
        } catch (Exception e) {
        }
      }
      doc.save(os, SaveFormat.PDF);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
      System.out.println("文件不存在");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (os != null) {
        try {
          os.close();
        } catch (IOException e) {
        }
        System.out.println("文件格式转换完成");
      }
    }
  }


  public static void main(String[] args) {
    String wordPath = "D:\\temp\\急性药物过敏性间质性肾炎临床路径(2016年版).docx";
    String pdfPath = "D:\\temp\\急性药物过敏性间质性肾炎临床路径(2016年版).pdf";
    WordToPdfUtil.execute(wordPath, pdfPath);
  }


}

项目结构如下:
《java实现Word文档转换PDF文档》

转换效果如下:
《java实现Word文档转换PDF文档》
《java实现Word文档转换PDF文档》

    原文作者:Juveniless
    原文地址: https://blog.csdn.net/u012775558/article/details/121530527
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。

相关文章