Have a go with this; as always, the main method shows how to cal the text and
I have mentioned the current limitations in the doc block.
/**
* First attempt at inserting text at a bookmark defined within a Word
document.
* Note that there are a few SERIOUS limitations with the code as it stands:
*
* Firstly, it will not insert text at a bookmark defined within a table
cell. It
* should be possible to amend this failing by simply iterating through the
tables
* the document contains in much the same way that this implementation
iterates
* through the document's paragraphs. TO DO.
*
* Secondly, it is possible to use bookmarks in a couple of ways. They can
* simply be inserted into the document as place markers or it is possible
to
* select a character, word or number of words and stipulate that these
serve as
* a bookmark. I am assuming that, in the latter case, the value that is to
be
* inserted at the bookmark will replace all of these selected characters
and,
* as yet, the code does not do this. TO DO.
*
* Modifications to follow.
*
* @author Mark Beardsley
* @version 0.1 10th June 2012
*/
public class DOCXTest {
private XWPFDocument document = null;
public DOCXTest() {
}
public final void openFile(String filename) throws IOException {
File file = null;
FileInputStream fis = null;
try {
file = new File(filename);
fis = new FileInputStream(file);
this.document = new XWPFDocument(fis);
}
finally {
try {
if(fis != null) {
fis.close();
fis = null;
}
}
catch(IOException ioEx) {
// Swallow this exception. It would have occured onyl
// when releasing the file handle and should not pose
// problems to later processing.
}
}
}
public final void saveAs(String filename) throws IOException {
File file = null;
FileOutputStream fos = null;
try {
file = new File(filename);
fos = new FileOutputStream(file);
this.document.write(fos);
}
finally {
if(fos != null) {
fos.close();
fos = null;
}
}
}
public final void insertAtBookmark(String bookmarkName, String
bookmarkValue) {
List<XWPFParagraph> paraList = null;
Iterator<XWPFParagraph> paraIter = null;
XWPFParagraph para = null;
List<CTBookmark> bookmarkList = null;
Iterator<CTBookmark> bookmarkIter = null;
CTBookmark bookmark = null;
XWPFRun run = null;
paraList = this.document.getParagraphs();
paraIter = paraList.iterator();
while(paraIter.hasNext()) {
para = paraIter.next();
bookmarkList = para.getCTP().getBookmarkStartList();
bookmarkIter = bookmarkList.iterator();
while(bookmarkIter.hasNext()) {
bookmark = bookmarkIter.next();
if(bookmark.getName().equals(bookmarkName)) {
run = para.createRun();
run.setText(bookmarkValue);
para.getCTP().getDomNode().insertBefore(run.getCTR().getDomNode(),
bookmark.getDomNode());
}
}
}
}
public static void main(String[] args) {
try {
DOCXTest docxTest = new DOCXTest();
docxTest.openFile("C:/temp/Doc1.docx");
docxTest.insertAtBookmark("WHOLE_WORD", "This should be inserted
at the WHOLE_WORD bookmark.");
docxTest.insertAtBookmark("MARK_ONE", "..and this at the
MARK_ONE bookmark.");
docxTest.saveAs("C:/temp/Doc1 With Bookmarks Updated.docx");
}
catch(Exception ex) {
System.out.println("Caught a: " + ex.getClass().getName());
System.out.println("Message: " + ex.getMessage());
System.out.println("Stacktrace follows:.....");
ex.printStackTrace(System.out);
}
}
}
I have tested it against a document with bookmarks inserted into paragraphs
and tables. As indicated, it will not work with tables yet but the fix ought
to be fairly straightforward (famous last words!!).
If my memory is not playing too many tricks, I think you said you had
documents where bookmarks with the same name appeared more than once; even
more than once in the same paragraph? Should this be the case, I am keen to
see if the code works because I was not able to create such a test document.
The copy of Word that I have access to does not allow me to insert two
bookmarks with the same name into the text and so I have been unable to try
it out against this scenario.
Will you let me know the results you have please?
Yours
Mark B
--
View this message in context:
http://apache-poi.1045710.n5.nabble.com/Replacing-the-value-of-the-bookmarks-tp5710052p5710115.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]