JAVA/Linux word转pdf常用方法汇总

在开发过程中,会遇到word转pdf的需求,通过研究提供几种操作方法,供大家学习参考。方法一可对文档中的图片、表格等进行转换,但是对特殊的格式(如目录等)可能存在乱码情况。方法二通过测试无法对文档中的图片、表格等进行转换,有报错异常,后续再进行研究。方法三有转换页数限制,免费版只能转换4页。方法四使用Document4j依赖可以解决windows上转换问题,但是无法在linux系统使用。方法五使用OpenOffice可以解决,但是需要在windows或linux安装OpenOffice应用,但是比较推荐这一种解决方案。好了费话不多说上代码:

方法一

引入maven依赖
 <!--    doc转pdf    -->
        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-JAXB-Internal</artifactId>
            <version>8.2.4</version>
        </dependency>
        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-export-fo</artifactId>
            <version>8.2.4</version>
        </dependency>
示例代码
// 方法调用
// 转换docx为pdf,如果文档中有特殊格式会产生乱码
word2pdf("D:\\work\\test\\test.docx", "D:\\work\\test.pdf");

	/**
     * word 转 pdf  有格式问题
     *
     * @param source 源文件
     * @param target 目标文件
     */
    public static void word2pdf(String source, String target) {

        try {
            WordprocessingMLPackage pkg = Docx4J.load(new File(source));

            Mapper fontMapper = new IdentityPlusMapper();
            fontMapper.put("隶书", PhysicalFonts.get("LiSu"));
            fontMapper.put("宋体", PhysicalFonts.get("SimSun"));
            fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei"));
            fontMapper.put("黑体", PhysicalFonts.get("SimHei"));
            fontMapper.put("楷体", PhysicalFonts.get("KaiTi"));
            fontMapper.put("新宋体", PhysicalFonts.get("NSimSun"));
            fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
            fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));
            fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
            fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));
            fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));
            fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));
            fontMapper.put("等线", PhysicalFonts.get("SimSun"));
            fontMapper.put("等线 Light", PhysicalFonts.get("SimSun"));
            fontMapper.put("华文琥珀", PhysicalFonts.get("STHupo"));
            fontMapper.put("华文隶书", PhysicalFonts.get("STLiti"));
            fontMapper.put("华文新魏", PhysicalFonts.get("STXinwei"));
            fontMapper.put("华文彩云", PhysicalFonts.get("STCaiyun"));
            fontMapper.put("方正姚体", PhysicalFonts.get("FZYaoti"));
            fontMapper.put("方正舒体", PhysicalFonts.get("FZShuTi"));
            fontMapper.put("华文细黑", PhysicalFonts.get("STXihei"));
            fontMapper.put("宋体扩展", PhysicalFonts.get("simsun-extB"));
            fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312"));
            fontMapper.put("新細明體", PhysicalFonts.get("SimSun"));
            pkg.setFontMapper(fontMapper);

            Docx4J.toPDF(pkg, new FileOutputStream(target));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Docx4JException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

方法二 XDocReport解决方案

引入maven依赖
<!--   XDocReport转换器     -->
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.xdocreport.converter.docx.xwpf</artifactId>
            <version>2.0.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>5.0.3</version>
        </dependency>
示例代码
// 方法调用
convertWordToPdf("D:\\work\\test\\test.docx", "D:\\work\\test_2.pdf");

	/**
     * 使用XDocReport转换器
     * @param docxFileName docx文件名
     * @param pdfFileName pdf文件名
     */
    public static void convertWordToPdf(String docxFileName, String pdfFileName) 	{
        try(InputStream inputStream = new FileInputStream(docxFileName);
            OutputStream outputStream = new FileOutputStream(pdfFileName)) {
            XWPFDocument document = new XWPFDocument(inputStream);
            PdfOptions options = PdfOptions.create();
            // Convert .docx file to .pdf file
            PdfConverter.getInstance().convert(document, outputStream, options);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

方法三 Spire.PDF解决方案

引入maven依赖
   <repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
    </repositories>
	
	<!--   Spire.PDF   free为免费版转换页数有限制,非免费版去掉free即可   -->
        <dependency>
            <groupId>e-iceblue</groupId>
            <artifactId>spire.office.free</artifactId>
            <version>5.3.1</version>
        </dependency>
示例代码
//通过流加载WPS文字文档
        FileInputStream inputStream = new FileInputStream(new File("C:\\Users\\lcl\\Desktop\\test.docx"));
        Document document = new Document();
        document.loadFromStream(inputStream, FileFormat.Docx);
        //保存为PDF
        document.saveToFile("C:\\Users\\lcl\\Desktop\\test.pdf",FileFormat.PDF);

方法四 Document4j解决方案

参照文章:
https://blog.csdn.net/apache_z/article/details/104970280

方法五 OpenOffice解决方案

参照文章:
https://zhuanlan.zhihu.com/p/114682616

  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值