Hi,
I am trying to locate a paragraph and a text string in the paragraph of a word document and then add shapes to it. Later I am trying to replace the text string with “”. For example
Hi there i have to do something similar, i have to use template, to do find and replace some patterns and to return content in new document. My problem is that automatic extracting of bookmarks seems to be unsupported (the old bookmarks seems to be removed in new document).
public static void main(String[] args)
{
try
{
Document doc = new Document("C:\\test.doc");
doc.getRange().replace("is", "James Bond", false, true);
doc.getRange().replace("име", "Андрей", true, true);
int bookmarkCount = doc.getRange().getBookmarks().getCount();
// obtain bookmarks
Bookmark[] book = new Bookmark[bookmarkCount];
String[] names = new String[bookmarkCount];
String[] texts = new String[bookmarkCount];
BookmarkStart[] bs = new BookmarkStart[bookmarkCount];
// get bookmarks content
for (int i = 0; i < bookmarkCount; ++i)
{
book[i] = doc.getRange().getBookmarks().get(i);
bs[i] = book[i].getBookmarkStart();
names[i] = book[i].getName();
texts[i] = book[i].getText();
}
// Save the modified document.
doc.save("C:\\FinalTest.doc");
Document docNew = new Document("C:\\FinalTest.doc");
for (int i = 0; i < bookmarkCount; i++)
{
Bookmark bookmark = null;
bookmark.setName(names[i]);
bookmark.setText(texts[i]);
}
docNew.save("C:\\FinalTest.doc");
}
catch (Exception e)
{
System.out.println("Error");
}
}
I have some troubles in placing the bookmarks back on the their places. Any ideas???
Hi
Thanks for your request. When you set text of bookmark, Aspose.Words removes all content between the corresponding bookmark start and end. Then it insert text between the corresponding BookmarkStart and BookmarkEnd. So if you have a nested bookmark it might be removed when you set text of this bookmark.
Could you please attach your document and describe your goals. Maybe we will help you to find an easier way to achieve what you need. For instance using mail merge: https://docs.aspose.com/words/net/types-of-mail-merge-operations/
Best regards,
Thanks for your fast and kind answer Alexey. The main idea is that the program should support various templates, it will do search and replace function in MS Word 2003-2007 with code.
Регистрационен номер: «Regnumber»
Име: «Name»
Фамилия: «Sirname»
I have already try to do this with Apache POI, but your product seems to be better and well-supported as I am reading your forum. I will receive HashMap<old value ,new value > and I have to replace old values in the template and make new document as result. In the final document the user wont be allowed to change the content of the document, but the this specific fields.
Best regards,
Yon
Hi
Thanks for your request. If you would like to fill the document with data and then fill the same document with other data, it is impossible to achieve using mail merge. This is how mail merge works: during mail merge, merge fields are replaced with values. So in final document, merge fields are no longer available. In MS Word, this works exactly the same.
If you need to fill document with data several times, you can consider using bookmarks or form fields. Or you can create the template document and use this template each time when you need to perform mailmerge.
Best regards,
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
Enables storage, such as cookies, related to analytics.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.