Jump to content

how to fill a Pdf form


Recommended Posts

hi,

i have a pdf with a form in it and i want to fill the cells of the pdf with the autoit variables and then save it to archive

for what i read on other topics i made this but it didn't work but din't give any error :

$fileForm = "c:\JM714A02.pdf"
$AVDoc = ObjCreate("AcroExch.AVDoc")
$AVDoc.Open($FileForm,"")
$text="Hello"
$avdoc.getField("Texto1.0").value=$text

i donwloaded the javascript references manual for pdf but is hasrt to find somthing on 769 pages without knowing what search exacly XD

Link to comment
Share on other sites

I am starting to work on the same problem, I have some editable PDFs ( transport documents) and I have already a DB sqllite with the data.

I think I 'll make all the worK with simple send keys .

I have already made a program for creating outlook accounts (also with advanced options) reading from a db of users, so fill a simple editable pdf will be easy I think....

someone has a better method.. ?

Link to comment
Share on other sites

OK,

here one short example how you can fill a form with Adobe Acrobat $$$ version from 5-X using JS:

;; user settings
$FilePath = "C:\temp.pdf"  ; fillable pdf with one form field
$fieldname = "Test1" ;form field name
$fileOut = "c:\temp_filled.pdf"


;; application
$App = ObjCreate("AcroExch.App")     ;; start Adobe Acrobat
$App.Show                           ;; show Acrobat or comment out for hidden mode
;; actions
$AVDoc = ObjCreate("AcroExch.AVDoc")     ;; connect to Ac Viewer
If $AVDoc.Open($FilePath,"") Then        ;; open the file
    $PDDoc = $AVDoc.GetPDDoc             ;; get PdDoc from AvDoc
    $AForm = ObjCreate("AFormAut.App")   ;;connect to AForm API
    $EX = "" _                              ;; write some JS-code to a variable
        & ' var f = this.getField("'&$fieldname&'")' & @LF _
        & ' f.value = "hello"'
    ;msgbox(0,"",$ex)
    $AForm.Fields.ExecuteThisJavaScript($EX)    ;;execute the Js-Code
    $PDDoc.save(1,$fileOut)
    $app.closeAlldocs()
    $app.exit()
Else
        msgbox(0,"","Coudn't open report")

endif
;; release objects
$AForm = 0
$AvDoc = 0
$PdDoc = 0
$App = 0

If you want to fill a pdf only with free Reader you can write and execute an FDF-File:

#include <GUIConstants.au3>

; Setting variables
Global $sFilename = "C:\Temp.pdf"


If FileExists($sFilename) Then
    $sX = StringReplace($sFilename,"\","/")
    $sX = StringReplace($sX,":","")
    $sPdfNM = "/" &$sX
    $FdfNM = FileOpen( "C:\Temp.FDF",2)

    $Ex = "%FDF-1.2 %% Fill Form Fields %%" &@CRLF _
        &"1 0 obj << /FDF << /F (" &$sPdfNM &") /Fields " &@CRLF _
        &"[" &@CRLF _
        & '<</T(Test1)/V(MyTestValue)>>' &@CRLF _
        &"]>>>>" &@CRLF _
        & "endobj" &@CRLF _
        & "trailer" &@CRLF _
        & "<</Root 1 0 R >>" &@CRLF _
        & "%%EOF"

    FileWrite($FdfNM,$Ex)
    FileClose($FdfNM)
    RunWait(@ComSpec & " /c " & 'start c:\Temp.fdf' , "", @SW_HIDE)
    ;FileDelete("C:\temp.fdf")
endif

But with Reader you can only save it if the rights are enabled. That normaly not or not ever.

The free commandline tool pdftk.exe (well known and tested) can read pdf field-values, prefill or fill and save it. In the next example I added that:

#include <GUIConstants.au3>

; Setting variables
Global $sFilename = "C:\Temp.pdf"

If FileExists($sFilename) Then
    $sX = StringReplace($sFilename,"\","/")
    $sX = StringReplace($sX,":","")
    $sPdfNM = "/" &$sX
    $FdfNM = FileOpen( "C:\Temp.FDF",2)

    $Ex = "%FDF-1.2 %% Fill Form Fields %%" &@CRLF _
        &"1 0 obj << /FDF << /F (" &$sPdfNM &") /Fields " &@CRLF _
        &"[" &@CRLF _
        & '<</T(Test1)/V(MyTestValue)>>' &@CRLF _
        &"]>>>>" &@CRLF _
        & "endobj" &@CRLF _
        & "trailer" &@CRLF _
        & "<</Root 1 0 R >>" &@CRLF _
        & "%%EOF"

    FileWrite($FdfNM,$Ex)
    FileClose($FdfNM)
    RunWait(@ComSpec & " /c " & 'start c:\Temp.fdf' , "", @SW_HIDE)
    ;FileDelete("C:\temp.fdf")
endif


if FileExists(@scriptdir&"/pdftk.exe") Then
    $OK=msgbox(51,"Pdf-Fill","Do you want "&@lf&" only to pre-fill (keep editable) or "&@lf&" pre-fill and flatten (fix content)?"&@lf&@lf&"Keep editable?")
    consolewrite($OK&@lf)
    select
        case $OK = 6 ;yes
            if FileExists("C:\temp_fe.pdf") then FileDelete("C:\temp_fe.pdf")
            Runwait(@ComSpec & " /c " &@scriptdir&"\pdftk.exe "&$sFilename&" fill_form c:\Temp.fdf output C:\temp_fe.pdf", "", @SW_HIDE)
            shellexecute("C:\temp_fe.pdf")
        case $OK = 7 ; no
            if FileExists("C:\temp_ff.pdf") then FileDelete("C:\temp_ff.pdf")
            RunWait(@ComSpec & " /c " &@scriptdir&"\pdftk.exe "&$sFilename&" fill_form c:\Temp.fdf output C:\temp_ff.pdf flatten", "", @SW_HIDE)
            shellexecute("C:\temp_ff.pdf")
        case $OK = 2 ; cancel
            msgbox(0,"Pdf-Fill","Job canceled")
    EndSelect
endif

If you don't have a fillable PDF-Form to test it, you just can set it up with Adobe Acrobat. If you don't want to spent money for that you can also use free Open-Office, which can also write fillable PDF-forms.

HTH, Reinhard

Edited by ReFran
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...