java 将word转为pdf文件,无水印无限制 Spire.Doc无水印

本文介绍了如何使用spire.doc和documents4j库将Word文档转换为PDF,以避免在生成的PDF中出现水印。特别提到spire.pdf的水印问题以及在Linux环境下使用LocalConverter的替代方案。
摘要由CSDN通过智能技术生成

逻辑很简单、就是不用spire.pdf。用他的pdf转换会在你的文件里面生成水印、每一页都有,用doc生成只有首页有,只需要替换掉首页的水印就可以了

1. 首先使用spire.doc生成word文档

Document doc2 = new Document(new FileInputStream(new File(name)));
Section sec = doc2.addSection();
doc2.saveToFile(name2, FileFormat.Docx_2010);//保存文件 生成word
doc2.close();

2、引用documents4j转换成pdf


        InputStream docxInputStream = null;
        OutputStream outputStream = null;
        try {
            // 原word地址
            docxInputStream = new FileInputStream("E:\\\\test2\\\\word模板.docx");
            // 转换后pdf生成地址
            outputStream = new FileOutputStream("E:\\\\test2\\\\word模板转pdf.pdf");
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream)
                    .as(DocumentType.DOCX)
                    .to(outputStream)
                    .as(DocumentType.PDF).execute();
            // 关闭
            converter.shutDown();
            // 关闭
            outputStream.close();
            // 关闭
            docxInputStream.close();
        } catch (Exception e) {
            System.out.println("[documents4J] word转pdf失败:" + e.toString());
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }
            if (docxInputStream != null) {
                docxInputStream.close();
            }
        }

3、引用的jar包

<repository>
    <id>com.e-iceblue</id>
    <name>e-iceblue</name>
    <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
<dependency>
    <groupId>com.documents4j</groupId>
    <artifactId>documents4j-local</artifactId>
    <version>1.0.3</version>
</dependency>
<dependency>
    <groupId>com.documents4j</groupId>
    <artifactId>documents4j-transformer-msoffice-word</artifactId>
    <version>1.0.3</version>
</dependency>
<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>spire.doc</artifactId>
    <version>5.4.10</version>
</dependency>

4、这个 IConverter converter = LocalConverter.builder().build();方法是不支持linux的、linux的解决办法可以参考这个大佬的方法

http://t.csdnimg.cn/mAVAP

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值