2016-07-12 16 views
1

Ich benutze PDFsharp 1.50 beta 3b. Ich benutze es hauptsächlich, um auf die Möglichkeit zuzugreifen, neuere PDF-Dokumente zu verwenden. Ich verwende keine neueren Funktionen. Die Konvertierung meiner PDF-Dokumente bringt sie um und ich weiß nicht warum. Das gesagt;PDFsharp Beta 1.50 PdfTextField, Null Ausnahmefehler, funktioniert aber noch?

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 

Dies erzeugt einen Fehler;

Nun, was macht das komisch und warum ich frage ist, dass dies immer noch mit dem Try/Catch-Block funktioniert. Es füllt das Feld und die Datei hat den richtigen Text in der PDF-Datei. Ich möchte nur wissen, warum diese Ausnahme ausgelöst wird?

+0

Welche Linie ist die Linie 2886 hier? – NePh

+0

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

Antwort

2

Ich habe das Problem herausgefunden. Der neue PDFSharp verwendet eine andere Zugriffsmethode für seine Steuerelemente.

Zuerst unsere Erklärungen;

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 

Für Kontrollkästchen;

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

Und für Strings;

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

So funktioniert es, braucht keinen try/catch-Block und wirft keinen Fehler.