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

Free Support Forum - aspose.com

IReplacingCallback does not keep orientation on replaced documents

Using IReplacingCallback as explained on https://apireference.aspose.com/words/net/aspose.words.replacing/ireplacingcallback/methods/replacing does not keep PageSetup or page sections.

If you replace a text with a document that has different page orientation it will keep the first one.

Document a -> 1 page orientation = 1 (vertical)
Document b -> 3 pages orientation = 1,2,2 (vertical, horizontal, horizontal)

a.getRange.replace(“TEXTTOREPLACE”, “”, options)
Having the callback including document b on that replacement it will put all sections with orientation = 1

Any tip?

@tsyses,

Please compress the following resources into ZIP format and attach the .zip file here for testing:

  • A simplified source Word document
  • Aspose.Words v21.7 generated output DOCX document showing the undesired behavior
  • Your expected Word file showing the desired output. You can create this file manually by using MS Word.
  • Please also create a standalone simplified Console Application (source code without compilation errors) that helps us to reproduce this problem on our end and attach it here for our testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

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

Here you have

README.txt
Source file = AsposeTest.java
Source documents simplified = IREPLACE_TEXT_TO_REPLACE.docx, IREPLACE_CONTENT_TO_REPLACE.docx
Generated output = output.docx
The generated output should have page breaks and page orientations as original doc (IREPLACE_CONTENT_TO_REPLACE.docx)
Expected output = expected_output.docx

To compile or run you need to put aspose-words-21.7-jdk16.jar on lib directory
File to compile source and generate jar = compile-create-jar.bat
File to run the test = run.bat
test_replacing.zip (76.3 KB)

@tsyses,

You can find text & replace it with content of another Word document by using the following code of Aspose.Words for Java API:

Document docWithTextToBeReplaced = new Document("C:\\Temp\\test_replacing\\IREPLACE_TEXT_TO_REPLACE.docx");
Document docWithContent = new Document("C:\\Temp\\test_replacing\\IREPLACE_CONTENT_TO_REPLACE.docx");
ReplacingCallbackTest callback = new ReplacingCallbackTest(docWithContent);
FindReplaceOptions options = new FindReplaceOptions(callback);
options.setDirection(1);
docWithTextToBeReplaced.getRange().replace("TEXT_TO_REPLACE", "", options);
docWithTextToBeReplaced.save("C:\\temp\\test_replacing\\awjava-21.7.docx", com.aspose.words.SaveFormat.DOCX);

static class ReplacingCallbackTest implements IReplacingCallback {
    Document doc1;

    public ReplacingCallbackTest(Document d) {
        doc1 = 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();

        // The first (and may be the only) run can contain text before the match,
        // in this case it is necessary to split the run.
        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)) {
            runs.add(currentNode);
            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);
            runs.add(currentNode);
        }

        //// to insert Document
        DocumentBuilder builder = new DocumentBuilder((Document) e.getMatchNode().getDocument());
        builder.moveTo((Run) runs.get(runs.size() - 1));

        builder.insertDocument(doc1, ImportFormatMode.USE_DESTINATION_STYLES);

        for (Run run : (Iterable<Run>) runs)
            run.remove();

        // 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);
        afterRun.setText(run.getText().substring(position));
        run.setText(run.getText().substring((0), (0) + (position)));
        run.getParentNode().insertAfter(afterRun, run);
        return afterRun;
    }
}

Thanks, it worked as expected.

1 Like

Hello again, I reproduced this issue with another document and the code you posted:

test_replacing.zip (88.2 KB)

@tsyses,

Please check if the following solution is acceptable for you?

Document docWithTextToBeReplaced = new Document("C:\\Temp\\test_replacing (1)\\IREPLACE_TEXT_TO_REPLACE.docx");
Document docWithContent = new Document("C:\\Temp\\test_replacing (1)\\IREPLACE_CONTENT_TO_REPLACE.docx");
ReplacingCallbackTest callback = new ReplacingCallbackTest();
FindReplaceOptions options = new FindReplaceOptions(callback);
options.setDirection(1);
docWithTextToBeReplaced.getRange().replace("TEXT_TO_REPLACE", "", options);

String bookmark_Name = "bm_0";
Bookmark bookmark = docWithTextToBeReplaced.getRange().getBookmarks().get(bookmark_Name);
Section sectionOfBookmark = (Section) bookmark.getBookmarkStart().getAncestor(NodeType.SECTION);

DocumentBuilder builder = new DocumentBuilder(docWithTextToBeReplaced);
builder.moveToBookmark(bookmark_Name);
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);

int index = docWithTextToBeReplaced.getSections().indexOf(sectionOfBookmark) + 1;
for (Section section : docWithContent.getSections()) {
    Section importedSection = (Section) docWithTextToBeReplaced.importNode(section, true);
    docWithTextToBeReplaced.getSections().insert(index, importedSection);
    index++;
}

docWithTextToBeReplaced.save("C:\\temp\\test_replacing (1)\\awjava-21.7.docx", com.aspose.words.SaveFormat.DOCX);

static class ReplacingCallbackTest implements IReplacingCallback {
    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();

        // The first (and may be the only) run can contain text before the match,
        // in this case it is necessary to split the run.
        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)) {
            runs.add(currentNode);
            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);
            runs.add(currentNode);
        }

        //// to insert Document
        DocumentBuilder builder = new DocumentBuilder((Document) e.getMatchNode().getDocument());
        builder.moveTo((Run) runs.get(runs.size() - 1));

        builder.startBookmark("bm_0");
        builder.endBookmark("bm_0");

        for (Run run : (Iterable<Run>) runs)
            run.remove();

        // 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);
        afterRun.setText(run.getText().substring(position));
        run.setText(run.getText().substring((0), (0) + (position)));
        run.getParentNode().insertAfter(afterRun, run);
        return afterRun;
    }
}

Hello,
I have an api that replaces bookmarks with documents that can be of any kind, will this solution work for any document? I cannot have different solutions depending on the document content, the api is generic.

@tsyses,

Yes, copying all the sections of word document at a bookmarked place should work most of the times. However, if you find any other scenario where the above solution does not work, then please do share those Word documents here for further testing on our end.