Java【代码 16】将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理

简介: 【2月更文挑战第3天】Java 将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理

1.感谢

感谢小伙伴儿的分享:
不羁
郭中天
整合调整后的工具类Gitee地址https://gitee.com/yuanzhengme/java_application_aspose_demo

2.包含的工具类

● WordToPdfUtil用于将word文档转换为pdf格式的工具类
● ExcelToPdfUtil用于将excel文档转换为pdf格式的工具类
● PdfToImageUtil用于将pdf文档转换为image格式的工具类

3.lib文件说明

3.1 使用的

● 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端中文出现乱码时使用)

3.2 未使用的

● aspose-words-15.12.0-jdk16.jar 未测试
● aspose-pdf-22.4.cracked.jar 将pdf转换为其他格式【破解版效果不佳】
● aspose-pdf-22.4.jar 将pdf转换为其他格式【未破解效果依然不佳】

4.核心代码

4.1 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();
            }

        }
    }

4.2 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();
            }
        }
    }

4.3 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();
        }
    }

6.问题处理

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

    6.1 Word中文无法转换

    在Linux环境下,如果转换后的pdf文件无中文,在WordToPdfUtil转换方法里添加以下代码:
    // 设置字体
    String realPath = new ApplicationHome(WordToPdfUtil.class).getSource().getParentFile().toString();
    FontSettings.setFontsFolder(realPath + File.separatorChar + "font", false);
    

    6.2 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包,实际上是需要授权的,否则会有水印,是个隐患。
目录
相关文章
|
2月前
|
C# 开发者 Windows
WPF遇上Office:一场关于Word与Excel自动化操作的技术盛宴,从环境搭建到代码实战,看WPF如何玩转文档处理的那些事儿
【8月更文挑战第31天】Windows Presentation Foundation (WPF) 是 .NET Framework 的重要组件,以其强大的图形界面和灵活的数据绑定功能著称。本文通过具体示例代码,介绍如何在 WPF 应用中实现 Word 和 Excel 文档的自动化操作,包括文档的读取、编辑和保存等。首先创建 WPF 项目并设计用户界面,然后在 `MainWindow.xaml.cs` 中编写逻辑代码,利用 `Microsoft.Office.Interop` 命名空间实现 Office 文档的自动化处理。文章还提供了注意事项,帮助开发者避免常见问题。
114 0
|
2月前
|
C# 开发者 Windows
WPF与PDF文档:解锁创建和编辑PDF文件的新技能——从环境配置到代码实践,手把手教你如何在WPF应用中高效处理PDF,提升文档管理效率
【8月更文挑战第31天】随着数字文档的普及,PDF因跨平台兼容性和高保真度成为重要格式。WPF虽不直接支持PDF处理,但借助第三方库(如iTextSharp)可在WPF应用中实现PDF的创建与编辑。本文通过具体案例和示例代码,详细介绍了如何在WPF中集成PDF库,并展示了从设计用户界面到实现PDF创建与编辑的完整流程。不仅包括创建新文档的基本步骤,还涉及在现有PDF中添加页眉页脚等高级功能。通过这些示例,WPF开发者可以更好地掌握PDF处理技术,提升应用程序的功能性和实用性。
47 0
[PDF提取重命名]提取识别文字并对PDF文件批量重命名,提取PDF指定可复制的内容并批量重命名PDF,批量PDF文档指定识别提取区域
本文介绍一款实用工具,能快速从可复制内容的PDF中提取指定区域信息并据此重命名文件。设置提取坐标及导入PDF文档、设定新文件名后启动提取流程,即可高效批量处理。保存坐标设置以便重复使用,适用于需频繁修改大量PDF文件名的场景。
[PDF提取重命名]提取识别文字并对PDF文件批量重命名,提取PDF指定可复制的内容并批量重命名PDF,批量PDF文档指定识别提取区域
|
2月前
|
开发框架 前端开发 JavaScript
在Winform分页控件中集成导出PDF文档的功能
在Winform分页控件中集成导出PDF文档的功能
|
2月前
|
缓存 Java 数据处理
|
2月前
|
Java
Java模拟文件发送给服务器,服务器将文件转发给其他用户,并保存到服务器本地,其他用户可以接收,并保存到本地磁盘,支持各种文件格式,并解决通信中服务器怎么区分客户端发来的文件类型
Java模拟文件发送给服务器,服务器将文件转发给其他用户,并保存到服务器本地,其他用户可以接收,并保存到本地磁盘,支持各种文件格式,并解决通信中服务器怎么区分客户端发来的文件类型
|
3月前
|
Java 编译器 开发者
Java演进问题之Truffle处理不同编程语言的源代码或中间格式如何解决
Java演进问题之Truffle处理不同编程语言的源代码或中间格式如何解决
|
7天前
|
安全 Java 调度
Java编程时多线程操作单核服务器可以不加锁吗?
Java编程时多线程操作单核服务器可以不加锁吗?
21 2
|
11天前
|
存储 缓存 Java
java线程内存模型底层实现原理
java线程内存模型底层实现原理
java线程内存模型底层实现原理
|
15天前
|
缓存 Java 应用服务中间件
Java虚拟线程探究与性能解析
本文主要介绍了阿里云在Java-虚拟-线程任务中的新进展和技术细节。
下一篇
无影云桌面