27

既製の 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);

エラー PdfTextField currentField = (PdfTextField)(document.AcroForm.Fields["<CASENUM>"]);が発生する場所も同様です。AcroForm がフィールドを認識さえしていないようです。

別のオプションは、PDF 内のテキストを検索して置換することです (ライセンスのために使用できないため、itextsharp を使用しないでください)。

どんな助けでも素晴らしいでしょう!

4

6 に答える 6

30

これは、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
于 2012-08-28T11:47:41.050 に答える
7

私は今日これに取り組んでおり、実用的なソリューションを作成することができました。以下に作業コードを貼り付けました。私のコードとOPの間で私が見ることができる唯一の本当の違いは次のとおりです:

  • NeedAppearancesを設定するための Marc Ferree のコードを含めました(+1 と、どうもありがとうございました!!)
  • PdfStringを使用してValueプロパティではなく、 String変数を使用してフィールドのTextプロパティを設定しました。

うまくいけば、これは誰かが同じことをしようとしているのに役立つでしょう.

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.
于 2013-02-26T18:34:33.360 に答える
6

私はちょうどこれに似たようなことを経験しました。最初に開いた 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");
        }
    }

補遺

また、このコード行のキーには山かっこがあってはならないことに気付きました

document.AcroForm.Fields["<CASENUM>"]

に変更します

document.AcroForm.Fields["CASENUM"]
于 2011-07-12T23:08:53.937 に答える
4

私は今日、この同じ問題に行き詰まりました。ただ、ソースコードが?updatedになっていると思いますので、上記の方法を試すとNullExceptionErrorが発生します。代わりに、TextField の場合、PdfString を生成し、.text の代わりに testfield.Value を使用する必要があります。これが例です。

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

于 2019-10-10T01:27:27.443 に答える
1

を克服するための解決策NullReferenceExceptionは、Adobe Acrobatで作成済みのPDFを開き、プロパティタイプを以外に変更することにより、フォームフィールドにデフォルト値を与えることですnull

于 2012-09-19T08:39:17.083 に答える
0

開こうとしたときに現在のディレクトリを入れてみましたか?

変化する

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

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

私はPDFの作成にASPOSEしか使用していませんが、PdfReaderには完全なファイルパスが必要になると確信しています。

于 2011-06-05T02:13:58.807 に答える