| Home | About | |
| KB | Contact |
|
Draft - Updating PDF forms via scripts Published: 10 Dec 2015 - Last update: 12 Dec 2015 (c) Nicolai Kjaer / Spinner Software B.V. This document explains how to read from, and write to, Adobe Acrobat / Adobe LiveCycle PDF forms.
1. SetupTODO: This may need Acrobat installed ; I will update the document later for Adobe Reader.
2. ListAllFields scriptThis script will be very helpful later, when you're trying to determine what field name to update. It lists all fields found in the PDF. I usually pipe the output to a text file I can have open at the same time as my script, for easy field reference. Change line 3 (Const PDFFile = "D:\Test.pdf") to point to your pdf. ListAllFields.vbs:
Option Explicit
Const PDFFile = "D:\Test.pdf"
'----------
' You should not need to modify any code below this line.
'----------
Dim AcroApp, theForm, jso
Set AcroApp = CreateObject("AcroExch.App")
If AcroApp Is Nothing Then
WScript.Echo "ERROR: Unable to get AcroExch.App object. Install Acrobat?"
WScript.Quit(-1)
End If
Set theForm = CreateObject("AcroExch.PDDoc")
theForm.Open PDFFile
Set jso = theForm.GetJSObject
If jso Is Nothing Then
WScript.Echo "ERROR: Unable to get js object!"
Else
Dim tstXfa
Set tstXfa = Nothing
On Error Resume Next
Set tstXfa = jso.xfa
On Error Goto 0
If Not tstXfa Is Nothing Then
WScript.Echo PDFFile & " is an XFA document."
Else
WScript.Echo PDFFile & " is a standard Acrobat document."
End If
Dim numFields, fldName, fld, n
numFields = jso.numFields
For n = 0 To numFields - 1
fldName = jso.GetNthFieldName(n)
Set fld = jso.getfield(fldName)
If fld.Type = "text" Then
If fld.readonly = False Then
WScript.Echo "text (r/o) field " & fld.Name & "=" & fld.Value
Else
WScript.Echo "text field " & fld.Name & "=" & fld.Value
End If
ElseIf fld.Type = "combobox" Then
Dim x
WScript.Echo "combobox field " & fld.Name & " value = '" & fld.Value & "'"
WScript.Echo " Choices: "
For x = 0 to (fld.numItems - 1)
WScript.Echo " [" & fld.getItemAt(x,false) & "] = '" & fld.getItemAt(x) & "'"
Next
Else
WScript.Echo "*** Unsupport field of type " & fld.Type
End If
Next
End If
theForm.Close
AcroApp.Exit
Set AcroApp = Nothing
Set theForm = Nothing
3. UpdatePDF scriptThis script will update a single form field in the PDF, outputting debug information along the way. UpdatePDF.vbs:
Option Explicit
Const PDFFile = "D:\Test.pdf"
Const PDSaveFull = 1
Const PDSaveBinaryOK = 16
Const PDSaveCollectGarbage = 32
Const PDSaveCopy = 2
Const PDSaveIncremental = 0
Const PDSaveLinearized = 4
Const PDSaveWithPSHeader = 8
Dim AcroApp, theForm, jso
Set AcroApp = CreateObject("AcroExch.App")
If AcroApp Is Nothing Then
WScript.Echo "ERROR: Unable to get AcroExch.App object. Install Acrobat?"
WScript.Quit(-1)
End If
Set theForm = CreateObject("AcroExch.PDDoc")
WScript.Echo "Opening " & PDFFile
theForm.Open PDFFile
Set jso = theForm.GetJSObject
If jso Is Nothing Then
WScript.Echo "ERROR: Unable to get js object!"
Else
Dim tstXfa
Set tstXfa = Nothing
On Error Resume Next
Set tstXfa = jso.xfa
On Error Goto 0
If Not tstXfa Is Nothing Then
WScript.Echo PDFFile & " is an XFA document."
Else
WScript.Echo PDFFile & " is a standard Acrobat document. Need a different script!"
WScript.Quit(-1)
End If
Dim f1
Set f1 = jso.xfa.resolveNode("form.form1.MyField.MyField2.Row1.MyField3")
WScript.Echo "Current values read from PDF: "
WScript.Echo "Field = " & f1.formattedValue
WScript.Echo "Updating.."
f1.formattedValue = "Hello!"
WScript.Echo "Updated field values in PDF: "
WScript.Echo "Field = " & f1.formattedValue
WScript.Echo "Saving.."
If (theForm.Save(PDSaveIncremental, PDFFile) = False) Then
WScript.Echo "ERROR: Unable to save document!"
Else
WScript.Echo "Document saved."
End If
Set f1 = Nothing
WScript.Echo "Done!"
End If
theForm.Close
AcroApp.Exit
Set AcroApp = Nothing
Set theForm = Nothing
I hope you enjoyed this guide ! (c) 2015 Nicolai Kjaer, Spinner Software B.V. |