简体   繁体   English

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

[英]Null Reference Exception when calling iText7 PdfAcroForm.GetAcroForm() in .Net Core 3.1 class library

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.我正在将应用程序转换为 .Net Core 3.1,在我的类库中,我正在从现有模板生成一个 PDF 表单,并用数据填充该表单。 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.在 IText7 的前身 ITextSharp 中,PdfAcroForm 静态方法“.GetAcroForm()”工作得很好,但在 iText7 (7.1.12) 的当前版本中,会抛出空引用异常。 I have followed the documentation to the best of my ability, but I am unsure how to continue.我已尽我所能遵循文档,但我不确定如何继续。 Any suggestions would be appreciated.任何建议,将不胜感激。

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.注意:模板路径存在,新文档显示已正确填写,无法“新建”一个PdfAcroForm,需要使用静态的.GetAcroForm()方法。

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.该文档表明,如果参数“createNotExist”设置为 true,则 .GetAcroForm() 方法将创建一个新表单,我已在此处完成。

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我已经研究并在 iText GitHub 上找到了一个问题,该问题表明该问题在一年前已“修复”: 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; }; 
        }

Any suggestions would be greatly appreciated任何建议将不胜感激

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.在纠正之前,您可以安全地绕过 Visual Studio 中的异常。 This has no negative impact on the functionality and is the result of a misplaced return in the original iText7 source.这对功能没有负面影响,并且是原始 iText7 源中错误返回的结果。

I had the same problem, and after digging down all the way to iText7's internal objects and methods, I finally "solved" my problem.我也遇到了同样的问题,在一直挖掘到iText7的内部对象和方法之后,我终于“解决”了我的问题。

Apparently iText has some internal errors/exceptions that they are just sort of "skipping" and "pushing past", because I realized by accident that I had "Enable Just My Code" in Visual Studios disabled, and so my system was trying to debug iText7's code as well as mine.显然 iText 有一些内部错误/异常,它们只是“跳过”和“推过去”,因为我偶然意识到我在 Visual Studios 中禁用了“仅启用我的代码”,因此我的系统正在尝试调试iText7 的代码和我的一样。 The moment that I re-enabled it in my Visual Studio settings (Tools > Options > Debugging > General > Enable Just My Code checkbox), the problem magically went away.当我在 Visual Studio 设置(工具 > 选项 > 调试 > 常规 > 启用仅我的代码复选框)中重新启用它时,问题就神奇地消失了。

Visual Studio 中的设置

So I spent four hours trying to troubleshoot a problem that was in THEIR code, but that they apparently found some way to work around and push through the method anyways even on a null reference failure.所以我花了四个小时试图解决他们代码中的问题,但他们显然找到了一些解决方法,即使在空引用失败的情况下也能通过该方法。

My convert to PDF function is now working just fine.我的转换为 PDF 功能现在工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM