Unity 结合 iText7 打包后 PDF不显示中文

环境

Unity2019.4.3.4 f1c1
iText7 7.2.0

问题

Unity编辑模式运行 可以生成PDF, 但项目打包后不能生成PDF

问题原因

Encoding 1252 data could not be found. Make sure you have correct international codeset assembly installed and enabled. 

找不到编码 1252 的数据。 确保您安装并启用了正确的国际代码集程序集。

解决方法

  1. 找到Unity编辑器的安装路径
    例如:
    2019.4.34f1c1\Editor\Data\MonoBleedingEdge\lib\mono\unityjit
  2. 找到以下dll
    I18N.CJK.dll 、I18N.dll、I18N.MidEast.dll
    I18N.Other.dll、I18N.Rare.dll、I18N.West.dll
  3. 将这些dll放入打包后的文件中
    XXXX_Data\Managed

测试

using UnityEngine;
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Kernel.Font;
using iText.IO.Font;
public class IText7 : MonoBehaviour
{
    void Start()
    {
        FileInfo file = new FileInfo("./itext7.pdf");
        file.Directory.Create();
        CreatePdf("./itext7.pdf");
    }

    public void CreatePdf(String dest)
    {
        PdfWriter writer = new PdfWriter(dest);
        PdfDocument pdf = new PdfDocument(writer);
        Document document = new Document(pdf);
        try
        {
            PdfFont font = null;       
            if (File.Exists("./msyh.ttf"))
            {
                Debug.Log("字体文件存在");
                //注意:字体文件直接放在 打包文件中 测试使用 字体文件路径
                font = PdfFontFactory.CreateFont("./msyh.ttf", PdfEncodings.IDENTITY_H );
                document.Add(new Paragraph("中文").SetFont(font));
            }
        }
        catch (Exception e)
        {
            Debug.Log("字体错误");
            Debug.Log("错误原因:" + e.Message);
            Debug.Log("错误在何处:" + e.StackTrace);
            Debug.Log("错误程序集位置:" + e.Source);
        }
        document.Close();
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值