NoobInLifeGeneral
NoobInLifeGeneral

Reputation: 21

Can I skip PDF form fields from being filled in an FDF?

I use pdftk to fill_form in many pdf's in a loop. The problem im having is that i use pdftk to fill 3 of the 5 fields. The other fields have standar values.

Is there a way to skip certain fields when filling?

This is my code now:

for each objfile in colFiles
    If right(objfile.Name,3) = "pdf" Then
    FileFill = strFolderPath + "\" + objFile.Name
    filled = strFolderPath + "\" + Replace(objFile.Name,".pdf", "_.pdf")
    pdfnumber = Left(objfile.name,InStr(objFile.Name," ")-1)
    set objFDF = objFSO.CreateTextFile(fdfform,true)
        objFDF.write "%FDF-1.2" & vbCrLf
        objFDF.write "%âãÏÓ" & vbCrLf
        objFDF.write "1 0 obj " & vbCrLf
        objFDF.write "<<" & vbCrLf
        objFDF.write "/FDF " & vbCrLf
        objFDF.write "<<" & vbCrLf
        objFDF.write "/Fields [" & vbCrLf
        objFDF.write "<<" & vbCrLf
        objFDF.write "/V (" & pdfnumber & ")" & vbCrLf
        objFDF.write "/T (Tekening nr)" & vbCrLf
        objFDF.write ">> " & vbCrLf
        objFDF.write "<<" & vbCrLf
        ' objFDF.write "/V ()" & vbCrLf
        objFDF.write "/T (Ruimte)" & vbCrLf
        objFDF.write ">> " & vbCrLf
        objFDF.write "<<" & vbCrLf
        objFDF.write "/V ()" & vbCrLf
        objFDF.write "/T (Aantal)" & vbCrLf
        objFDF.write ">> " & vbCrLf
        objFDF.write "<<" & vbCrLf
        objFDF.write "/V ()" & vbCrLf
        objFDF.write "/T (Kleur)" & vbCrLf
        objFDF.write ">> " & vbCrLf
        objFDF.write "<<" & vbCrLf
        objFDF.write "/V (" & Project & ")" & vbCrLf
        objFDF.write "/T (Project)" & vbCrLf
        objFDF.write ">> " & vbCrLf
        objFDF.write "<<" & vbCrLf
        objFDF.write "/V ()" & vbCrLf
        objFDF.write "/T (Opmerkingen)" & vbCrLf
        objFDF.write ">> " & vbCrLf
        objFDF.write "<<" & vbCrLf
        objFDF.write "/V ()" & vbCrLf 
        objFDF.write "/T (Materiaal)" & vbCrLf
        objFDF.write ">>]" & vbCrLf
        objFDF.write ">>" & vbCrLf
        objFDF.write ">>" & vbCrLf
        objFDF.write "endobj " & vbCrLf
        objFDF.write "trailer" & vbCrLf
        objFDF.write "" & vbCrLf
        objFDF.write "<<" & vbCrLf
        objFDF.write "/Root 1 0 R" & vbCrLf
        objFDF.write ">>" & vbCrLf
        objFDF.write "%%EOF" & vbCrLf
        objFDF.Close
        shl.run "cmd.exe /C pdftk """ & FileFill & """ fill_form """ & fdfform & """ output """ & filled & """"
        'wscript.echo "cmd.exe /C pdftk """ & FileFill & """ fill_form """ & fdfform & """ output """ & FileFill & """"
    Wscript.sleep 1000
    'objFSO.MoveFile strFolderPath + "\" + objFile.Name , strFolderPath + "\" + Replace(objFile.Name," ", "_")
    fSO.DeleteFile(FileFill)
    Wscript.sleep 1000
    fSO.DeleteFile(fdfform)
    Wscript.sleep 1000
    Else
    'nothing
    end If
Next

so the pdfnumber and Project needs to be filled, which is working perfectly but Kleur and Materiaal have a value already and now its sets the value as blank.

I have tried to just delete these lines from the vbs but that doesnt work as it deletes all my pdf's.

ps. this is just the section of code that handles the form. The dims are all set somewhere in the beginning of my code.

Upvotes: 1

Views: 49

Answers (1)

NoobInLifeGeneral
NoobInLifeGeneral

Reputation: 21

For other people, this is how i solved it for myself. Doesnt mean its the best solution and this way can probably be done way better/ more efficient. But this is how i got it working:

for each objfile in colFiles
If right(objfile.Name,3) = "pdf" Then
'set al dims
fdfform = scriptDir & "\fdfform.fdf"
fdfformtxt = scriptDir & "\fdfform.txt"
FileFill = strFolderPath + "\" + objFile.Name
filled = strFolderPath + "\" + Replace(objFile.Name,".pdf", "_.pdf")
pdfnumber = Left(objfile.name,InStr(objFile.Name," ")-1)

'the strings that need to be replaced
strFind = "/V ()" & vblf & "/T (Tekening nr)"
strReplace = "/V (" & pdfnumber & ")" & vblf & "/T (Tekening nr)"
strFind2 = "/V ()" & vblf & "/T (Project)"
strReplace2 = "/V (" & Project & ")" & vblf & "/T (Project)"

'generate fdf
shl.run "cmd.exe /C pdftk """ & FileFill & """ generate_fdf output """ & fdfform & """"

'make fdf into txt
wscript.sleep 500
objFSO.copyFile fdfform , fdfformtxt

'for each replacement it opens the txt and replaces the string
Set ts=fso.OpenTextFile(fdfformtxt,1) 'ForReading
buf=ts.ReadAll
ts.Close
Set ts=fso.OpenTextFile(fdfformtxt,2, true) 'ForWriting
ts.Write replace(buf, strFind,strReplace)
ts.Close
Set ts=fso.OpenTextFile(fdfformtxt,1) 'ForReading
buf=ts.ReadAll
ts.Close
Set ts=fso.OpenTextFile(fdfformtxt,2, true) 'ForWriting
ts.Write replace(buf, strFind2,strReplace2)
ts.Close

'make the file fdf again and fill_form
objFSO.copyFile fdfformtxt , fdfform
shl.run "cmd.exe /C pdftk """ & FileFill & """ fill_form """ & fdfform & """ output """ & filled & """"
Wscript.sleep 1000
fSO.DeleteFile(FileFill)
Wscript.sleep 500
fSO.DeleteFile(fdfform)
Wscript.sleep 500
fSO.DeleteFile(fdfformtxt)
Wscript.sleep 500
Else
'nothing
end If
Next

So now it makes the fdf into a txt and in that txt you can replace strings.

If people see ways to make this code less messy or more efficient, feedback is always welcome!

Upvotes: 1

Related Questions