小编jos*_*019的帖子

在 .Net Core 3.1 类库中调用 iText7 PdfAcroForm.GetAcroForm() 时出现空引用异常

我正在努力将应用程序转换为 .Net Core 3.1,在我的类库中,我正在从现有模板生成 PDF 表单,并用数据填充该表单。在 ITextSharp(IText7 的前身)中,PdfAcroForm 静态方法“.GetAcroForm()”工作得很好,但在当前版本的 iText7 (7.1.12) 中会引发空引用异常。我已尽我所能遵循文档,但我不确定如何继续。任何建议,将不胜感激。

注意:模板路径存在,新文档显示已正确填写,并且无法“新建”PdfAcroForm,需要使用静态 .GetAcroForm() 方法。

空检查无法解决此问题,因为对象永远不应该为空。文档表明,如果参数“createNotExist”设置为 true,则 .GetAcroForm() 方法将创建一个新表单(我在这里已完成)。

我研究并在 iText GitHub 上找到了一个问题,表明该问题大约一年前已“修复”:https://github.com/itext/itext7/pull/44#issue-351612749

以下是准备表格的方法:

public string DocumentGenerator(string templatePath, FormFieldSet[] formFieldSet, bool useSpecailOutputPath)
        {
            if(!File.Exists(templatePath))
            {
                throw new Exception("The template file provided does not exist: MC-071(iText)"); 
            }

            string newFile = useSpecailOutputPath ? 
                m_SpecialOutputPath : 
                Path.GetTempPath() + Guid.NewGuid().ToString() + ".pdf";
            try
            {

                PdfDocument newDocument = new PdfDocument(new PdfReader(templatePath), new PdfWriter(newFile));
                PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(newDocument, true); // <=== Exception Thrown Here

                foreach …
Run Code Online (Sandbox Code Playgroud)

c# pdf itext .net-core itext7

1
推荐指数
2
解决办法
3039
查看次数

标签 统计

.net-core ×1

c# ×1

itext ×1

itext7 ×1

pdf ×1