Using Aspose, how to mark bookmarks in the same position?

Hello,
I’m having some difficulties.
I have a bookmarked file (MarkFile.docx) with three marks on it, and then I have another file (newFile.docx) with some data changes in the same place as the previous file. How can I manipulate both files with aspose, and then in the same place as the second file (newFile.docx), I can change the data in the same place as the second file (newFile.docx). How can I use aspose to manipulate both files and then add the same bookmarks to the second file (newFile.docx) in the same location as the first file?
newFile.docx (14.2 KB)

MarkFile.docx (13.9 KB)

Is there any way to do this?

@kellach As I can see the newFile.docx document does not have any bookmarks. If you have control over the document creation, you can put the bookmarks with the same names as in MarkFile.docx into newFile.docx. In this case you will be able to get bookmarks’ content from one document and put it into the bookmarks in another document. I am afraid without bookmarks or other placeholders in the destination document, there is no way to determine place where the content should be inserted unless structures of both documents are identical, that is not the case in the provided examples.

@alexey.noskov
Hi alexey,
We found that word has a review function. aspose can compare the differences between the two files, is it possible for us to use this review function to find the differences between newFile and markfile, and then the differences in the part of the RevisionCollection by aspose way, to find the same position, and then add bookmarks in that position. Is it possible to use this idea to do this effect? If so, is there any direction or documentation to draw from?

@kellach You can use compare your documents using the following simple code:

Document v1 = new Document("C:\\Temp\\MarkFile.docx");
Document v2 = new Document("C:\\Temp\\newFile.docx");
v1.compare(v2, "aspose", new Date());
v1.save("C:\\Temp\\out.docx");

As a result the following document is produced: out.docx (12.1 KB)

You can get differences between the document as revisions using the following code:

Document v1 = new Document("C:\\Temp\\MarkFile.docx");
Document v2 = new Document("C:\\Temp\\newFile.docx");
v1.compare(v2, "aspose", new Date());

for (RevisionGroup g : v1.getRevisions().getGroups())
{
    System.out.println("Revision author: " + g.getAuthor() +
            ";\r\nRevision type: " + g.getRevisionType() +
            ";\r\nRevision text: '" + g.getText() + "'");
}

@alexey.noskov
We understand that we could get the text, type and author for those changes via revision. We wonder:

1.If we could get the position of those change by aspose api

We notice that under revision, there are lots of change logs in reviewing pane. One could click previous or next to switch the revisions. In this case, we want to get the position of all the changes.

2.If we could get the information of the changed text

Here is our thought. If we could get the position of each changes, we could know that the text that is deleted has a bookmark (named A). Then, the revisied text will be added after the deleted text. We want to add a some bookmark (A) on the added text.

Can i use RevisionGroup or Revision api that i can find the change place ? Then i can get the change place (Like node) , I can use some node api that i can add bookmark on this area .

@kellach

Do you mean XY coordinates of the node under the revision or the parent node of the revision? If it is required to get the node under the revision, you can use Revision.getParentNode().
If it is required to get XY coordinates of the node under the revision, you can use LayoutCollector and LayoutEnumerator classes.

If it is simply required to wrap the revised text into a bookmark, you can simply add bookmark start before Revision.ParentNode and bookmark end after it. For example used the following code:

Document v1 = new Document("C:\\Temp\\MarkFile.docx");
Document v2 = new Document("C:\\Temp\\newFile.docx");
v1.compare(v2, "aspose", new Date());
        
int i=0;
for(Revision r : v1.getRevisions())
{
    Node parentNode = r.getParentNode();
    if(parentNode instanceof Inline)
    {
        String bkName = "bk"+i;
        i++;
        parentNode.getParentNode().insertBefore(new BookmarkStart(v1, bkName), parentNode);
        parentNode.getParentNode().insertAfter(new BookmarkEnd(v1, bkName), parentNode);
    }
}
        
v1.save("C:\\Temp\\out.docx");

@alexey.noskov
I see.
I’m going to try.
Thank you so much for your support and help!

1 Like