Sub Class_Globals
Private Root As B4XView
Private xui As XUI
End Sub
Public Sub Initialize
' B4XPages.GetManager.LogEvents = True
End Sub
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
End Sub
'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.
Private Sub Button1_Click
TryItToLoadText("c:\Nikos","MyDoc.docx")
End Sub
Sub TryItToLoadText(Dir As String, Filename As String)
MySub
' Return
'
' Dim sb As StringBuilder
' sb.Initialize
'
' 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
'' For Each key As String In m.Keys
'' If text.Contains("$" & key & "$") Then
'' r.RunMethod("setText", Array(" " & m.Get(key) & " ", 0))
'' End If
'' Next
' sb.Append(text)
' End If
' Next
' sb.Append(CRLF)
' End If
' Next
' 'SaveDocument(doc, File.DirApp , "Document" & c & ".docx")
' Log(sb.ToString)
End Sub
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 inps As InputStream = File.OpenInput(Dir, FileName)
Dim document As JavaObject
document.InitializeNewInstance("org.apache.poi.xwpf.usermodel.XWPFDocument", _
Array(inps))
Return document
End Sub
Sub CreateNewDocx() As JavaObject 'ignore not used in this example
Dim document As JavaObject
document.InitializeNewInstance("org.apache.poi.xwpf.usermodel.XWPFDocument", _
Null)
Return document
End Sub
Sub MySub
Dim jo As JavaObject = asJO(Me)
jo.RunMethod("readDocxFile",Array("c:/Nikos/MyDoc.docx"))
End Sub
Sub asJO(o As JavaObject)As JavaObject
Return o
End Sub
#if java
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
public static void readDocxFile(String fName) {
try {
File file = new File(fName);
System.out.println("1");
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
System.out.println("2");
XWPFDocument document = new XWPFDocument(fis);
System.out.println("3");
List<XWPFParagraph> paragraphs = document.getParagraphs();
for (XWPFParagraph para : paragraphs) {
System.out.println(para.getText());
}
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
#End If