-1

I am doing a task in which I need to read data between two bookmarks of docx file using Java. I got all the name of bookmarks by using docx4j api using below code-

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(file.getPath()));
MainDocumentPart tempDocPart = wordMLPackage.getMainDocumentPart();
List<Object> obj = wordMLPackage.getMainDocumentPart().getContent();

RangeFinder rt = new RangeFinder("CTBookmark", "CTMarkupRange");
    new TraversalUtil(obj, rt);
    for (CTBookmark bm : rt.getStarts()) {
        if(bm.getName().equals("bookmarkstart1")){
            System.out.println(bm.getName());

        }
    }

My docx file bookmark name like bookmarkstart1, bookmarkend1, bookmarkstart2, bookmarkend2... so on. I need to read data between bookmarkstart1 and bookmarkend1.

Any help is appreciated.

enter image description here.

Mostch Romi
  • 531
  • 1
  • 6
  • 19

2 Answers2

0

I've done this before. There is my solution:

  1. you've got target bookmark start tag object CTBookmark start.

  2. you can get its parent by P p = (P) start.getParent(), its parent object should be an instance of P.

  3. List<Object> content = p.getContent(),get content of P tag, your bookmark start tag and end tag should be there, then you can get what you want.

ps. bookmark tag objects in the content of P maybe an instance of JAXBElement, you can cast to CTBookmark by using XmlUtil.unwrap().
end tag has same id with start tag, remember.

yang
  • 1
  • 1
-1

like this.

private void getConten(WordprocessingMLPackage wordMLPackage){
    MainDocumentPart tempDocPart = wordMLPackage.getMainDocumentPart();
    List<Object> obj = wordMLPackage.getMainDocumentPart().getContent();

    RangeFinder rt = new RangeFinder("CTBookmark", "CTMarkupRange");
    new TraversalUtil(obj, rt);
    CTBookmark start = null;
    List<CTMarkupRange> ends = rt.getEnds();
    for (CTBookmark bm : rt.getStarts()) {
        if(bm.getName().equals("targetBookmarkName")){
            start = bm;
            break;
        }
    }
    if(start == null){
        return;
    }
    Object parent = start.getParent();
    if(!( parent instanceof P)){
        return;
    }
    List<Object> content = ((P) parent).getContent();
    int startIndex = -1;
    int endIndex = -1;
    BigInteger startId = start.getId();
    for (int i = 0; i < content.size(); i++) {
        Object o = content.get(i);
        if(o == start){
            startIndex = i;
        }else if(o instanceof CTMarkupRange){
            if(startId.equals(((CTMarkupRange)o).getId())){
                endIndex = i;
                break;
            }
        }else if(o instanceof JAXBElement){
            Object unwrap = XmlUtils.unwrap(o);
            if(unwrap instanceof CTBookmark){
                // start tag
                if(startId.equals(((CTBookmark)unwrap).getId()){
                    startIndex = i;
                }
            }else if(unwrap instanceof CTMarkupRange){
                // end tag
                if(startId.equals(((CTMarkupRange)unwrap).getId())){
                    endIndex = i;
                }
            }
        }
    }
    if(startIndex < 0 || endIndex < 0){
        // content not found
        return;
    }
    List<Object> betweenContent = content.subList(startIndex, endIndex);

}
yang
  • 1
  • 1