Suppose there is table of thousand data items and we need to create an index of any of the items. So,how can an index be created in Docx file programmatically i.e with the help of docx4j.
Asked
Active
Viewed 170 times
-2
-
You might be better off using Apache POI to read the docx file. – greg-449 Jun 27 '18 at 07:40
-
Or pxDoc... (www.pxdoc.fr) – Romain Bernard Jun 27 '18 at 21:03
-
Or Visual Basic – i.e., no need for an external programming language. – Jongware Jun 27 '18 at 23:32
1 Answers
0
MainDocumentPart mdp = word.getMainDocumentPart();
String textXpath = "//w:t";
List<Object> textNodes= mdp.getJAXBNodesViaXPath(textXpath, true);
c=0;
for (Object obj : textNodes)
{
Text text = (Text) ((JAXBElement<?>) obj).getValue();
textValue = text.getValue();
String[] words = textValue.split("\\W+");
for (String word : words)
{
word = word.toLowerCase();
List<Integer> list = occurences.get(word);
if (list == null)
{
list = new ArrayList<Integer>();
occurences.put(word, list);
}
list.add(c);
}
c++;
}
//System.out.print(occurences.toString());
word.getMainDocumentPart().addParagraphOfText("");
word.getMainDocumentPart().addParagraphOfText("");
word.getMainDocumentPart().addStyledParagraphOfText("Title", "INDEX");
String count = occurences.toString();
word.getMainDocumentPart().addParagraphOfText("count");

shivani
- 1
- 3
-
The above code will count the occurrences of each item in the table and will return the position of each item. – shivani Jul 02 '18 at 10:18