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 的数据。 确保您安装并启用了正确的国际代码集程序集。
解决方法- 找到Unity编辑器的安装路径
例如:
2019.4.34f1c1EditorDataMonoBleedingEdgelibmonounityjit - 找到以下dll
I18N.CJK.dll 、I18N.dll、I18N.MidEast.dll
I18N.Other.dll、I18N.Rare.dll、I18N.West.dll - 将这些dll放入打包后的文件中
XXXX_DataManaged
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();
}
}



