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 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.

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
https://forum.openoffice.org/en/forum/v ... 86#p434033
To convert all .xfdf files to 1 csv see:
https://forum.openoffice.org/en/forum/v ... 21&t=91524
Create CSV from Formdata of PDFs in a folder (using PDFtk)
https://forum.openoffice.org/en/forum/viewtopic.php?f=21&t=91588

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("com.sun.star.ucb.SimpleFileAccess")
    sFolderpath=""
    rem---folderpicker
    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
    else
    exit sub
    end if

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



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

   oTextinputStream = CreateUnoService("com.sun.star.io.TextInputStream")
   inputfile = converttourl(Fileurl)
   oinputStream = oSFA.openFileRead(inputfile)
   oTextinputStream.setInputStream(oinputStream)
   
   mkdir(sFolderpath & "/" & "XFDF")
   outputfile = converttourl(sFolderpath & "/" & "XFDF" & "/" & getfilenamewithoutextension(sFileName) & ".xfdf")
    if fileexists(outputfile) then kill(outputfile)   
   
    oTextoutputStream = CreateUnoService("com.sun.star.io.TextOutputStream")
    ooutputStream = oSFA.openFileWrite(outputfile)
    oTextoutputStream.setOutputStream(ooutputStream)
   
   oTextoutputStream.writestring("<?xml version=""1.0"" encoding=""UTF-8""?>" & chr(10) & "<xfdf xmlns=""http://ns.adobe.com/xfdf/"" xml:space=""preserve"">" & chr(10))
   rem oTextoutputStream.writestring("<f href=""" & convertfromurl(inputfile) & """/>" & chr(10))   
   oTextoutputStream.writestring("<fields>" & chr(10))
   
   
   foundFieldName=0
   foundFieldValue=0

   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))
   foundFieldValue=0
   end if
   if instr(stringA,"FieldName: ")=1 then
   FieldName=mid(stringA,len("FieldName: ")+1)
   foundFieldName=1
   oTextoutputStream.writestring("<field name=""" & FieldName & """>" & chr(10))
   end if
   if instr(stringA,"FieldValue: ")=1 then
   FieldValue=mid(stringA,len("FieldValue: ")+1)
   foundFieldValue=1
   end if
   
   if instr(stringA,"---")=1 and foundFieldName then
   oTextoutputStream.writestring("</field>" & chr(10))
   foundFieldName=0
   end if
   
   loop
   
   if foundFieldName then oTextoutputStream.writestring("</field>" & chr(10))
   oTextoutputStream.writestring("</fields>" & chr(10) & "</xfdf>" )
       
   oTextinputStream.closeInput
   oTextoutputStream.closeOutput()
    end if
    sFileName = Dir()
    loop

   
   
    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)
http://struckkai.blogspot.de/2015/04/libreofficesongbookarchitect.html
musikai
Volunteer
 
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