Free Support Forum - aspose.com

Bug when removing a bookmark from the doc

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("

replacing text

"); 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. } }

If I use the following code and after the bookmark I add a blank line everything seems to work OK.

(check docx attached, bug1_v2.docx)

bookmark = ourBookMarks.get(1);
            bookmark.setText("");
            db.moveToBookmark(bookmark.getName(), true, false);
            db.insertHtml("

hi

"); db.moveToBookmark(bookmark.getName(), false, true); db.getCurrentParagraph().remove(); bookmark.remove(); doc.save(dir + "exemplo_simples_out.docx", SaveFormat.DOCX);

Regarding the first post, this code seems to work with a bookmark between 2 headings (no blank lines). This is far from pretty so that’s my doubt if I’m doing something wrong…


Bookmark bookmark = ourBookMarks.get(0);
db.moveToBookmark(bookmark.getName(), false, true);
db.writeln("\n");
db.moveToBookmark(bookmark.getName(), true, false);
db.insertHtml(“

my book

”);
db.moveToBookmark(bookmark.getName(), true, false);
db.getCurrentParagraph().getNextSibling().getNextSibling().remove();
db.getCurrentParagraph().getNextSibling().remove();
db.getCurrentParagraph().remove();
bookmark.remove();

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. I think the following code should work in your case:

Document doc = new Document("C:\\Temp\\bug1.docx");

DocumentBuilder db = new DocumentBuilder(doc);

Bookmark bookmark = doc.getRange().getBookmarks().get("t2");

// Place cursor before bookmark.

db.moveToBookmark(bookmark.getName(), true, false);

// Insert HTML

db.insertHtml("

my book

");

// Remove paragraph with bookmark if BookmarkStart is the first node of the paragraph.

CompositeNode bkParent = bookmark.getBookmarkStart().getParentNode();

if (bkParent.getFirstChild().equals(bookmark.getBookmarkStart()))

{

bkParent.remove();

}

else

{

bookmark.setText("");

bookmark.remove();

}

doc.save("C:\\Temp\\out.doc");

Hope this helps.

Best regards.