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
        }
    }
}
相关推荐
冰橙子id2 小时前
linux-远程访问管理(sshd,scp,sftp)
linux·网络·ssh
光电的一只菜鸡3 小时前
ubuntu之坑(十五)——设备树
linux·数据库·ubuntu
saynaihe4 小时前
ubuntu 22.04 anaconda comfyui安装
linux·运维·服务器·ubuntu
企鹅与蟒蛇5 小时前
Ubuntu-25.04 Wayland桌面环境安装Anaconda3之后无法启动anaconda-navigator问题解决
linux·运维·python·ubuntu·anaconda
程序设计实验室5 小时前
小心误关了NAS服务器!修改Linux的电源键功能
linux·nas
渡我白衣8 小时前
Linux操作系统之信号:信号的产生
linux
阿巴~阿巴~8 小时前
理解Linux文件系统:从物理存储到统一接口
linux·运维·服务器
tan77º9 小时前
【Linux网络编程】应用层自定义协议与序列化
linux·运维·服务器·网络·c++·tcp/ip
余大侠在劈柴9 小时前
pdf.js 开发指南:在 Web 项目中集成 PDF 预览功能
前端·javascript·学习·pdf
解牛之术10 小时前
Android展示加载PDF
android·pdf