Hi all,
I'm using some modified old code by @Erel to "update" a docx document.
The base document sports 14 "user fields" (all of them of type Text). My code offers a simple interface to the user to fill up all or part of those variable fields, then writes a new document based on the original one (which is a fixed model).
Problem is that model_1 (one column page) works OK while model_2 (two columns page) skips a few fields outputting an ugly placeholder ("User field <name of the field> =").
Model_1 and Model_2 use exactly the same map for the user fields.
Here's the code I use:
I saw that it exists a commit() function in the definition of XWPFDocument, but the document returned by OpenDocX seems to be of type POIXMLDocument so that function is unavaible (and anyway I don't know if it could solve the problem).
Any ideas or suggestions?
I'm using some modified old code by @Erel to "update" a docx document.
The base document sports 14 "user fields" (all of them of type Text). My code offers a simple interface to the user to fill up all or part of those variable fields, then writes a new document based on the original one (which is a fixed model).
Problem is that model_1 (one column page) works OK while model_2 (two columns page) skips a few fields outputting an ugly placeholder ("User field <name of the field> =").
Model_1 and Model_2 use exactly the same map for the user fields.
Here's the code I use:
B4X:
Dim doc As JavaObject = OpenDocx(Dir, FileName)
Dim paragraphs As List = doc.RunMethod("getParagraphs", Null)
For Each p As JavaObject In paragraphs
Dim runs As List = p.RunMethod("getRuns", Null)
If runs.IsInitialized Then
For Each r As JavaObject In runs
Dim text As String = r.RunMethod("getText", Array(0))
If text <> Null Then
Log(text)
For Each key As String In m1.Keys
'If text.Contains("$" & key & "$") Then <<- this was from original code whcih made use of Excel as first step
If text.Contains(key) Then
Log(key)
r.RunMethod("setText", Array(m1.Get(key), 0))
End If
Next
End If
Next
End If
Next
Sub SaveDocument(doc As JavaObject, Dir As String, FileName As String)
Dim out As OutputStream = File.OpenOutput(Dir, FileName, False)
doc.RunMethod("write", Array(out))
out.Close
End Sub
Sub OpenDocx(Dir As String, FileName As String) As JavaObject
Dim in As InputStream = File.OpenInput(Dir, FileName)
Dim document As JavaObject
document.InitializeNewInstance("org.apache.poi.xwpf.usermodel.XWPFDocument", Array(in))
Return document
End Su
I saw that it exists a commit() function in the definition of XWPFDocument, but the document returned by OpenDocX seems to be of type POIXMLDocument so that function is unavaible (and anyway I don't know if it could solve the problem).
Any ideas or suggestions?