当前位置: 代码网 > it编程>编程语言>Java > 基于Java实现将word,excel文件转换为pdf的工具类

基于Java实现将word,excel文件转换为pdf的工具类

2025年08月01日 Java 我要评论
包含的工具类wordtopdfutil用于将word文档转换为pdf格式的工具类exceltopdfutil用于将excel文档转换为pdf格式的工具类pdftoimageutil用于将pdf文档转换

包含的工具类

wordtopdfutil用于将word文档转换为pdf格式的工具类

exceltopdfutil用于将excel文档转换为pdf格式的工具类

pdftoimageutil用于将pdf文档转换为image格式的工具类

lib文件说明

使用的

aspose-words-15.8.0-jdk16.jar 将word文档转换为pdf需要引入

aspose-cells-8.5.2.jar 将excel文档转换为pdf需要引入

aspose-cells-20.7.jar 将excel文档转换为pdf需要引入(linux端中文出现乱码时使用)

未使用的

aspose-words-15.12.0-jdk16.jar 未测试

aspose-pdf-22.4.cracked.jar 将pdf转换为其他格式【破解版效果不佳】

aspose-pdf-22.4.jar 将pdf转换为其他格式【未破解效果依然不佳】

核心代码

wordtopdfutil

    /**
     * word 转 pdf
     *
     * @param wordfilepath word文件路径
     * @param pdffilepath  pdf文件路径
     */
    public static void convert(string wordfilepath, string pdffilepath) {
        fileoutputstream fileoutputstream = null;
        try {
            pdffilepath = pdffilepath == null ? getpdffilepath(wordfilepath) : pdffilepath;
            setlicense();
            file file = new file(pdffilepath);
            fileoutputstream = new fileoutputstream(file);
            document doc = new document(wordfilepath);
            doc.save(fileoutputstream, saveformat.pdf);
        } catch (exception e) {
            e.printstacktrace();
        } finally {
            try {
                assert fileoutputstream != null;
                fileoutputstream.close();
            } catch (ioexception e) {
                e.printstacktrace();
            }

        }
    }

exceltopdfutil

    /**
     * excel 转 pdf
     *
     * @param excelfilepath excel文件路径
     * @param pdffilepath   pdf文件路径
     * @param convertsheets 需要转换的sheet
     */
    public static void convert(string excelfilepath, string pdffilepath, int[] convertsheets) {
        fileoutputstream fileoutputstream = null;
        try {
            pdffilepath = pdffilepath == null ? getpdffilepath(excelfilepath) : pdffilepath;
            // 设置license
            setlicense();
            // 读取excel文件
            workbook wb = new workbook(excelfilepath);
            fileoutputstream = new fileoutputstream(pdffilepath);
            // 设置pdf格式
            pdfsaveoptions pdfsaveoptions = new pdfsaveoptions();
            pdfsaveoptions.setonepagepersheet(true);
            if (null != convertsheets) {
                printsheetpage(wb, convertsheets);
            }
            wb.save(fileoutputstream, pdfsaveoptions);
            fileoutputstream.flush();
        } catch (exception e) {
            e.printstacktrace();
        } finally {
            try {
                assert fileoutputstream != null;
                fileoutputstream.close();
            } catch (ioexception e) {
                e.printstacktrace();
            }
        }
    }

pdftoimageutil

    /**
     * 根据参数将全部的pdf转换为image
     *
     * @param pdffilepath   pdf文件路径
     * @param imagefiledir  图片存储目录
     * @param imagefilename 图片存储文件没
     * @param type          图片类型
     */
    public static void convertallpage(string pdffilepath, string imagefiledir, string imagefilename, string type) {
        system.setproperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.kcmsserviceprovider");
        // 图片类型
        if (type == null || "".equals(type)) {
            type = image_type_jpg;
        }
        // 1.加载pdf文件
        file file = new file(pdffilepath);
        // 2.生成jpg图片的文件夹
        imagefiledir = imagefiledir == null ? getimagefiledir(pdffilepath) : imagefiledir;
        imagefilename = imagefilename == null ? getimagefilename(pdffilepath) : imagefilename;
        try {
            pddocument pddocument = pddocument.load(file);
            pdfrenderer renderer = new pdfrenderer(pddocument);
            int pagecount = pddocument.getnumberofpages();

            for (int i = 0; i < pagecount; i++) {
                bufferedimage image = renderer.renderimagewithdpi(i, 144);
                imageio.write(image, type,
                        new file(imagefiledir.concat(file.separator).concat(imagefilename).concat("_")
                                .concat(string.valueof(i + 1)).concat(".").concat(type)));
            }
        } catch (ioexception e) {
            e.printstacktrace();
        }
    }

问题处理

都需要将字体文件simsun.ttc上传到jarpath/font目录下。

word中文无法转换

在linux环境下,如果转换后的pdf文件无中文,在wordtopdfutil转换方法里添加以下代码:

// 设置字体
string realpath = new applicationhome(wordtopdfutil.class).getsource().getparentfile().tostring();
fontsettings.setfontsfolder(realpath + file.separatorchar + "font", false);

excel中文无法转换

使用aspose-cells-20.7.jar:

<dependency>
	<groupid>com.aspose.cells</groupid>
	<artifactid>aspose-cells</artifactid>
	<version>20.7</version>
	<scope>system</scope>
	<systempath>${project.basedir}/lib/aspose-cells-20.7.jar</systempath>
</dependency>

并在exceltopdfutil转换方法里添加以下代码:

// 设置字体
string realpath = new applicationhome(wordtopdfutil.class).getsource().getparentfile().tostring();
string fontdir = realpath + file.separatorchar + "font";
individualfontconfigs individualfontconfigs = new individualfontconfigs();
individualfontconfigs.setfontfolder(fontdir, false);
loadoptions loadoptions = new loadoptions();
loadoptions.setfontconfigs(individualfontconfigs);
// 读取excel文件
workbook wb = new workbook(excelfilepath, loadoptions);

7.总结

pdf转换为其他格式的方法效果不佳,遇到好的方案会进行补充。

主要用到aspose的jar包,实际上是需要授权的,否则会有水印,是个隐患。

到此这篇关于基于java实现将word,excel文件转换为pdf的工具类的文章就介绍到这了,更多相关java word与excel转pdf内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com