Java操作Word转PDF(Word转图片)

1. spire.doc的jar引用

        首先我们需要用到国产word处理工具jar包spire.doc,可以通过maven仓库寻找,然后在pom文件中直接引用。

        此处需要注意,我们需要使用的是spire.doc.free(免费版的),切勿使用spire.doc(如果使用了,处理后的word文件第一页的顶部会出现红色的警告水印信息)

        如果不能直接从仓库引用到此jar,可以在仓库直接下载下来后,手动存放与本地仓库中,处理方式详见本人的另一个帖子: 本地Maven仓库导入外部jar

2. 直接上代码

/**
     * word转img(word转pdf)
     *
     * @param inFilePath word文件存放地址全路径
     * @return 生成的图片地址集合
     * @throws IOException
     */
    public static List<String> word2Img(String inFilePath) throws IOException {
        List<String> list = new ArrayList<>();
        Document doc = new Document(inFilePath);
        String outFilePath = inFilePath.substring(0, inFilePath.lastIndexOf("."));
        String pdfFilePath = outFilePath + "_副本.pdf";
        //产生pdf文件(如不需要自己后续增加删除pdf的代码)
        doc.saveToFile(pdfFilePath, FileFormat.PDF);
        int pageCount = doc.getPageCount();
        pageCount = pageCount > 3 ? 3 : pageCount;
        for (int i = 0; i < pageCount; i++) {
        for (int i = 0; i < doc.getPageCount(); i++) {
            BufferedImage bufferedImage = doc.saveToImages(i, ImageType.Bitmap);
            String imgPath = outFilePath + "_副本" + (i + 1) + ".png";
            File file = new File(imgPath);
            ImageIO.write(bufferedImage, "PNG", file);
            list.add(imgPath);
        }
        return list;
    }

    public static void main(String[] args) throws IOException {
        String wordPath = "C:\\Users\\DaiHaijiao\\Desktop/aaa.docx";
        List<String> list = WordUtils.word2Img(wordPath);
        System.out.println(list);
    }

说明:代码执行后,会生成一个pdf文件(此文件就是word转pdf后的文件),而后续的转图片就是基于此pdf文件进行的转换。如果不需要pdf文件的输出,在转图片完成后执行代码删除此pdf即可;如只需要得到pdf文件,则就不需要后续的转图片代码了(直接删除那块代码即可)。

注意:此spire.doc.free在转换pdf或是图片时,word内的内容不能超过3页,如果超过了3页,第四页开始将无法转换(第四页上将会显示警告错误信息,第五页,第六页...全部丢失),转成的图片也一样。

3. 若只需要将Word转PDF(不需要转图片),可以使用poi。此方式可以解决spire.doc.free在页码数量限制上的问题。

3.1 代码pom引入依赖

        <!--<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
            <version>2.0.1</version>
        </dependency>-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
            <version>2.0.2</version>
            <exclusions>
                <exclusion>
                    <artifactId>org.apache.poi</artifactId>
                    <groupId>poi-ooxml</groupId>
                </exclusion>
            </exclusions>
        </dependency>
此处注意:
poi-ooxml 3.17 + fr.opensagres.poi.xwpf.converter.pdf-gae 2.0.1可以直接配合使用。
如果要使用poi-ooxml 4.1.2 + fr.opensagres.poi.xwpf.converter.pdf-gae 2.0.2时,要去除fr.opensagres.poi.xwpf.converter.pdf-gae 2.0.2里面的poi-ooxml。

3.2 相关使用代码

    /**
     * word转pdf
     *
     * @param inFilePath word存放全路径
     * @return 转换后的文件全路径
     * @throws IOException
     */
    public static String doc2Pdf(String inFilePath) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(inFilePath);
        XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
        PdfOptions pdfOptions = PdfOptions.create();
        String outFilePath = inFilePath.substring(0, inFilePath.lastIndexOf("."));
        outFilePath += "_副本.pdf";
        FileOutputStream fileOutputStream = new FileOutputStream(outFilePath);
        PdfConverter.getInstance().convert(xwpfDocument, fileOutputStream, pdfOptions);
        fileInputStream.close();
        fileOutputStream.close();
        return outFilePath;
    }

    public static void main(String[] args) throws IOException {
        String inFilePath = "C:\\Users\\DaiHaijiao\\Desktop/aaa.docx";
        String outFilePath = WordUtils.doc2Pdf(inFilePath);
        System.out.println(outFilePath);
    }

此处Word和生成的PDF就不截图了(当Word较大时,执行可能会有点慢)。

特别注意:Word中的字体,一定,一定,一定,要用一样的字体!

字体切勿混用!

字体切勿混用!

字体切勿混用!

好话说三遍!!!

猜你喜欢

转载自blog.csdn.net/Dai_Haijiao/article/details/128672237