Convert Folder of Formdata.txt (created with PDFtk) to .xfdf

Creating Extension - Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by [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.

Convert Folder of Formdata.txt (created with PDFtk) to .xfdf

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

This will convert all Formfield data files (.txt) (created with PDFtk) in a chosen folder to .xfdf files into a subfolder.

To extract form data of Folder of PDFs (using PDFtk) in to .txt-files see ... 86#p434033
To convert all .xfdf files to 1 csv see: ... 21&t=91524
Create CSV from Formdata of PDFs in a folder (using PDFtk)

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

    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)="txt" then
    Fileurl=sFolderpath & "/" & sFileName

   oTextinputStream = CreateUnoService("")
   inputfile = converttourl(Fileurl)
   oinputStream = oSFA.openFileRead(inputfile)
   mkdir(sFolderpath & "/" & "XFDF")
   outputfile = converttourl(sFolderpath & "/" & "XFDF" & "/" & getfilenamewithoutextension(sFileName) & ".xfdf")
    if fileexists(outputfile) then kill(outputfile)   
    oTextoutputStream = CreateUnoService("")
    ooutputStream = oSFA.openFileWrite(outputfile)
   oTextoutputStream.writestring("<?xml version=""1.0"" encoding=""UTF-8""?>" & chr(10) & "<xfdf xmlns="""" xml:space=""preserve"">" & chr(10))
   rem oTextoutputStream.writestring("<f href=""" & convertfromurl(inputfile) & """/>" & chr(10))   
   oTextoutputStream.writestring("<fields>" & chr(10))

   do while oTextinputStream.isEOF()=0
   stringA = oTextinputStream.readLine()
   if instr(stringA,"Field")=0 and instr(stringA,": ")=0 and foundFieldValue=1 then
   FieldValue = FieldValue & chr(10) & stringA
   elseif foundFieldValue=1 then
   oTextoutputStream.writestring("<value>" & FieldValue & "</value>" & chr(10))
   end if
   if instr(stringA,"FieldName: ")=1 then
   FieldName=mid(stringA,len("FieldName: ")+1)
   oTextoutputStream.writestring("<field name=""" & FieldName & """>" & chr(10))
   end if
   if instr(stringA,"FieldValue: ")=1 then
   FieldValue=mid(stringA,len("FieldValue: ")+1)
   end if
   if instr(stringA,"---")=1 and foundFieldName then
   oTextoutputStream.writestring("</field>" & chr(10))
   end if
   if foundFieldName then oTextoutputStream.writestring("</field>" & chr(10))
   oTextoutputStream.writestring("</fields>" & chr(10) & "</xfdf>" )
    end if
    sFileName = Dir()

    msgbox "Files saved to: " & convertfromurl(sFolderpath & "/" & "XFDF")

    End Sub
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
Posts: 197
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