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包,实际上是需要授权的,否则会有水印,是个隐患。
目录
相关文章
|
1月前
|
XML 物联网 API
服务端和客户端 RESTful 接口上传 Excel 的 Python 代码
本文作者木头左是物联网工程师,分享如何使用 Python 和 Flask-RESTful 构建一个简单的 RESTful API,实现文件上传功能,特别支持Excel文件。通过安装Flask和Flask-RESTful库,创建Flask应用,实现文件上传接口,并将其添加到API。该方法具有简单易用、灵活、可扩展及社区支持等优点。
服务端和客户端 RESTful 接口上传 Excel 的 Python 代码
|
1月前
|
Java
java处理pdf代码
java处理pdf代码
23 0
|
2天前
|
数据格式 Python
Python代码示例,读取excel表格,将行数据转为列数据。(10)
【7月更文挑战第10天】Python代码示例,读取excel表格,将行数据转为列数据。
17 2
|
11天前
|
JavaScript 数据库
文本,在线浏览PDF,一个最简单的文档标准样式,文档预览非常简单的样式,文档管理样式设计,标准,好的设计
文本,在线浏览PDF,一个最简单的文档标准样式,文档预览非常简单的样式,文档管理样式设计,标准,好的设计
|
22天前
|
Java 区块链
用Java将ico格式转 PNG/JPG等格式
用Java将ico格式转 PNG/JPG等格式
14 1
|
2天前
|
Unix Linux Shell
Sphinx是一个Python文档生成工具,它可以解析reStructuredText或Markdown格式的源代码注释,并生成多种输出格式,如HTML、LaTeX、PDF、ePub等。
Sphinx是一个Python文档生成工具,它可以解析reStructuredText或Markdown格式的源代码注释,并生成多种输出格式,如HTML、LaTeX、PDF、ePub等。
5 0
|
25天前
|
Java C语言
Java微信语音amr格式转mp3格式
Java微信语音amr格式转mp3格式
|
1月前
|
编解码 文字识别
印刷文字识别操作报错合集之在尝试将PDF文件转换为图片时出现了问题,具体的错误代码是415,该怎么处理
在使用印刷文字识别(OCR)服务时,可能会遇到各种错误。例如:1.Java异常、2.配置文件错误、3.服务未开通、4.HTTP错误码、5.权限问题(403 Forbidden)、6.调用拒绝(Refused)、7.智能纠错问题、8.图片质量或格式问题,以下是一些常见错误及其可能的原因和解决方案的合集。
|
18天前
|
Java Maven
使用Java合并PDF文档
使用Java合并PDF文档
11 0
|
21天前
|
Java
使用java文件过滤器输出制定格式文件路径
使用java文件过滤器输出制定格式文件路径
9 0