PDFSharp填写表单字段 [英] PDFSharp filling in form fields

查看:679
本文介绍了PDFSharp填写表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想填补了预制PDF文档表单字段,但运行时,我收到一个空Refrence误差AcroForm。

I would like to fill in form fields in a premade PDF doc, but I'm receiving a Null Refrence error with AcroForm when running.

 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);



所以 PdfTextField currentField =(PdfTextField)(document.AcroForm.Fields [ < CASENUM>中]); 是错误发生。它接缝的AcroForm甚至没有认识到各个领域。

So PdfTextField currentField = (PdfTextField)(document.AcroForm.Fields["<CASENUM>"]); is where the error happens. It seams that AcroForm is not even recognizing the fields.

另一种选择将是一个查找和替换PDF中的文字(不使用iTextSharp的是,由于不能使用许可)。

Another option would be a find and replace text in a PDF (without using itextsharp as cannot use due to licensing).

任何帮助将是真棒!

推荐答案

您还需要这种如果你正试图填充PDF表单域,您还需要元素设置NeedsAppearances为true。否则,将PDF隐藏的表单上的值。这里是VB代码。

You also need this if you are attempting to populate PDF form fields, you also need to set the NeedsAppearances element to true. Otherwise the PDF will "hide" the values on the form. Here is the VB code.

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

这篇关于PDFSharp填写表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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