by arulface » Mon Apr 06, 2020 1:33 am
Here is the complete code to add BookMark in a document using docx4j.
import com.tnq.aceauto.bookmark.BookMarkSetting;
import com.tnq.aceauto.core.Alterations;
import com.tnq.aceauto.model.DataCaptureDto;
import com.tnq.aceauto.util.ACEConstants;
import org.apache.log4j.Logger;
import org.docx4j.TraversalUtil;
import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.*;
import javax.xml.bind.JAXBElement;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
public class AddBookMark {
static final Logger log = Logger.getLogger(AddBookMark.class);
public static void main(String as[]) throws Exception{
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new FileInputStream("D:\\MyProjects\\TNQ\\ACETool\\ACE_Samples\\SampleFiles_set2\\ACE_100_ACE\\Tool_Output/JCCT_01113_tud_ACE.docx"));
String outputfilepath="D:\\ACEAUTOTOOL\\temp/BookmarkAdded.docx";
AddBookMark addBookmark = new AddBookMark();
System.out.println("Before bookmark method");
addBookmark.addBookmark(wordMLPackage, BigInteger.valueOf(1),outputfilepath);
System.out.println("after bookmark method");
}
public void addBookmark(WordprocessingMLPackage wordMLPackage, BigInteger i, String outputFilepath) throws Docx4JException, FileNotFoundException {
// MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
System.out.println("in bookmark method");
List<Object> objectList = getAllElementFromObject(wordMLPackage.getMainDocumentPart());
System.out.println("--------------runInsList---------"+objectList.size());
StringBuffer sb = new StringBuffer();
BigInteger ID = BigInteger.valueOf(100);
for(Object o: objectList) {
ObjectFactory factory = Context.getWmlObjectFactory();
ID = BigInteger.valueOf(ID.intValue()+1);
// Next, bookmark start
CTBookmark bm = factory.createCTBookmark();
bm.setId(ID);
bm.setName("Actual_missing_"+ID);
CTMarkupRange mr = factory.createCTMarkupRange();
mr.setId(ID);
//RunIns ins = (RunIns) runIns;
//System.out.println(o.getClass()+"--------------before if---------");
if (o instanceof Text) {
Text textNode = (Text) o;
// R r = (R) o;
String textContent = textNode.getValue();
if(! (textContent.contains("\\o") || textContent.contains("\"aff") || textContent.contains("\\l") || textContent.contains("ffa\"") || textContent.contains("ffb\"") || textContent.contains("\\l")) ) {
sb.append(textContent);
System.out.println(" Text : " + textContent);
JAXBElement<CTMarkupRange> bmEndtext = factory.createTcBookmarkEnd(mr);
JAXBElement<CTBookmark> bmStarttext = factory.createTcBookmarkStart(bm);
R r = (R) textNode.getParent();
r.getContent().add(bmStarttext);
r.getContent().add(bmEndtext);
}else
System.out.println(" FOUND aff : " + textContent);
}
if (o instanceof RunIns ) {
//System.out.println(" MATCHED");
Integer charPosition = sb.length();
//Use your own approach to get value from RunIns
System.out.println(" RunIns content "+Alterations.getChildrenVal(o));
RunIns ins = (RunIns)o;
JAXBElement<CTMarkupRange> bmEnd = factory.createRunInsBookmarkEnd(mr);
JAXBElement<CTBookmark> bmStart = factory.createRunInsBookmarkStart(bm);
ins.getCustomXmlOrSmartTagOrSdt().add(bmStart);
ins.getCustomXmlOrSmartTagOrSdt().add(bmEnd);
// System.out.println(" RunIns bookmark set done ");
}
if (o instanceof RunDel) {
Integer charPosition = sb.length();
RunDel del = (RunDel)o;
JAXBElement<CTMarkupRange> bmEnd = factory.createRunDelBookmarkEnd(mr);
JAXBElement<CTBookmark> bmStart = factory.createRunDelBookmarkStart(bm);
del.getCustomXmlOrSmartTagOrSdt().add(bmStart);
del.getCustomXmlOrSmartTagOrSdt().add(bmEnd);
// System.out.println(" RunDel : "+o);
}
if(o instanceof R.CommentReference){
Integer charPosition = sb.length();
R.CommentReference commentReference = (R.CommentReference) o;
}
}
wordMLPackage.save(new FileOutputStream(outputFilepath));
}
public List<Object> getAllElementFromObject(Object obj) {
List<Object> result = new ArrayList<Object>();
if (obj instanceof JAXBElement)
obj = ((JAXBElement<?>) obj).getValue();
if (obj instanceof ContentAccessor) {
List<?> children = ((ContentAccessor) obj).getContent();
for (Object child : children) {
result.addAll(getAllElementFromObject(child));
}
}else{
result.add(obj);
}
return result;
}
}