Aspose Java - Replace a text in Word with Blanks

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

builder.moveTo(paragraph);
com.aspose.words.Shape shape = builder.insertImage(signatureImage);
shape.setAllowOverlap(true);
shape.setName("Approver 0");
// docParagraphs.remove(paragraph);
builder.getCurrentParagraph().getText().replace("Meilleures ", "ABC");

The text does not get replaced.Can you please help

Thanks and Regards,
Rajesh

This message was posted using Aspose.Live 2 Forum

Hi Rajesh,
Thanks for your request. You should use code like this:

builder.getCurrentParagraph().getRange().replace("Meilleures ", "ABC", false, true);

Please see the following link for more information:
https://docs.aspose.com/words/java/find-and-replace/
Best regards,

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

My code so far:

try
{
    Document doc = new Document("D:\\test.doc");
    DocumentBuilder builder = new DocumentBuilder(doc);
    String[] fields = {
        "Name",
        "Sirname",
        "Regnumber"
    };
    String[] values = {
        "James",
        "Bond",
        "007"
    };

    // first attempt
    for (int i = 0; i <fields.length; i++)
    {
        builder.insertField(fields[i], values[i]);
    }

    // second attempt
    // doc.getMailMerge().execute(fields,values);

    doc.save("D:\\FinalTest.doc", SaveFormat.DOC);
}
catch (Exception e)
{
    System.out.println("Error, call 112");
}

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,