2016-07-12 9 views
1

私はPDFsharp 1.50 beta 3bを使用しています。私は主に、新しいPDFドキュメントを使用する機能にアクセスするためにそれを使用しています。私は新しい機能を使用していません。ダウンして私のPDF文書を変換すると、それらを殺していると私はなぜか分からない。それは言った。PDFsharp Beta 1.50 PdfTextField、Null例外エラー、それでも動作しますか?

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ファイルの正しいテキストを持っています。私はちょうどなぜこの例外をスローするのか知りたいですか?

+0

ここで2886行目はどれですか? – NePh

+0

CType(form.Fields( "StringTest")、PdfSharp.Pdf.AcroForms.PdfTextField).Text = "Test" – Kayot

答えて

2

私は問題を把握しました。新しい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ブロックは必要なく、エラーは発生しません。

関連する問題