Page 1 of 1

Get all Annotations/Comments/Notes

Posted: Fri Mar 08, 2024 12:19 pm
by wanglong
How to get all Annotations in XTextDocument?

Re: Get all Annotations/Comments/Notes

Posted: Fri Mar 08, 2024 1:36 pm
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

Re: Get all Annotations/Comments/Notes

Posted: Fri Mar 08, 2024 10:19 pm
by JeJe
The annotations will support the com.sun.star.text.textfield.Annotation service.

https://www.openoffice.org/api/docs/com ... le-ix.html

Re: Get all Annotations/Comments/Notes

Posted: Sat Mar 09, 2024 8:40 am
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 7848 times

Friendly, J.P

Re: Get all Annotations/Comments/Notes

Posted: Sun Mar 10, 2024 7:50 pm
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 136 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.

Re: Get all Annotations/Comments/Notes

Posted: Mon Mar 11, 2024 7:51 am
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...

Re: Get all Annotations/Comments/Notes

Posted: Mon Mar 11, 2024 11:09 am
by JeJe

Code: Select all

with thiscomponent.currentcontroller
.select .viewcursor.textfield.anchor
end with

Re: Get all Annotations/Comments/Notes

Posted: Mon Mar 11, 2024 11:20 am
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.

Re: Get all Annotations/Comments/Notes

Posted: Mon Mar 11, 2024 11:28 am
by wanglong

Code: Select all

XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor();
XTextField xTextField = UnoRuntime.queryInterface(XTextField.class, xViewCursor);
I get xTextField is equals to "null".

Re: Get all Annotations/Comments/Notes

Posted: Mon Mar 11, 2024 12:43 pm
by JeJe
If the viewcursor isn't in a textfield you will get null.

Re: Get all Annotations/Comments/Notes

Posted: Mon Mar 11, 2024 1:20 pm
by wanglong
I has comfirmed that the viewCursor is in the annotation area,but I still get "null" return. :crazy:

Re: Get all Annotations/Comments/Notes

Posted: Mon Mar 11, 2024 1:38 pm
by JeJe
My code selects the anchor fine for me in LO as well as OO.

Re: Get all Annotations/Comments/Notes

Posted: Mon Mar 11, 2024 10:35 pm
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 129 times
obsolete:
annotationExperiments.odt
(34.95 KiB) Downloaded 137 times

Re: Get all Annotations/Comments/Notes

Posted: Mon Mar 11, 2024 11:15 pm
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.

Re: Get all Annotations/Comments/Notes

Posted: Mon Mar 11, 2024 11:27 pm
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.

Re: Get all Annotations/Comments/Notes

Posted: Mon Mar 11, 2024 11:28 pm
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.

Re: Get all Annotations/Comments/Notes

Posted: Tue Mar 12, 2024 10:20 am
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:

Re: Get all Annotations/Comments/Notes

Posted: Tue Mar 12, 2024 1:20 pm
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?