在 .Net Core 3.1 class 库中调用 iText7 PdfAcroForm.GetAcroForm() 时出现空引用异常
Null Reference Exception when calling iText7 PdfAcroForm.GetAcroForm() in .Net Core 3.1 class library
我正在将应用程序转换为 .Net Core 3.1,在我的 class 库中,我正在从现有模板生成 PDF 表单,并用数据填充该表单。在 IText7 的前身 ITextSharp 中,PdfAcroForm 静态方法“.GetAcroForm()”工作得很好,但在当前版本的 iText7 (7.1.12) 中会抛出空引用异常。我已尽我所能遵循文档,但我不确定如何继续。如有任何建议,我们将不胜感激。
注意:模板路径存在,新建文档显示填写正确,无法“新建”一个PdfAcroForm,需要使用static.GetAcroForm()方法
null 检查不会解决这个问题,因为对象永远不应该为 null。文档表明,如果参数“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 (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 源中错放 return 的结果。
我也遇到了同样的问题,在一路深入到iText7的内部对象和方法之后,我终于“解决”了我的问题。
显然 iText 有一些内部 errors/exceptions 它们只是某种“跳过”和“推过去”,因为我偶然意识到我在 Visual Studio 中有“仅启用我的代码”它被禁用了,所以我的系统正在尝试调试 iText7 的代码以及我的代码。当我在 Visual Studio 设置(工具 > 选项 > 调试 > 常规 > 启用我的代码复选框)中重新启用它时,问题神奇地消失了。
所以我花了四个小时试图解决他们代码中的问题,但他们显然找到了一些解决方法,即使在空引用失败的情况下也能通过该方法。
我的转换为 PDF 功能现在工作正常。
我正在将应用程序转换为 .Net Core 3.1,在我的 class 库中,我正在从现有模板生成 PDF 表单,并用数据填充该表单。在 IText7 的前身 ITextSharp 中,PdfAcroForm 静态方法“.GetAcroForm()”工作得很好,但在当前版本的 iText7 (7.1.12) 中会抛出空引用异常。我已尽我所能遵循文档,但我不确定如何继续。如有任何建议,我们将不胜感激。
注意:模板路径存在,新建文档显示填写正确,无法“新建”一个PdfAcroForm,需要使用static.GetAcroForm()方法
null 检查不会解决这个问题,因为对象永远不应该为 null。文档表明,如果参数“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 (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 源中错放 return 的结果。
我也遇到了同样的问题,在一路深入到iText7的内部对象和方法之后,我终于“解决”了我的问题。
显然 iText 有一些内部 errors/exceptions 它们只是某种“跳过”和“推过去”,因为我偶然意识到我在 Visual Studio 中有“仅启用我的代码”它被禁用了,所以我的系统正在尝试调试 iText7 的代码以及我的代码。当我在 Visual Studio 设置(工具 > 选项 > 调试 > 常规 > 启用我的代码复选框)中重新启用它时,问题神奇地消失了。
所以我花了四个小时试图解决他们代码中的问题,但他们显然找到了一些解决方法,即使在空引用失败的情况下也能通过该方法。
我的转换为 PDF 功能现在工作正常。