locked
Easy C# to VB help with iTextSharp RRS feed

  • Question

  • User993368333 posted

    I have the following code Snipit that i'm working with using the iTextSharp DLL.  I've got the thing working fine, BUT, I had to change a 'var' type in my converted VB example code to string.  It works for everything that is text, but I can't get Checkbox values to set in the PDF document. 

     

    'This was the C# Code

    public static byte[] GeneratePDF(string pdfPath, Dictionary<string, string> formFieldMap)
        {
            var output = new MemoryStream();
            var reader = new PdfReader(pdfPath);
            var stamper = new PdfStamper(reader, output);
            var formFields = stamper.AcroFields;

            foreach (var fieldName in formFieldMap.Keys)
                formFields.SetField(fieldName, formFieldMap[fieldName]);

            stamper.FormFlattening = true;
            stamper.Close();
            reader.Close();

            return output.ToArray();
        }

     

    I converted it to VB using: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    to get this:

    Public Shared Function GeneratePDF(pdfPath As String, formFieldMap As Dictionary(Of String, String)) As Byte()
    		Dim output = New MemoryStream()
    		Dim reader = New PdfReader(pdfPath)
    		Dim stamper = New PdfStamper(reader, output)
    		Dim formFields = stamper.AcroFields
    
    		For Each fieldName As var In formFieldMap.Keys
    			formFields.SetField(fieldName, formFieldMap(fieldName))
    		Next
    
    		stamper.FormFlattening = True
    		stamper.Close()
    		reader.Close()
    
    		Return output.ToArray()
    	End Function

     

    NOW, the line I changed because I dind't know the VB equivilant was:

    For Each fieldName As var In formFieldMap.Keys
    ----I changed it to:
    For Each fieldName As string In formFieldMap.Keys

    I know that's cheating, but I didn't know what var was...it's not VariantType

    SO, i'm hoping that's the reason I can't set checkboxes with code like this:

    Neither of these work: (IE, the boxes are not checked)

            formFieldMap("intro_ckdeath") = "yes"
            formFieldMap("ck_3") = "1"

    Where as:

            formFieldMap("clientname") = M_FullName

    Works perfect.

    I got the example code from:

    http://www.4guysfromrolla.com/articles/030211-1.aspx

     

    Thanks everyone for the help, I appreciate it.

     

    Joe

     

     

     

     

    Wednesday, May 18, 2011 9:46 PM

Answers

  • User397347636 posted

    The VB equivalent to inferred typing - assuming you're not using an old version of VB - is just to leave the type off:

    For Each fieldName In formFieldMap.Keys

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 19, 2011 10:20 AM
  • User993368333 posted

    Ok, for anyone else who has this issue, here is the solution:

     

    formFieldMap("intro_ckdeath") = "yes"  (DOESN'T WORK)

     

    formFieldMap("intro_ckdeath") = "Yes" (WORKS)

     

    Notice the capital 'Y" 

     

    Thanks David for your CORRECT answer to the first question.  I was just wrong in what was causing it.

     

    Joe

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 23, 2011 2:04 PM

All replies

  • User397347636 posted

    The VB equivalent to inferred typing - assuming you're not using an old version of VB - is just to leave the type off:

    For Each fieldName In formFieldMap.Keys

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 19, 2011 10:20 AM
  • User993368333 posted

    David,

    Removing the of type was valid.  But, unfortunitaly, it didn't solve the problem with iTextSharp no checking the text boxes. So I guess I was wrong to make the assumption that that was the cause.

     

    This is the code i'm trying:

            formFieldMap("intro_ckdeath") = "yes"
            formFieldMap("ck_3") = "1"

    I'm going to try a different PDF template, maybe mine is not made correctly in Acrobat for some reason.  Any other suggestions?

    Monday, May 23, 2011 1:18 PM
  • User993368333 posted

    Ok, for anyone else who has this issue, here is the solution:

     

    formFieldMap("intro_ckdeath") = "yes"  (DOESN'T WORK)

     

    formFieldMap("intro_ckdeath") = "Yes" (WORKS)

     

    Notice the capital 'Y" 

     

    Thanks David for your CORRECT answer to the first question.  I was just wrong in what was causing it.

     

    Joe

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 23, 2011 2:04 PM