Get all Annotations/Comments/Notes

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
wanglong
Posts: 51
Joined: Fri Sep 09, 2022 10:57 am
Location: Beijing,China

Get all Annotations/Comments/Notes

Post by wanglong »

How to get all Annotations in XTextDocument?
Libre Office 7.6 on Windows 11.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Get all Annotations/Comments/Notes

Post by Villeroy »

Annotations are special types of TextFields. MRI records the following:

Code: Select all

Sub Snippet
  
  oTextFields = ThisComponent.getTextFields()
  oObj2 = oTextFields.createEnumeration()
  
  oObj3 = oObj2.nextElement()
End Sub
[Tutorial] Introduction into object inspection with MRI
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Get all Annotations/Comments/Notes

Post by JeJe »

The annotations will support the com.sun.star.text.textfield.Annotation service.

https://www.openoffice.org/api/docs/com ... le-ix.html
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Jurassic Pork
Posts: 24
Joined: Wed Oct 25, 2017 7:55 am
Location: France

Re: Get all Annotations/Comments/Notes

Post by Jurassic Pork »

Hello,
you can try something like this :

Code: Select all

Sub EnumAnnotations
  Dim oTextFields as Object, oTextField As Object
  CreateDbgDlg
  oTextFields = ThisComponent.getTextFields()
  for each oTextField in oTextFields
      if oTextField.supportsService("com.sun.star.text.textfield.Annotation") then
          DebugPrint "=====    " + oTextField.Author + ": " + _
                   Format(CDateFromUnoDateTime(oTextField.DateTimeValue), _
                   "d MMM yyyy hh:mm:ss") + "    ====="
          DebugPrint oTextField.Content
      end If
  Next
 End Sub
CreateDbgDlg create a non modal dialogbox (code here by JeJe) to display string from basic code with procedure DebugPrint

Annotations.gif
Annotations.gif (118.82 KiB) Viewed 2108 times

Friendly, J.P
OpenOffice 4.1.14 , LibreOffice 7.6.2.1 on Windows 11/ LibreOffice 7.3.7 on Lubuntu 22.04
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Get all Annotations/Comments/Notes

Post by Lupp »

To collect all the annotations contained in a Writer document (even if nested into any textcontent objects there) is simple. A next solution using the Basic object 'Collection' you find in the attached example:
annotationExperiments.odt
(32.68 KiB) Downloaded 26 times
An open question is how to make reasonable use of the result.

Also: The current LibO version 24.2 has a related bug https://bugs.documentfoundation.org/sho ... ?id=160138. It thwaits an attempt to get the number of the page containing the field. V 7.5 does it well AOO versions also do.
It will hopefully soon be fixed.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
wanglong
Posts: 51
Joined: Fri Sep 09, 2022 10:57 am
Location: Beijing,China

Re: Get all Annotations/Comments/Notes

Post by wanglong »

First of all, I want to express my sincere gratitude for everyone's enthusiastic assistance.

Just as Lupp said, 'how to make reasonable use of the result?'

You have already helped me find the method to query annotations. Now, let me describe my specific requirement. I want to select and copy the content pointed to by an annotation by clicking on it, namely the content at the anchor of the annotation. Then, I intend to paste it into another location within the document, or even into a different document.

Currently, my challenge lies in how to implement the listening of mouse click events on annotations, so as to identify which annotation I have clicked on, and then...
Libre Office 7.6 on Windows 11.
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Get all Annotations/Comments/Notes

Post by JeJe »

Code: Select all

with thiscomponent.currentcontroller
.select .viewcursor.textfield.anchor
end with
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
RoryOF
Moderator
Posts: 34619
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Get all Annotations/Comments/Notes

Post by RoryOF »

My experiments with Annotations (not in this thread) showed that all attempts to handle annotations (whether by extraction or merely listing) returned the Annotation nearest the end of the file, then all other annotations in order from the beginning of the file. Hubert Lambert suggested sorting them by their field anchor

viewtopic.php?p=495327#p495327

which worked, but it is an extra step. It would be nice if they were returned in strict order in the document - I can see no reason for this out-of-order return.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
wanglong
Posts: 51
Joined: Fri Sep 09, 2022 10:57 am
Location: Beijing,China

Re: Get all Annotations/Comments/Notes

Post by wanglong »

Code: Select all

XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor();
XTextField xTextField = UnoRuntime.queryInterface(XTextField.class, xViewCursor);
I get xTextField is equals to "null".
Libre Office 7.6 on Windows 11.
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Get all Annotations/Comments/Notes

Post by JeJe »

If the viewcursor isn't in a textfield you will get null.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
wanglong
Posts: 51
Joined: Fri Sep 09, 2022 10:57 am
Location: Beijing,China

Re: Get all Annotations/Comments/Notes

Post by wanglong »

I has comfirmed that the viewCursor is in the annotation area,but I still get "null" return. :crazy:
Libre Office 7.6 on Windows 11.
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Get all Annotations/Comments/Notes

Post by JeJe »

