PDFWriter Reflection problem

2008-10-03
2013-05-01
  • I am currently trying to use iTextdotNET via reflection in a VB.NET application. Everything works fine until I try to create an instance of the PDFWriter. When trying to instantiate the writer using the (com.lowagie.text.Document, java.io.FileOutputStream) constructor, I get an error that indicates an ambiguous match on the constructor/the object needs to be narrowed. I am passing in the exact types for the constructor I need, yet it fails to find the constructor. I have included an excerpt from the code below. I also tried to build a PDFWriter constructor to call Invoke, but that causes the same "ambiguous match" problem. Any suggestions on how to get the troublesome call to the PDFWriter constructor to succeed would be appreciated.

    Dim oPDFWriter, oDocument
    Dim TWriter, TDoc As Type
    TWriter = iTextdll.GetType("com.lowagie.text.pdf.PdfWriter")
    TDoc = iTextdll.GetType("com.lowagie.text.Document")
    oPDFWriter = Activator.CreateInstance(TWriter)
    ...
    oDocument = docConstructor.Invoke(docParams)

    Dim TFos As Type
    Dim paramTypes(0) As Type
    paramTypes(0) = Type.GetType("System.String")
    Dim fosConstructor As ConstructorInfo
    Dim fosArgs(0) As Object
    fosArgs(0) = pdfFilename
    TFos = vjsdll.GetType("java.io.FileOutputStream")
    fosConstructor = TFos.GetConstructor(paramTypes)

    oPDFWriter.getInstance(oDocument, fosConstructor.Invoke(fosArgs)) 'this line causes the exception

     
    • In the previous code, I had the following incorrect line:
      oPDFWriter.getInstance(oDocument, fosConstructor.Invoke(fosArgs))

      The actual reflection based code is:

      Dim writerArgs() As Object = {oDocument, outFile}
      Dim writerArgTypes() As Type = {oDocument.GetType(), outFile.GetType()}

      Dim mi As MethodInfo
      mi = TWriter.GetMethod("getInstance", writerArgTypes) 'this line fails to find the method I want to call due to ambiguous match
      mi.Invoke(oPDFWriter, writerArgs) ' this line fails since mi is null

      Sorry if my previous post caused confusion. Anyway, the question is: why does the TWriter.GetMethod() call not find the method since it is present and has parameters that match those I am providing, and how do I resolve this?