c# - PDFSharp 填写表单域

标签 c# pdf pdfsharp

我想在预制 PDF 文档中填写表单字段,但在运行 AcroForm 时收到空引用错误。

 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 甚至无法识别字段。

另一种选择是查找和替换 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

关于c# - PDFSharp 填写表单域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6240585/

相关文章:

c# - 用于嵌入字体 : System. ArgumentException 的 PDFsharp/MigraDoc 字体解析器:找不到字体 'Ubuntu'

c# - 在 Pdf 上绘制成窗口窗体 C# (visual studio 2015)

c# - 使用 XmlDocument 向元素添加命名空间的正确方法

WPF 网络浏览器在 Adob​​e 阅读器窗口中打开 PDF 文件

php - 带有 MPDF 的 PDF 页面周围的边框

r - 在 R : grid. 表中打印数据框输出裁剪后的表,不响应字体大小

c# - PDFsharp 文档

c# - 在 .Net 应用程序中包含单元测试时的设计注意事项?

c# - 数据集是否与记录集相同

c# - 使用 C# 自定义配置