We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Set Values of Bookmarks contained inside FORMTEXT Fields in Word DOCX Document using C# .NET

We are using aspose.word for java to populate bookmarks in a word document. Some bookmarks are populating, while others arent. Below is the section of code we are using to replace. Could you please review and let us know?

private void replace(String documentPath, Map<String,String> bookmarkValueMap) throws Exception {
com.aspose.words.Document asposeDocument = new com.aspose.words.Document(documentPath);
BookmarkCollection bookMarkCollection = asposeDocument.getRange().getBookmarks();
bookMarkCollection.forEach(bookMark-> {
try {
if (bookmarkValueMap.containsKey(bookMark.getName())) {
String replacementText = bookmarkValueMap.get(bookMark.getName());
bookMark.setText(replacementText);
}
} catch (Exception e) {
e.printStackTrace();
}
});
asposeDocument.save(documentPath);
}

@akashkaware,

Please ZIP and attach the following resources here for testing:

  • Your simplified input Word document containing the Bookmarks
  • Aspose.Words for Java 20.9 generated output DOCX file showing the undesired behavior
  • Your expected DOCX file showing the desired output. You can create this document manually by using MS Word.
  • Please also create a standalone simple Java application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words JAR files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your particular scenario/issue and provide you more information.

Hi,

Please find attached a zip file containing the information you asked for.

Thanks,
Akash

eclipse-aspose-workspace.zip (204.4 KB)

@akashkaware,

It seems that Aspose.Words for Java is currently unable to set text of Bookmarks contained inside FORMTEXT fields of Word document. For the sake of any correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-21112. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.

Thank you. Could you please let us know approximately when we can expect this fix? It is holding up a deployment of a major release on our side.

Hi,

I consulted my team again, and they informed me that all of our bookmarks are inside FORMTEXT fields, yet some are working, and some are not working. Do you know why that might be?

Also, is there a workaround for the issue? Can bookmarks be created in any other way other than FORMTEXT? Please let me know if there is a way to do that so we can get the bookmarks working while you work on this fix.

@akashkaware,

You can try any of the following workarounds to fix the output:

  1. By taking out the BookmarkStart nodes from inside the FORMTEXT fields.

Java code:

Map<String, String> bookmarkValueMap = new HashMap<String, String>();
bookmarkValueMap.put("OFFEN_NAME", "JOHN SMITH");
bookmarkValueMap.put("DOB", "2020/03/03");
bookmarkValueMap.put("MIN", "00043948487");
bookmarkValueMap.put("CRT_DATE_REQ", "2005/06/06");
bookmarkValueMap.put("COURT_DATE", "2005/06/06");
bookmarkValueMap.put("COURT", "BIG COURT VALUE");
bookmarkValueMap.put("OFFENCES", "OFFENCES");
bookmarkValueMap.put("STAFF_NAME", "ROGER DINTWIT");

Document doc = new Document("C:\\Temp\\eclipse-aspose-workspace\\SARN_beforeReplacement_9.docx");

for (Field field : doc.getRange().getFields()) {
    if (field.getType() == FieldType.FIELD_FORM_TEXT_INPUT) {
        FieldFormText fieldFormText = (FieldFormText) field;
        BookmarkStart bookmarkStart = null;
        if (fieldFormText.getStart().getNextSibling().getNodeType() == NodeType.BOOKMARK_START) {
            bookmarkStart = (BookmarkStart) fieldFormText.getStart().getNextSibling();
        }

        if (bookmarkStart != null) {
            fieldFormText.getStart().getParentNode().insertBefore(bookmarkStart, fieldFormText.getStart());
        }
    }
}

BookmarkCollection bookMarkCollection = doc.getRange().getBookmarks();
bookMarkCollection.forEach(bookMark -> {

    System.out.println(bookMark.getName());

    if (bookmarkValueMap.containsKey(bookMark.getName())) {
        String replacementText = bookmarkValueMap.get(bookMark.getName());
        try {
            bookMark.setText(replacementText);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
});

doc.save("C:\\temp\\eclipse-aspose-workspace\\awjava-20.9.docx");
  1. If there are fields in your Word document, it would be better to use the form fields instead of bookmarks.

Java code:

Map<String, String> bookmarkValueMap = new HashMap<String, String>();
bookmarkValueMap.put("OFFEN_NAME", "JOHN SMITH");
bookmarkValueMap.put("DOB", "2020/03/03");
bookmarkValueMap.put("MIN", "00043948487");
bookmarkValueMap.put("CRT_DATE_REQ", "2005/06/06");
bookmarkValueMap.put("COURT_DATE", "2005/06/06");
bookmarkValueMap.put("COURT", "BIG COURT VALUE");
bookmarkValueMap.put("OFFENCES", "OFFENCES");
bookmarkValueMap.put("STAFF_NAME", "ROGER DINTWIT");

Document doc = new Document("C:\\Temp\\eclipse-aspose-workspace\\SARN_beforeReplacement_9.docx");

FormFieldCollection formFieldCollection = doc.getRange().getFormFields();
formFieldCollection.forEach(bookMark -> {

    if (bookmarkValueMap.containsKey(bookMark.getName())) {
        String replacementText = bookmarkValueMap.get(bookMark.getName());
        try {
            bookMark.setTextInputValue(replacementText);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
});

doc.save("C:\\temp\\eclipse-aspose-workspace\\awjava-20.9-2nd.docx");

@akashkaware,

It is to inform you that we have completed the work on WORDSNET-21112 and concluded to close this issue with “Not a Bug” status. Please see my previous post for details.

Hi Awais,

Your previous post mentioned that your suggestion was a workaround, not a fix. Can a fix be made so that we dont have use the workaround going forward?

@akashkaware,

I have logged your concerns in our issue tracking system and will keep you posted here on any further updates.

@akashkaware,

After discussing the matter further, we have decided that we are not going to fix this because even VBA fails when trying to set the BAD bookmarks:

ActiveDocument.Bookmarks("OFFEN_NAME").Range.Text = "Blah"

---------------------------
Microsoft Visual Basic for Applications
---------------------------
Run-time error '6028':

The range cannot be deleted.