Tuesday, August 12, 2014

Using VB.Net to check a checkbox on a pdf

It took me a little while to figure out how to check and uncheck a checkbox on a pdf form using VB.Net. The value of the checkbox has to be set to either "0" for unchecked or "1" for checked.  Here is the code I used to access the form and get to the checkbox.

' Reference to Acrobat application

Dim gApp As Acrobat.CAcroApp

'IAC objects
Dim gPdDoc As Acrobat.CAcroPDDoc

Dim formApp As AFORMAUTLib.AFormApp
Dim acroForm As AFORMAUTLib.Fields = Nothing
Dim field As AFORMAUTLib.Field

'Initialize Acrobat by creating App object
gApp = CreateObject("AcroExch.App")

' Initialize the PDF object
gPdDoc = CreateObject("AcroExch.PDDoc")

' Open an example PDF file in acrobat
If gPdDoc.Open("C:\example.pdf") Then 
   
   ' I'm still not sure what all of this does
   s = gPdDoc.GetFileName
   gPdDoc.OpenAVDoc(s)

   formApp = CreateObject("AFormAut.App")
  
   ' Get a reference to the fields from the form 
   acroForm = formApp.Fields

   ' Get a reference to the particular checkbox field 
   ' This uses the data structure of the form to find the form, 
   ' then page, then fieldname
   field = acroForm.Item("form1[0].#subform[0].Checkbox3[0]")
   
   ' Set the checkmark to "on"
   field.Value = "1"

   ' Set the checkmark to "off"
   field.Value = "0"
 
Else
   MsgBox("Failed to open")
End If

No comments:

Post a Comment