Kayot
Kayot

Reputation: 602

PDFsharp Beta 1.50 PdfTextField, Null Exception Error, But Still Works?

I'm using PDFsharp 1.50 beta 3b. I'm mainly using it to access the ability to use newer PDF Documents. I'm not using any newer features. Down converting my PDF docs is killing them and I don't know why. That said;

Private Sub Print_Form()

    Dim filename As String = ""

    If IO.File.Exists(String.Format("{0}Template\Form.pdf", AppDomain.CurrentDomain.BaseDirectory)) Then
        filename = String.Format("{0}Template\Form.pdf", AppDomain.CurrentDomain.BaseDirectory)
    Else
        MessageBox.Show("You're missing the Templates directory. If you don't know what this means, tell your IT Administrator.", "Missing Files")
        Exit Sub
    End If

    Dim PDFDocument As PdfSharp.Pdf.PdfDocument = PdfSharp.Pdf.IO.PdfReader.Open(filename, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Modify)
    Dim form As PdfSharp.Pdf.AcroForms.PdfAcroForm = PDFDocument.AcroForm

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

    Try
        'the subsequent line causes the exception to be thrown
        CType(form.Fields("StringTest"), PdfSharp.Pdf.AcroForms.PdfTextField).Text = "Test"
    Catch ex As Exception
        Clipboard.SetText(ex.StackTrace)
    End Try

    CType(form.Fields("CheckBoxTest"), PdfSharp.Pdf.AcroForms.PdfCheckBoxField).Checked = True

    PDFDocument.Save("temp.pdf")
    Dim p As New System.Diagnostics.ProcessStartInfo()
    p.Verb = "print"
    p.WindowStyle = ProcessWindowStyle.Hidden
    p.FileName = "temp.pdf"
    p.UseShellExecute = True
    System.Diagnostics.Process.Start(p)

End Sub

This creates an error;

An unhandled exception of type 'System.NullReferenceException' occurred in PdfSharp.dll

Additional information: Object reference not set to an instance of an object.

at PdfSharp.Pdf.AcroForms.PdfTextField.RenderAppearance()
at PdfSharp.Pdf.AcroForms.PdfTextField.set_Text(String value)
at WOTC_FE.frmInterview.Print_ICF() in d:\Programming\FE\FE\Applications\frmInterview.vb:line 2886

Now what makes this weird and why I'm asking is that this still works with the try/catch block. It will fill the field and the file has the correct text in the PDF file. I just want to know why does it throw this exception?

Upvotes: 2

Views: 2027

Answers (1)

Kayot
Kayot

Reputation: 602

I figured out the issue. The new PDFSharp uses a different access method for it's controls.

First, our declarations;

Dim PDFDocument As PdfSharp.Pdf.PdfDocument = PdfSharp.Pdf.IO.PdfReader.Open(filename, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Modify)
Dim form As PdfSharp.Pdf.AcroForms.PdfAcroForm = PDFDocument.AcroForm

For checkboxes;

CType(form.Fields(<Field Name>), PdfSharp.Pdf.AcroForms.PdfCheckBoxField).Checked = True

And for strings;

PDFDocument.AcroForm.Fields("Field Name").Value = New PdfSharp.Pdf.PdfString("Input Text")

Doing it this way works, doesn't need a try/catch block, and doesn't throw an error.

Upvotes: 3

Related Questions