Extract form data of Folder of PDFs (using PDFtk)

Creating Extension - Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This forum is not for asking questions about writing your own macros.

Extract form data of Folder of PDFs (using PDFtk)

Postby musikai » Thu Dec 14, 2017 12:20 am

PDFtk must be installed.
This will run the command "dump_data_fields_utf8" for all PDFs in a chosen folder and extract the formdatas to .txt-files into a subfolder.

To convert all .txt files to .xdfd files see:
To convert all .xfdf files to 1 csv see:
Create CSV from Formdata of PDFs in a folder (using PDFtk)

Code: Select all   Expand viewCollapse view
    Sub PDFTK_dumpdatafields_of_Folder_with_PDFs_to_txt_files
    If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then GlobalScope.BasicLibraries.LoadLibrary("Tools")
    oSFA = createUnoService("com.sun.star.ucb.SimpleFileAccess")
    rem to not use the folderpicker set the folder here and uncomment:
    rem sFolderpath=converttourl("C:\Users\kai\Desktop")
    if sFolderpath="" then
    oDialog = CreateUnoService("com.sun.star.ui.dialogs.FolderPicker")

    If oDialog.Execute() = 1 Then
    sFolderpath = oDialog.getDirectory
    exit sub
    end if

    end if
    if not oSFA.Exists(sFolderpath) then
    msgbox "Folder not found!"
    exit sub
    end if

    sFileName = Dir(sFolderpath & "/", 0)
    Do While (sFileName <> "")
    if GetFileNameExtension(sFileName)="pdf" then
    Fileurl=sFolderpath & "/" & sFileName

   mkdir(sFolderpath & "/" & "PDFtk-DataFields")
   rem indirect way because of PDFtk utf8 filename bug
   outputfiledummy = sFolderpath & "/" & "PDFtk-DataFields" & "/" & "datafields.txt"
   outputfile = sFolderpath & "/" & "PDFtk-DataFields" & "/" & getfilenamewithoutextension(sFileName) & ".txt"
    if fileexists(outputfiledummy) then kill(outputfiledummy)
    if fileexists(outputfile) then kill(outputfile)
    Shell(pdftkapp,0, """" & inputfile & """" & " dump_data_fields_utf8 output " & """" & outputfiledummy & """", true)

    rem indirect way because of PDFtk utf8 filename bug
      if fileexists(outputfiledummy) then
      Name outputfiledummy as outputfile
      end if
    end if
    sFileName = Dir()
    msgbox "Files saved to: " & convertfromurl(sFolderpath & "/" & "PDFtk-DataFields")

    End Sub
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
Posts: 282
Joined: Wed Nov 11, 2015 12:19 am

Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 1 guest