Linux word转pdf汉字没有乱码,但是dataMap.put(“userTrainedOper1“, “\u2611“);填充单选框时乱码

原来的,只支持汉字,不支持Unicode

javascript 复制代码
package com.gangwantech.web.utils;

import com.aspose.words.*;
import net.coobird.thumbnailator.Thumbnails;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * @Author: LiHuaZhi
 * @Date: 2021/7/13 14:21
 * @Description:
 **/
public class Doc2PdfUtil {

    /**
     * 加载授权配置文件
     *
     * @return
     */
    private static boolean getLicense() {
        boolean result = false;
        try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("license.xml")) {
            // License的包路径必须为com.aspose.words.License
            License license = new License();
            license.setLicense(in);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * doc转pdf
     *
     * @return
     */
    public static byte[] doc2Pdf(byte[] bytes) {
        System.out.println("pdf转换中...");
        long old = System.currentTimeMillis();

        try (ByteArrayOutputStream fos = new ByteArrayOutputStream()) {
            // 验证
            if (!getLicense()) {
                throw new RuntimeException("文件转换失败!");
            }
            // 加载字体
            //FontSettings settings = FontSettings.getDefaultInstance();
            //String[] fontFamilyNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
            //settings.setFontsFolders(fontFamilyNames, true);
            //LoadOptions loadOptions = new LoadOptions();
            //loadOptions.setFontSettings(settings);

             加载字体
            //FontSettings settings = FontSettings.getDefaultInstance();
            //settings.setFontsFolder("C:\\Windows\\Fonts", true);
            //LoadOptions loadOptions = new LoadOptions();
            //loadOptions.setFontSettings(settings);

            ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
            //Document document = new Document(inputStream, loadOptions);
            Document document = new Document(inputStream);

            document.save(fos, SaveFormat.PDF);
            byte[] buffer = fos.toByteArray();
            long now = System.currentTimeMillis();
            System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒");
            return buffer;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("文件转换失败!");
        }
    }


    public static byte[] rotateUsingThumbnailatorPng(String urlAddress, double angle) throws IOException {
        URL url = new URL(urlAddress);
        BufferedImage originalImage = ImageIO.read(url);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        // 直接使用Thumbnailator进行旋转操作,并写入到ByteArrayOutputStream
        Thumbnails.of(originalImage)
                .scale(1.0)
                .rotate(angle)
                .outputFormat("PNG") // 根据需要可更改输出格式
                .toOutputStream(baos);

        // 使用ByteArrayInputStream读取处理后的字节数组并转换为BufferedImage
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

        try {
            BufferedImage read = ImageIO.read(bais);
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(read, "PNG", os); // 注意这里是写为PNG格式,根据实际情况可以调整为JPEG等其他格式
            return os.toByteArray();
        } finally {
            bais.close(); // 关闭ByteArrayInputStream
        }
    }
}

现在的支持汉字和Unicode

javascript 复制代码
package com.gangwantech.web.utils;

import com.aspose.words.*;
import net.coobird.thumbnailator.Thumbnails;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * @Author: LiHuaZhi
 * @Date: 2021/7/13 14:21
 * @Description:
 **/
public class Doc2PdfUtil {

    private static final String DEFAULT_FONT_FOLDER = "/usr/share/fonts/noto/"; // Linux环境下默认字体目录

    /**
     * 加载授权配置文件
     *
     * @return
     */
    private static boolean getLicense() {
        boolean result = false;
        try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("license.xml")) {
            // License的包路径必须为com.aspose.words.License
            License license = new License();
            license.setLicense(in);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * doc转pdf
     *
     * @return
     */
    public static byte[] doc2Pdf(byte[] bytes) {
        System.out.println("pdf转换中...");
        long old = System.currentTimeMillis();

        try (ByteArrayOutputStream fos = new ByteArrayOutputStream()) {
            // 验证
            if (!getLicense()) {
                throw new RuntimeException("文件转换失败!");
            }


            // 设置字体目录以确保支持Unicode字符
            FontSettings fontSettings = new FontSettings();
            fontSettings.setFontsFolder(DEFAULT_FONT_FOLDER, true);

            LoadOptions loadOptions = new LoadOptions();
            loadOptions.setFontSettings(fontSettings);

            ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
            Document document = new Document(inputStream, loadOptions);

            document.save(fos, SaveFormat.PDF);
            byte[] buffer = fos.toByteArray();
            long now = System.currentTimeMillis();
            System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒");
            return buffer;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("文件转换失败!");
        }
    }


    public static byte[] rotateUsingThumbnailatorPng(String urlAddress, double angle) throws IOException {
        URL url = new URL(urlAddress);
        BufferedImage originalImage = ImageIO.read(url);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        // 直接使用Thumbnailator进行旋转操作,并写入到ByteArrayOutputStream
        Thumbnails.of(originalImage)
                .scale(1.0)
                .rotate(angle)
                .outputFormat("PNG") // 根据需要可更改输出格式
                .toOutputStream(baos);

        // 使用ByteArrayInputStream读取处理后的字节数组并转换为BufferedImage
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

        try {
            BufferedImage read = ImageIO.read(bais);
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(read, "PNG", os); // 注意这里是写为PNG格式,根据实际情况可以调整为JPEG等其他格式
            return os.toByteArray();
        } finally {
            bais.close(); // 关闭ByteArrayInputStream
        }
    }
}
相关推荐
AAA_自动化工程师2 分钟前
TIA博途中的程序导出为PDF格式的具体方法示例
pdf·tia博途·程序导出·pdf格式·具体方法
行云流水剑17 分钟前
【学习记录】如何使用 Python 提取 PDF 文件中的内容
python·学习·pdf
IDRSolutions_CN29 分钟前
PDF 转 HTML5 —— HTML5 填充图形不支持 Even-Odd 奇偶规则?(第二部分)
java·经验分享·pdf·软件工程·团队开发
呆萌的代Ma1 小时前
Cursor实现用excel数据填充word模版的方法
word·excel
yzx9910132 小时前
Linux 系统中的算法技巧与性能优化
linux·算法·性能优化
fengyehongWorld2 小时前
Linux Docker的简介
linux·docker
upp2 小时前
【bug】Error: /undefinedfilename in (/tmp/ocrmypdf.io.9xfn1e3b/origin.pdf)
ubuntu·pdf·bug·ghostscript
曹瑞曹瑞2 小时前
VMware导入vmdk文件
linux·运维·服务器
Johny_Zhao2 小时前
2025年6月Docker镜像加速失效终极解决方案
linux·网络·网络安全·docker·信息安全·kubernetes·云计算·containerd·yum源·系统运维
hello kitty w3 小时前
Python学习(7) ----- Python起源
linux·python·学习