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
        }
    }
}
相关推荐
qwfys2009 分钟前
How to configure Linux mint desktop
linux·desktop·configure·mint
南方以南_13 分钟前
Ubuntu操作合集
linux·运维·ubuntu
冼紫菜1 小时前
[特殊字符]CentOS 7.6 安装 JDK 11(适配国内服务器环境)
java·linux·服务器·后端·centos
大强同学1 小时前
Adobe DC 2025安装教程
windows·adobe·pdf
qq_29494019102 小时前
WORD个人简历单页326款模版分享下载
word·个人简历·doc模版
JOYUAGV2 小时前
Word压缩解决方案
python·word
Chuncheng's blog2 小时前
RedHat7 如何更换yum镜像源
linux
爱莉希雅&&&2 小时前
shell脚本之条件判断,循环控制,exit详解
linux·运维·服务器·ssh
wei_work@3 小时前
【linux】Web服务—搭建nginx+ssl的加密认证web服务器
linux·服务器·ssl
扶尔魔ocy3 小时前
【Linux C/C++开发】轻量级关系型数据库SQLite开发(包含性能测试代码)
linux·数据库·c++·sqlite