I have a document with several headings and between 2 headings I only have a bookmark. What I’m trying to achieve is replace the bookmark with some HTML / text.
The problem is that, if I change the bookmark text and remove it the next heading formatting seems to be messed up.
Attached is the docx file that I’m reading, the resulting output file (***_out.docx)and here’s the code I’m using.
private void test2()
{
try
{
com.aspose.words.License license = new com.aspose.words.License();
license.setLicense(dir + "Aspose.Words.lic");
Document doc = new Document(inputFile);
FormFieldCollection formFields = doc.getRange().getFormFields();
db = new DocumentBuilder(doc);
System.out.println("" + formFields.getCount());
Iterator nodeIterator = formFields.iterator();
while (nodeIterator.hasNext())
{
FormField f = (FormField) nodeIterator.next();
System.out.println(f.getName() + "||" + f.getResult());
}
System.out.println("\n\nmail merge");
String[] strings = doc.getMailMerge().getFieldNames();
for (String string: strings)
{
System.out.println("fieldnames ||" + string);
}
System.out.println("bookmarks");
BookmarkCollection bookmarks = doc.getRange().getBookmarks();
for (Bookmark bookmark: bookmarks)
{
System.out.println(bookmark.getName() + "||" + bookmark.getText());
}
ArrayList ourBookMarks = findOurBookmarks(doc);
// wrong
Bookmark bookmark = ourBookMarks.get(0);
db.moveToBookmark(bookmark.getName(), true, false);
db.insertHtml("<b>replacing text</b>");
bookmark.setText("");
bookmark.remove();
doc.save(dir + "exemplo_simples_out.docx", SaveFormat.DOCX);
}
catch (Exception e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}