在 .Net Core 3.1 类库中调用 iText7 PdfAcroForm.GetAcroForm() 时出现空引用异常 [英] Null Reference Exception when calling iText7 PdfAcroForm.GetAcroForm() in .Net Core 3.1 class library

查看:19
本文介绍了在 .Net Core 3.1 类库中调用 iText7 PdfAcroForm.GetAcroForm() 时出现空引用异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on converting an application to .Net Core 3.1, and in my class library I am generating a PDF form from an existing template, and filling that form with data. In ITextSharp, the predecessor to IText7, the PdfAcroForm static method ".GetAcroForm()" worked perfectly, but in the current version of iText7 (7.1.12) a Null Reference Exception is thrown. I have followed the documentation to the best of my ability, but I am unsure how to continue. Any suggestions would be appreciated.

注意:模板路径存在,新建文档显示已填妥,无法新建"一个 PdfAcroForm,你需要使用静态的 .GetAcroForm() 方法.

NOTE: The template path exists, the new document shows that it has been filled properly, and it is impossible to "new" a PdfAcroForm, you are required to use the static .GetAcroForm() method.

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

A null check will not solve this issue, as the object should never be null. The documentation indicates that the .GetAcroForm() method will create a new form if the parameter "createNotExist" is set to true, which I have done here.

我在 iText GitHub 上研究并发现了一个问题,表明此问题已修复";大约一年前:https://github.com/itext/itext7/pull/44#issue-351612749

I have researched and have located an issue on the iText GitHub that indicates that this issue was "fixed" around a year ago: https://github.com/itext/itext7/pull/44#issue-351612749

以下是准备表格的方法:

The following is the method which prepares the forms:

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 (FormFieldSet fs in formFieldSet)
                {
                    acroForm.GetField(fs.FieldName).SetValue(fs.FillValue);
                }

                // Sets form flattening
                acroForm.FlattenFields();

                // Closes and writes the form
                newDocument.Close();

                return newFile;
            }
            catch { return string.Empty; }; 
        }

任何建议将不胜感激

推荐答案

只是对寻找此问题的任何人的更新.这是一个已知问题,已在当前开发分支中修复.在纠正之前,您可以安全地绕过 Visual Studio 中的异常.这对功能没有负面影响,是原始 iText7 源中错误返回的结果.

Just an update to anyone looking for this issue. This is a known issue and is fixed in the current development branch. You are safe to bypass the exception in visual studio until it is corrected. This has no negative impact on the functionality and is the result of a misplaced return in the original iText7 source.

这篇关于在 .Net Core 3.1 类库中调用 iText7 PdfAcroForm.GetAcroForm() 时出现空引用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