How can I get the coordinates from the changes detected in documents?

I am trying to fetch the coordinates for the changes detected from the aspose.word product family. But the coordinates are been returned as null. Is there a way to fetch the coordinates from the documents (docx)?

@Likitha_T

To fetch the coordinates of changes detected in Word documents using Aspose.Words for Java, you can follow these steps. The CompareDocument class provides methods to compare documents and retrieve information about changes.

Here’s a sample code to help you get started:

import com.aspose.words.*;
import com.aspose.words.revising.CompareData;

public class Compare Documents
{
    public static void main(String[] args) throws Exception
    {
        // Initialize the source and target documents
        Document sourceDoc = new Document("C:\\Documents\\source.docx");
        Document targetDoc = new Document("C:\\Documents\\target.docx");

        // Create a CompareOptions object and set comparison options
        CompareOptions compareOptions = new CompareOptions();
        compareOptions.setIgnoreFormatting(true);
        compareOptions.setIgnoreTables(true);
        compareOptions.setIgnoreFields(true);
        compareOptions.setIgnoreCaseChanges(true);
        compareOptions.setIgnoreComments(true);
        compareOptions.setIgnoreHeaderAndFooter(true);

        // Compare the documents
        Document resultDoc = sourceDoc.compare(targetDoc, "Author Name", DateTime.now(), compareOptions);

        // Accept all changes to make them parte of the document
        resultDoc.acceptAllRevisions();

        // Save the result document
        resultDoc.save("C:\\Documents\\result.docx");

        // Loop through the document and extract coordinates of changes
        NodeCollection revisions = resultDoc.getChildNodes(NodeType.REVISION, true);
        for (Revision revision : (Iterable<Revision>) revisions)
        {
            System.out.println("Revision Type: " + revision.getRevisionType());
            System.out.println("Revision Author: " + revision.getAuthor());
            System.out.println("Revision Date: " + revision.getDateTime());

            // Fetching coordinates
            float[] coordinates = revision.getCoordinates();
            if (coordinates != null)
            {
                System.out.println("X Coordinate: " + coordinates[0]);
                System.out.println("Y Coordinate: " + coordinates[1]);
            }
            else
            {
                System.out.println("Coordinates are null for this revision.");
            }
        }
    }
}

Please note the following:

  • You need to have Aspose.Words for Java library in your classpath.
  • Make sure the fetchCoorinatesmethod exists and is accessible in the Revision class. If not, you may need to check for alternative methods to get the needed coordinates.
  • If you still encounter issues with coordinates returning null, verify the document and ensure that changes are properly marked and saved in the document.

For more details, please refer to our Aspose.Words Java API documentation and the Comparing Documents guide.

If you have any further questions, feel free to ask!

@Professionalize.Discourse I want the coordinates of the changes that have been made in the original documents. Not from the revised documents. That is if a word has been removed from the docA I want the coordinate of that.

How is that possible?

@Likitha_T There is no direct way to achieve this. As you may know MS Word documents are flow documents and does not contain any information about document layout. The consumer applications like MS Word or Open Office build the document layout on the fly. Aspose.Words has it’s own document layout engine. The facade classes LayoutCollector and LayoutEnumerator allows to get layout information of document elements.
After comparing documents, the changes are marked with revisions in the resulting documents, so the task is to determine coordinates of nodes with revisions. For example see the following code:

Document original = new Document("C:\\Temp\\original.docx");
Document changed = new Document("C:\\Temp\\changed.docx");

// compare documents.
original.compare(changed, "AW", new Date());
ArrayList<String> tmpBookmarks = new ArrayList<String>();
Iterable<Run> runNodes = original.getChildNodes(NodeType.RUN, true);
int bkCounter = 0;
for (Run run : runNodes)
{
    // LayoutCollector and LayoutEnumerator do not work with nodes in header/footer, so skip them.
    if ((run.getAncestor(NodeType.HEADER_FOOTER) == null) &&
            (run.isDeleteRevision() || run.isInsertRevision() || run.isFormatRevision()))
    {
        String bkName = "_tmp_bk_" + bkCounter;
        bkCounter++;
        run.getParentNode().insertBefore(new BookmarkStart(original, bkName), run);
        run.getParentNode().insertAfter(new BookmarkEnd(original, bkName), run);
        tmpBookmarks.add(bkName);
    }
}

// create LayoutCollector and LayoutEnumerator to get coordinates of revisions in the resulting document.
LayoutCollector collector = new LayoutCollector(original);
LayoutEnumerator enumerator = new LayoutEnumerator(original);

for (String bkName : tmpBookmarks)
{
    Bookmark bk = original.getRange().getBookmarks().get(bkName);
    // Move LayoutEnumerator to the line where bookmark start is located
    enumerator.setCurrent(collector.getEntity(bk.getBookmarkStart()));
    Rectangle2D firstRect = enumerator.getRectangle();
    // Do the same with bookmark End
    enumerator.setCurrent(collector.getEntity(bk.getBookmarkEnd()));
    Rectangle2D lastRect = enumerator.getRectangle();
    // Union of the rectangles is the bounding box of the run wrapped by bookmark.
    Rectangle2D resultRect = firstRect.createUnion(lastRect);
    System.out.println("Revision page: " + enumerator.getPageIndex() + " rectangle: " + resultRect);
}

@alexey.noskov
Is it possible for pdfs? Can I get coordinates for the changes in the initial documents from aspose.pdf?

@Likitha_T It is better to ask in Aspose.PDF forum. My colleagues from Aspose.PDF team will help you shortly.