java - How can i delete a bookmark using docx4j? -
strange thing. i'm trying delete "_goback" bookmark in docx file, because it's ruining variableprepare (interesting, different topic). have simple template, , there 1 bookmark, is:
<w:bookmarkstart w:id="0" w:name="_goback"/> <w:bookmarkend w:id="0"/> it appears anytime make change in template, in place of last change.
this code:
maindocumentpart main = docx.getmaindocumentpart(); list<object> bmlist = getallelementfromobject(main, ctbookmark.class); ctbookmark bm = (ctbookmark) bmlist.get(0); system.out.println(bm.getname()); gives me "_goback", i'm sure bookmark found correctly. strangely if this:
getallelementfromobject(main, ctbookmark.class).remove(0); the bookmark doesn't deleted. check, code:
system.out.println(getallelementfromobject(main, ctbookmark.class).size()); getallelementfromobject(main, ctbookmark.class).remove(0); system.out.println(getallelementfromobject(main, ctbookmark.class).size()); returns
1 1 should use other approach delete bookmark in docx4j?
you don't provide code method getallelementfromobject, guess doesn't find ctbookmark object wrapped in jaxb element.
from bookmarksdeleter.java sample:
// can't remove object parent, // since in parent, may wrapped in jaxbelement (object ox : thelist) { if (xmlutils.unwrap(ox).equals(bm)) { return thelist.remove(ox); } you need handle ctmarkuprange?
so yeah, delete bookmarks using docx4j, see linked sample, or try rangefinder.java
Comments
Post a Comment