我可以跳过填写表单字段吗?

问题描述 投票:0回答:1

我使用 pdftk 循环填充许多 pdf 中的表单。我遇到的问题是我使用 pdftk 来填充 5 个字段中的 3 个。其他字段有标准值。

有没有办法在填写时跳过某些字段?

这是我现在的代码:

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

因此需要填充

pdfnumber 
Project
,这工作正常,但是
Kleur
Materiaal
已经有一个值,现在将值设置为空白。

我尝试从 vbs 中删除这些行,但这不起作用,因为它删除了我所有的 pdf 文件。

ps。这只是处理表单的代码部分。暗淡都设置在我的代码开头的某个地方。

vbscript pdftk pdf-form
1个回答
0
投票

对于其他人来说,这就是我为自己解决的方法。并不意味着它是最好的解决方案,这种方式可能可以做得更好/更有效。但这就是我让它工作的方式:

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

现在它将 fdf 转换为 txt,并且在该 txt 中您可以替换字符串。

如果人们找到了让代码不那么混乱或更高效的方法,欢迎提供反馈!

© www.soinside.com 2019 - 2024. All rights reserved.