Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOo: UNO (Java) TrackedChanges: How to accept (or hide) Tracked Changes when Document is hidden?

My Problem: I write an automated system that needs to read .doc and .odt, performs some operation on it and exports it to pdf again.

Currently that works fine for everything I need, I could solve all problems until this one:

If a user provides a Document that had recorded changes (Redlines) I need to automatically accept all that changes or hide them.

I could solve that one with the code below as long as the OOo is showing on screen. When I launch it hidden, my calls do nothing at all.

So, here is what I do currently:

    // DO NOT try to cast this to Desktop as com.sun.star.frame.Desktop is NOT a valid class!
    // keep it as Object and cast it to XDesktop later (via queryInterface)
    Object desktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext);
    XMultiServiceFactory xFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(
        XMultiServiceFactory.class, xMCF);
    // what goes for desktop above is valid for DispatchHelper as well.
    Object dispatchHelper = xFactory.createInstance("com.sun.star.frame.DispatchHelper");

    // the DispatchHelper is the class that handles the interaction with dialogs.
    XDispatchHelper helper = (XDispatchHelper) UnoRuntime.queryInterface(
        XDispatchHelper.class, dispatchHelper);
    XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class, desktop);
    XFrame xFrame = xDesktop.getCurrentFrame();
    XDispatchProvider xDispatchProvider = (XDispatchProvider) UnoRuntime.queryInterface(XDispatchProvider.class, xFrame);

    // We issute the Track Changes Dialog (Bearbeiten - Änderungen // Edit - Changes) and tell it
    // to ACCEPT all changes.
    PropertyValue[] acceptChanges = new PropertyValue[1];
    acceptChanges[0] = new PropertyValue();
    acceptChanges[0].Name = "AcceptTrackedChanges";
    acceptChanges[0].Value = Boolean.TRUE;
    helper.executeDispatch(xDispatchProvider, ".uno:AcceptTrackedChanges", "", 0, acceptChanges);

    // We issue it again to tell it to stop showing changes.
    PropertyValue[] showChanges = new PropertyValue[1];
    showChanges[0] = new PropertyValue();
    showChanges[0].Name = "ShowTrackedChanges";
    showChanges[0].Value = Boolean.FALSE;
    helper.executeDispatch(xDispatchProvider, ".uno:ShowTrackedChanges", "", 0, showChanges);

My current guess is that I cannot call this because being hidden, I have no Frame to call any dispatcher to. But I could not find a way to get the Dispatcher for the Component.

I already tried to dispatch TrackChanges as well (to FALSE) but that didn't do it either.

like image 502
Angelo Fuchs Avatar asked Oct 09 '22 13:10

Angelo Fuchs


1 Answers

After spending two days understanding the OOo API, I realized that the document isn't loaded in the frontend which is why this approach fails. However, you can modify the document's property directly:

XPropertySet docProperties = UnoRuntime.queryInterface(XPropertySet.class, document);
docProperties.setPropertyValue("RedlineDisplayType", RedlineDisplayType.NONE);

The property name "RedlineDisplayType" can be found in the RedlinePortion documentation

like image 88
Gjorgi Kjosev Avatar answered Oct 12 '22 12:10

Gjorgi Kjosev