PDF填写表单字段

我想在预制的PDF文档中填写表单字段,但是在运行时我收到了AcroForm的Null Refrence错误。

string fileN4 = TextBox1.Text + " LOG.pdf"; File.Copy(Path.Combine(textBox4.Text + "\\", fileN4), Path.Combine(Directory.GetCurrentDirectory(), fileN4), true); // Open the file PdfDocument document = PdfReader.Open(fileN4, PdfDocumentOpenMode.Modify); PdfTextField currentField = (PdfTextField)(document.AcroForm.Fields[""]); //const string caseName = TextBox1.Text; PdfString caseNamePdfStr = new PdfString(caseName); //set the value of this field currentField.Value = caseNamePdfStr; // Save the document... document.Save(fileN4); 

所以PdfTextField currentField = (PdfTextField)(document.AcroForm.Fields[""]); 是错误发生的地方。 它接缝AcroForm甚至没有认识到这些领域。

另一种选择是在PDF中查找和替换文本(不使用由于许可而无法使用的itextsharp)。

任何帮助都是极好的!

如果您尝试填充PDF表单字段,还需要将NeedsAppearances元素设置为true。 否则,PDF将“隐藏”表单上的值。 这是VB代码。

 If objPdfSharpDocument.AcroForm.Elements.ContainsKey("/NeedAppearances") = False Then objPdfSharpDocument.AcroForm.Elements.Add("/NeedAppearances", New PdfSharp.Pdf.PdfBoolean(True)) Else objPdfSharpDocument.AcroForm.Elements("/NeedAppearances") = New PdfSharp.Pdf.PdfBoolean(True) End If 

我刚刚经历过类似的事情。 我打开的第一个pdf文件不包含acroform数据,并导致如上所述的nullexception。 问题不在于打开pdf,而是对Acroform成员变量的引用值为null。 您可以使用以下代码示例测试您的pdf:

  OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == DialogResult.OK) { PdfDocument _document = null; try { _document = PdfReader.Open(ofd.FileName, PdfDocumentOpenMode.Modify); } catch(Exception ex) { MessageBox.Show(ex.Message,"FATAL"); //do any cleanup and return return; } if (_document != null) { if (_document.AcroForm != null) { MessageBox.Show("Acroform is object","SUCCEEDED"); //pass acroform to some function for processing _document.Save(@"C:\temp\newcopy.pdf"); } else { MessageBox.Show("Acroform is null","FAILED"); } } else { MessageBox.Show("Uknown error opening document","FAILED"); } } 

ADENDUM

我也注意到这行代码中的键不应该有尖括号

 document.AcroForm.Fields[""] 

将其更改为

 document.AcroForm.Fields["CASENUM"] 

我今天一直在努力,我已经设法创建了一个有效的解决方案。 我在下面粘贴了我的工作代码。 我可以在代码和OP之间看到的唯一真正的区别如下:

  • 我收录了Marc Ferree设置NeedAppearances的代码(+1和非常感谢!)
  • 我使用String变量设置字段的Text属性,而不使用PdfString设置Value属性。

希望这对尝试做同样事情的人有用。

 string templateDocPath = Server.MapPath("~/Documents/MyTemplate.pdf"); PdfDocument myTemplate = PdfReader.Open(templateDocPath, PdfDocumentOpenMode.Modify); PdfAcroForm form = myTemplate.AcroForm; if (form.Elements.ContainsKey("/NeedAppearances")) { form.Elements["/NeedAppearances"] = new PdfSharp.Pdf.PdfBoolean(true); } else { form.Elements.Add("/NeedAppearances", new PdfSharp.Pdf.PdfBoolean(true)); } PdfTextField testField = (PdfTextField)(form.Fields["TestField"]); testField.Text = "012345"; myTemplate.Save(Server.MapPath("~/Documents/Amended.pdf")); // Save to new file. 

克服NullReferenceException的解决方案是使用Adobe Acrobat打开预制PDF并通过将其属性类型更改为null以外的null来为表单字段提供默认值。

您尝试打开当前目录时是否尝试过将其放入?

更改

 PdfDocument document = PdfReader.Open(fileN4, PdfDocumentOpenMode.Modify); 

 PdfDocument document = PdfReader.Open(Path.Combine(Directory.GetCurrentDirectory(), fileN4), PdfDocumentOpenMode.Modify); 

我很确定PdfReader需要一个完整的文件路径,尽管我只使用ASPOSE来创建pdf。