Java word(doc/docx)转pdf 无水印/免费

最近项目有个需求需要将word转为pdf。
网络上有好多种方案:spire,aspose,jacob等,由于jacob需要在jdk添加dll文件,有点束缚,所以在这里就不讲这个方案了
一.spire.doc

1.引入jar包

        <dependencies>
        <dependency>
            <groupId>e-iceblue</groupId>
            <artifactId>spire.doc.free</artifactId>
            <version>3.9.0</version>
        </dependency>
        </dependencies>

      <repositories>
          <repository>
              <id>com.e-iceblue</id>
              <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
          </repository>
      </repositories>

2.wordtopdfutil

    public class WordToPdfUtil2 {
        public static void word2pdf(String inPath, String outPath) throws Exception {
            Document document = new Document();
            document.loadFromFile(inPath);
            document.saveToFile(outPath, FileFormat.PDF);
        }

        public static void word2pdf(String inPath, OutputStream outputStream){
            Document document = new Document();
            document.loadFromFile(inPath);
            document.saveToStream(outputStream,FileFormat.PDF);
        }
    }

3.测试

  public static void main(String[] args) {
         //aspose
 //        String docPath = "C:\\Users\\linxishui\\Desktop\\test1.doc";
 //        String pdfPath = "C:\\Users\\linxishui\\Desktop\\test.pdf";
 //        WordToPdfUtil.doc2pdf(docPath,pdfPath);

         //spire
         String docPath2 = "C:\\Users\\linxishui\\Desktop\\test1.doc";
         String pdfPath2 = "C:\\Users\\linxishui\\Desktop\\test2.pdf";
         try {
             WordToPdfUtil2.word2pdf(docPath2,pdfPath2);
         } catch (Exception e) {
             e.printStackTrace();
         }
     }

4.效果
word:在这里插入图片描述
pdf:
在这里插入图片描述

5.限制
此种方法为免费的,但是限制每次只能打印3页,超过三页后就不给打了,
免费的就是这样的啦,不然人家花钱版的怎么卖的出去嘞,价格6000/年起步,
我这个穷人家是买不起的啦,所以有了下面的一种方法

二.aspose

1.引入jar包

        <dependencies>
         <dependency>
           <groupId>com.aspose</groupId>
           <artifactId>aspose-words</artifactId>
           <version>18.6</version>
           <classifier>jdk16</classifier>
         </dependency>
        </dependencies>

这里注意:这个jar包如果不是pj后的,而是直接从maven拉下来的话,
虽然没有了页数限制,但是页面顶端会有一行广告水印哦,我这里用的
是pj版本配合license验证使用的
在这里插入图片描述
2.wordtopdfutil

    public class WordToPdfUtil {

                    public static boolean getLicense() {
                        boolean result = false;
                        try {
                            InputStream is = Test.class.getClassLoader().getResourceAsStream("\\license.xml");
                            License aposeLic = new License();
                            aposeLic.setLicense(is);
                            result = true;
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return result;
                    }

                    public static boolean doc2pdf(String inPath, String outPath) {
                        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
                            return false;
                        }
                        FileOutputStream os = null;
                        try {
                            long old = System.currentTimeMillis();
                            File file = new File(outPath); // 新建一个空白pdf文档
                            os = new FileOutputStream(file);
                            Document doc = new Document(inPath); // Address是将要被转化的word文档
                            doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
                            long now = System.currentTimeMillis();
                            System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
                        } catch (Exception e) {
                            e.printStackTrace();
                            return false;
                        }finally {
                            if (os != null) {
                                try {
                                    os.flush();
                                    os.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        return true;
                    }



    }

3.测试

  public static void main(String[] args) {
          public static void main(String[] args) {
                 //aspose
                 String docPath = "C:\\Users\\linxishui\\Desktop\\test1.doc";
                 String pdfPath = "C:\\Users\\linxishui\\Desktop\\test.pdf";
                 WordToPdfUtil.doc2pdf(docPath,pdfPath);

                 //spire
         //        String docPath2 = "C:\\Users\\linxishui\\Desktop\\test1.doc";
         //        String pdfPath2 = "C:\\Users\\linxishui\\Desktop\\test2.pdf";
         //        try {
         //            WordToPdfUtil2.word2pdf(docPath2,pdfPath2);
         //        } catch (Exception e) {
         //            e.printStackTrace();
         //        }
             }

4.效果
word:在这里插入图片描述
pdf:
在这里插入图片描述
5.限制
这种方法是我目前找到的最实惠好用的一种了,限制就是要找到pj包了,我找了好久,这里我就不传播了

总结:此两种方法都比较简单,好用,而且window,linux版本都适用,如果linux上转换有乱码的话,装一下windows字体就行了
这里推荐一篇装windows字体博客:https://blog.csdn.net/neulily2005/article/details/106003527

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
POI(Poor Obfuscation Implementation)是一个Java库,可以用来处理Microsoft Office格式的文件,包括.doc和.docx。要将这些文件换为HTML格式,可以使用POI库的一些功能。 首先,需要引入POI库的相关依赖。然后,使用POI的XWPFDocument类来读取.docx文件,或者使用HSSFWorkbook类来读取.doc文件。它们都提供了访问文件的内容和结构的方法。 读取文件后,可以遍历其中的段落、文本和样式等元素,并将其换为HTML格式。可以使用StringBuilder来拼接HTML文本。例如,可以将每个段落换为一个\<p>标签,每个文本换为一个\<span>标签,并根据样式设置相关属性,如字体、颜色等。 对于嵌入的表格、图片和其他对象,可以使用POI的一些工具类来提取它们的内容,并根据需要换为HTML格式。例如,可以使用XWPFTables获取表格的数据,并将其换为\<table>标签。对于图片,可以使用XWPFPictureData获取图片的二进制数据,并使用Base64编码将其嵌入到HTML中。 最后,将换后的HTML文本保存到文件中或传输到Web页面中即可。 需要注意的是,POI库只提供了对Microsoft Office格式文件的处理功能,不保证100%的换准确性。在使用过程中,可能需要根据具体情况进行一些调整和优化。另外,由于POI是一个开源项目,可以通过查阅官方文档和社区提供的资料来获得更多帮助和支持。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值