2016-07-12 5 views
1

PDFsharp 1.50 beta 3b를 사용하고 있습니다. 주로 새로운 PDF 문서를 사용할 수있는 기능에 액세스하는 데 사용하고 있습니다. 나는 새로운 기능을 사용하지 않고있다. 아래로 내 PDF 문서를 변환하는 그들을 죽이고 왜 그런지 모르겠다. 그것이 말했다.PDFsharp 베타 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 블록이 필요하지 않으며 오류가 발생하지 않습니다.