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
        }
    }
}
相关推荐
2401_826097624 小时前
JavaEE-Linux环境部署
java·linux·java-ee
(:满天星:)6 小时前
第31篇:块设备与字符设备管理深度解析(基于OpenEuler 24.03)
linux·运维·服务器·网络·centos
爱莉希雅&&&6 小时前
shell编程之awk命令详解
linux·服务器·git
笑稀了的野生俊6 小时前
在服务器中下载 HuggingFace 模型:终极指南
linux·服务器·python·bash·gpu算力
渡我白衣6 小时前
Linux操作系统之文件(四):文件系统(上)
linux
ZZH1120KQ6 小时前
Linux系统安全及应用
linux·运维·系统安全
程序漫游人7 小时前
centos8.5安装jdk21详细安装教程
java·linux
小小小糖果人7 小时前
Linux云计算基础篇(5)
linux·运维·服务器
small_wh1te_coder7 小时前
硬件嵌入式学习路线大总结(一):C语言与linux。内功心法——从入门到精通,彻底打通你的任督二脉!
linux·c语言·汇编·嵌入式硬件·算法·c
小张是铁粉7 小时前
docker在Linux的安装遇到的问题
linux·docker·容器