My code selects the anchor fine for me in LO as well as OO.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Get all Annotations/Comments/Notes

Post by Lupp »

I took the time to go a bit deeper into this, and must report that it wasn't just fun.
There are versions that accept anchors with non-empty strings, others do not.
With the most recent version of LibO (24.2) you cannot even select the anchor of an annotation, by user code, and therefore you cant't get positioning information with the help of the ViewCursor.
Many versions are stacking the annotations along the margin in a really funny way ...
What I could actually get depending on the version's bugs and regressions is contained in the attached demo. It uses a dedicated spreadsheet to gather and to sort the info about the annotations contained in a Writer document. If positioning information was not accessible, the sorting makes no sense, of course.
===Editing===
Sorry. I hadn't updated the Basic code on the document's pages.
I now attach the updated version.
annotationExperimentsUpdated.odt
(35.38 KiB) Downloaded 26 times
obsolete:
annotationExperiments.odt
(34.95 KiB) Downloaded 22 times
Last edited by Lupp on Mon Mar 11, 2024 11:42 pm, edited 1 time in total.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Get all Annotations/Comments/Notes

Post by JeJe »

If fhere are versions where the viewcursor returns null for the textfield - then I don't see how you can do anything.

If its just getting the anchor that fails, then you could enumerate all the paragraphs and text portions in the document till you find the matching textfield - that would be very slow and cumbersome.

A workaround might be to mark the anchor in some way so you can easily go to it - a bookmark or a unique number (like a footnote uses) which could be hidden text or non visible white text.

A document property could be used to store information pairs that match the anchor text or identifier to some textfield property identifying it.

Edit:

Or, most easily, you could revert to a version where there isn't this problem.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
RoryOF
Moderator
Posts: 34619
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Get all Annotations/Comments/Notes

Post by RoryOF »

I was fiddling around tonight (not very seriously, waiting for a large document to download over a very slow connection).
Andrew Pitonyak, in his personal macro libraries, at
https://www.pitonyak.org/AndrewPersonalLibs.odt
gives some macros for handling Annotations.

I have not inspected these in detail, and not yet tried them, but I think they may solve some of the Annotation problems Lupp and JeJe have mentioned. Also, possibly, the problem of a listing of Annotations starting with the last and then the Annotations in the document in sequence.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Get all Annotations/Comments/Notes

Post by Lupp »

To be clear: I didn't comment on JeJe's posts.
The ViewCursor wasn't a means for me to get a grip on any textfield. I went through all the textfields, jumped over different subtypes, and selected the ...textfield.Annotation objects to be able to get positioning information (page, position.Y, position.X) about the anchor with the help of the ViewCursor.
Who runs my demo with a not too buggy version (like LibO 7.5 e.g.) will soon see how it works.
V24.2 has the mentioned bug and can therefore not sort the results reasonably.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
wanglong
Posts: 51
Joined: Fri Sep 09, 2022 10:57 am
Location: Beijing,China

Re: Get all Annotations/Comments/Notes

Post by wanglong »

First, I bound a mouse click event to the document.

Then, in the mouse event methods, I performed the following operations.

Code: Select all

   public static class MouseClickHandler implements XMouseClickHandler {
        @Override
        public boolean mousePressed(MouseEvent mouseEvent) {
            return false;
        }

        @Override
        public boolean mouseReleased(MouseEvent mouseEvent) {
            logger.info("---------------------------------");
            XTextCursor xTextCursor =  compareRightXTextDocument.getText().createTextCursorByRange(compareRightTextViewCursor);
            XParagraphCursor xParagraphCursor = UnoRuntime.queryInterface(XParagraphCursor.class, xTextCursor);
            logger.info(xParagraphCursor.getString());

            try {
                logger.info("X:" + compareRightTextViewCursor.getPosition().X + ",Y:" + compareRightTextViewCursor.getPosition().Y);
                compareRightTextViewCursor.goLeft((short)1,false);
                compareRightTextViewCursor.setString("123");
            }catch (java.lang.Exception e)
            {
                logger.error(e.getMessage());
            }

            return false;
        }

        @Override
        public void disposing(EventObject eventObject) {

        }
    }
Afterwards, I clicked on the annotation area of the document, triggering a mouse event. In the mouse event, I was able to obtain an XTextViewCursor, but when I tried to perform the next operation (e.g., setString/goLeft), I received exception message “no text selection.”. Conversely, if I clicked on an area other than the annotation area to trigger the mouse event, I was able to obtain an XTextViewCursor and perform the next operation.

By printing the Position information of the XTextViewCursor, I found that the XTextViewCursors obtained from clicking on the annotation or its Anchor position have the same Position attribute (I don't know if this indicates that they are the same), but from the subsequent operations described above, I think they are different.

I don't know why this issue occurs. Please help me. :ucrazy:
Libre Office 7.6 on Windows 11.
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Get all Annotations/Comments/Notes

Post by JeJe »

You seem to keep changing what you're asking - or you're not being clear about what it is. Now you want to edit the contents of the textfield?
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply