vb.net - PDFsharp Beta 1.50 PdfTextField,空异常错误,但仍然有效?

标签 vb.net pdfsharp

我正在使用 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 block 。它将填充该字段,并且该文件在 PDF 文件中具有正确的文本。我只是想知道为什么它会抛出这个异常?

最佳答案

我发现了这个问题。新的 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 block ,并且不会抛出错误。

关于vb.net - PDFsharp Beta 1.50 PdfTextField,空异常错误,但仍然有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38332832/

相关文章:

vb.net - Devexpress Gridview editform如何在中间打开?

vb.net - VB - 使用 StreamWriter 写入文件

c# - 如何在同一个 ASP.NET Web 窗体应用程序中隐藏 c# 和 vb.net 代码?

json - VB.net JSON 反序列化

c# - PDFSharp 不处理资源(内存泄漏)

c# - PDFsharp 在图形下绘制文本

c# - 什么是 Environment.FailFast?

.net - 单声道 PdfSharp

c# - 如何调整 PDF 文档的大小以适合位图

c# - 如何使用动态数据创建 PDFsharp 表格?