【问题标题】:PDFsharp Beta 1.50 PdfTextField, Null Exception Error, But Still Works?PDFsharp Beta 1.50 PdfTextField,空异常错误,但仍然有效?
【发布时间】:2016-07-12 15:13:35
【问题描述】:

我正在使用 PDFsharp 1.50 beta 3b。我主要使用它来访问使用更新的 PDF 文档的能力。我没有使用任何更新的功能。向下转换我的 PDF 文档正在杀死它们,我不知道为什么。也就是说;

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

这会产生错误;

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

现在是什么让这很奇怪,我问的原因是它仍然适用于 try/catch 块。它将填充该字段,并且该文件在 PDF 文件中具有正确的文本。我只想知道为什么会抛出这个异常?

【问题讨论】:

  • 这里的2886行是哪一行?
  • CType(form.Fields("StringTest"), PdfSharp.Pdf.AcroForms.PdfTextField).Text = "Test"

标签: vb.net pdfsharp


【解决方案1】:

我发现了这个问题。新的 PDFSharp 对其控件使用不同的访问方法。

首先,我们的声明;

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

对于复选框;

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

对于字符串;

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

这样做是可行的,不需要 try/catch 块,也不会引发错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-11
    • 2015-06-21
    • 2012-10-30
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多