PDF填写表单字段

use*_*344 20 c# pdf pdfsharp

我想在预制的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["<CASENUM>"]);
  //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);
Run Code Online (Sandbox Code Playgroud)

PdfTextField currentField = (PdfTextField)(document.AcroForm.Fields["<CASENUM>"]);错误发生的地方也是如此 .它接缝表明AcroForm甚至没有认识到这些领域.

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

任何帮助都是极好的!

小智 26

如果您尝试填充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
Run Code Online (Sandbox Code Playgroud)

  • 对于懒惰,这里是在C#:if(doc.AcroForm.Elements.ContainsKey("/ NeedAppearances")== false)doc.AcroForm.Elements.Add("/ NeedAppearances",new PdfBoolean(true)); else doc.AcroForm.Elements ["/ NeedAppearances"] = new PdfBoolean(true); (4认同)

Ram*_*ngh 9

今天早些时候我遇到了同样的问题。但是,我认为源代码已更新,因此如果您尝试上面的方法,您将收到 NullExceptionError。相反,对于 TextField,您需要生成 PdfString 并使用 testfield.Value 而不是 .text。这是一个例子。

      static PdfAccess()
        {
            Pdf.PdfDocument doc = Pdf.IO.PdfReader.Open(@"C:\...\ Contract.pdf", Pdf.IO.PdfDocumentOpenMode.Modify);
            Pdf.AcroForms.PdfAcroForm form = doc.AcroForm;

            if (form.Elements.ContainsKey("/NeedAppearances"))
            {
                form.Elements["/NeedAppearances"] = new PdfSharp.Pdf.PdfBoolean(true);
            }
            else
            {
                form.Elements.Add("/NeedAppearances", new PdfSharp.Pdf.PdfBoolean(true));
            }

           var name = (Pdf.AcroForms.PdfTextField)(form.Fields["Email"]);
           name.Value = new Pdf.PdfString("ramiboy");


            doc.Save(@"C:\...\ Contract.pdf");
            doc.Close();

Run Code Online (Sandbox Code Playgroud)


pau*_*ulH 7

我今天一直在研究这个,我已经设法创建了一个可行的解决方案。我在下面粘贴了我的工作代码。我的代码和 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.
Run Code Online (Sandbox Code Playgroud)


Mer*_*lin 5

我刚刚经历过类似的事情.我打开的第一个pdf文件不包含acroform数据,并导致如上所述的null异常.问题不在于打开pdf,而是对具有值null的Acroform成员变量的引用.您可以使用以下代码示例测试您的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");
        }
    }
Run Code Online (Sandbox Code Playgroud)

ADENDUM

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

document.AcroForm.Fields["<CASENUM>"]
Run Code Online (Sandbox Code Playgroud)

将其更改为

document.AcroForm.Fields["CASENUM"]
Run Code Online (Sandbox Code Playgroud)


Gat*_*ats 0

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

改变

PdfDocument document = PdfReader.Open(fileN4, PdfDocumentOpenMode.Modify);
Run Code Online (Sandbox Code Playgroud)

PdfDocument document = PdfReader.Open(Path.Combine(Directory.GetCurrentDirectory(), fileN4), PdfDocumentOpenMode.Modify);
Run Code Online (Sandbox Code Playgroud)

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