Parsing through MS Word document.

Pavan Dileep
Tera Contributor

Hi,

 

We have a requirement to parse the word document and copy the document content and the number of pages that the word document contains onto the custom fields. 

 

Can anyone please share some related resource which could help us achieve this.

 

Thanks

Pavan Dileep

3 REPLIES 3

Manmohan K
Tera Sage

Hi @Pavan Dileep ,

 

You can make use of Apache POI API to parse word docs. Below is sample code

package com.mkyong.poi.word;

import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class ReadParseDocument1 {

    public static void main(String[] args) throws IOException {

        String fileName = "c:\\test\\simple.docx";

        try (XWPFDocument doc = new XWPFDocument(
                Files.newInputStream(Paths.get(fileName)))) {

            XWPFWordExtractor xwpfWordExtractor = new XWPFWordExtractor(doc);
            String docText = xwpfWordExtractor.getText();
            System.out.println(docText);

            // find number of words in the document
            long count = Arrays.stream(docText.split("\\s+")).count();
            System.out.println("Total words: " + count);

        }

    }

}

.  

You can refer below article for more details

https://mkyong.com/java/java-read-and-write-microsoft-word-with-apache-poi/

 

API link - https://poi.apache.org/components/document/index.html

Thanks for the Reply @Manmohan K 

 

As this a java code, is it compatible in ServiceNow?

 

If it is compatible, can you please share how can we use the Java code in ServiceNow?

@Pavan Dileep ,

 

You can upload custom JAR files to ServiceNow MID Server and then you can trigger the java function call using probe. As a MID Server is a java service which is connected to your ServiceNow instance, it can accept those java call requests execute them, and return the results in ECC Queue.

 

 Refer to this video for step by step example - https://www.youtube.com/watch?v=tOHuFVE3XNQ