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

Free Support Forum - aspose.com

How to replace text with same size of text box using Java

i also looking for solution for this problem,code what you provide just figure out location of paragraph.
our need is that blank content of Run ,but need preserve document’ layout


Could you please ZIP and attach your input and expected output Word documents? We will then provide you more information about your query along with code example.

textreplace.zip (31.5 KB)


We suggest you please check the code examples shared in the following article.
Find and Replace

Please use the following code example to get the desired output. Hope this helps you.

Map<String,String> data = new HashMap<String, String>();
data.put("name", "");
data.put("age", "22");
data.put("gender", "female");
data.put("hobby", "rap");

Document doc = new Document(MyDir + "import.doc");
Pattern pattern = Pattern.compile("\\$(.?)\\{(.*?)\\}");
FindReplaceOptions options = new FindReplaceOptions();
ReplaceEvaluatorSpaces evaluatorSpaces =  new ReplaceEvaluatorSpaces(data);
doc.getRange().replace(pattern, "", options);

doc.save(MyDir + "out.docx");

public class ReplaceEvaluatorSpaces implements IReplacingCallback {
    public  Map<String,String> data;
     ReplaceEvaluatorSpaces(Map<String,String> d)
        this.data = d;
    public int replacing(ReplacingArgs e) throws Exception {
        // This is a Run node that contains either the beginning or the complete match.
        Node currentNode = e.getMatchNode();
        String key = e.getMatch().group(2).trim();
        String value = data.get(key);
        if (e.getMatchOffset() > 0)
            currentNode = splitRun((Run) currentNode, e.getMatchOffset());

        ArrayList runs = new ArrayList();

        // Find all runs that contain parts of the match string.
        int remainingLength = e.getMatch().group().length();
        while ((remainingLength > 0) && (currentNode != null) && (currentNode.getText().length() <= remainingLength)) {
            remainingLength = remainingLength - currentNode.getText().length();

            // Select the next Run node.
            // Have to loop because there could be other nodes such as BookmarkStart etc.
            do {
                currentNode = currentNode.getNextSibling();
            } while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN));

        // Split the last run that contains the match if there is any text left.
        if ((currentNode != null) && (remainingLength > 0)) {
            splitRun((Run) currentNode, remainingLength);
        String str = "";
        for (Run run : (Iterable<Run>) runs)
            for(int i = 0; i < run.getText().length() - value.length() ;i++)
                str += " ";
            run.setText(value + str);

        // Signal to the replace engine to do nothing because we have already done all what we wanted.
        return ReplaceAction.SKIP;

     * Splits text of the specified run into two runs. Inserts the new run just
     * after the specified run.
    private  Run splitRun(Run run, int position) throws Exception {
        Run afterRun = (Run) run.deepClone(true);
        run.setText(run.getText().substring((0), (0) + (position)));
        run.getParentNode().insertAfter(afterRun, run);
        return afterRun;


image.png (30.7 KB)
i had try this solution,but it’s accuracy is not enough for me.
simply padding blank-space can’t accurately preserve original pixel width.
specially,pixel width of one chinese character is different with pixel width of one blank-space


Please note that MS Word document is flow document and Aspose.Words mimics the behavior of MS Word. You are facing the expected behavior of Aspose.Words. If you replace the shared content with spaces using MS Word, you will get the same output.

In your case, we suggest you please use monospaced font in your document.

You may create a table with fixed column width and insert each text item e.g. name, age etc. in each cell of table’s row. Hope this helps you.

i have found resolution for my problem,i first calculate size and position of text through bookmark,then inserting a textbox with same size cover it,lastly,i convert word document to pdf。i come cross this problem when i prepare to implement pdf template teck base aspose.word.


It is nice to hear from you that your problem has been resolved. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.