[PROVENANCE INTERNET] RE: need help to insert an image with a caption with the Libo java API <text:sequence-ref instead of <text:sequence

LORENZO Vincent vincent.lorenzo at cea.fr
Mon Oct 7 14:59:43 UTC 2019


Hello everybody,
              Here my java code to get numbered caption under the inserted image (with some dependencies on my API, sorry for that)
Hoping it will help someone in the future. I still thank you for the help.
FYI, this code will be available in few days (or week) on this git repository : https://git.eclipse.org/c/papyrus/org.eclipse.papyrus-model2doc.git/


      public static void addImageLink(String imageFilePath, String caption, XTextCursor xTextCursor, ODTEditor odtEditor) {
            try {
                   XMultiServiceFactory xMultiServiceFactory = odtEditor.getXMultiServiceFactory();

                   Object tmp = xMultiServiceFactory.createInstance("com.sun.star.text.TextFrame"); //$NON-NLS-1$
                   XTextFrame textFrame = UnoRuntime.queryInterface(XTextFrame.class, tmp);
                   XPropertySet framePropertySet = createXPropertySet(textFrame);
                   framePropertySet.setPropertyValue("SizeType", SizeType.VARIABLE); //$NON-NLS-1$
                   framePropertySet.setPropertyValue("AnchorType", TextContentAnchorType.AS_CHARACTER); //$NON-NLS-1$
                   framePropertySet.setPropertyValue("ZOrder", 1);// not really sure //$NON-NLS-1$
                   framePropertySet.setPropertyValue("TextWrap", WrapTextMode.THROUGH); //$NON-NLS-1$

                   // Creating the service GraphicObject
                   Object graphicObject = xMultiServiceFactory.createInstance("com.sun.star.text.TextGraphicObject"); //$NON-NLS-1$

                   // Creating TextContent for GraphicObject
                   XTextContent graphicContent = UnoRuntime.queryInterface(XTextContent.class, graphicObject);

                   // Creating bitmap container service
                   XNameContainer bitmapContainer = UnoRuntime.queryInterface(XNameContainer.class, xMultiServiceFactory.createInstance("com.sun.star.drawing.BitmapTable")); //$NON-NLS-1$

                   // Inserting image to the container
                   bitmapContainer.insertByName(imageFilePath, imageFilePath);

                   PropertySetUtil.setProperty(graphicContent, "AnchorType", TextContentAnchorType.AT_CHARACTER); //$NON-NLS-1$
                   PropertySetUtil.setProperty(graphicContent, "GraphicURL", bitmapContainer.getByName(imageFilePath)); //$NON-NLS-1$

                   graphicContent = ImageUtil.resizeImage(graphicContent, imageFilePath, odtEditor.getXTextDocument(), odtEditor.getXMultiComponentFactory(), odtEditor.getXComponentContext());

                   XPropertySet graphicPropSet = createXPropertySet(graphicContent);
                   Object heightValue = graphicPropSet.getPropertyValue("Height"); //$NON-NLS-1$
                   Object widthValue = graphicPropSet.getPropertyValue("Width"); //$NON-NLS-1$
                   XPropertySet textFrameSet = createXPropertySet(textFrame);
                   textFrameSet.setPropertyValue("Height", heightValue);// TODO don't work, and should be on the next level... //$NON-NLS-1$
                   textFrameSet.setPropertyValue("Width", widthValue); //$NON-NLS-1$

                xTextCursor.getText().insertTextContent(xTextCursor, textFrame, false);
                   XTextCursor localCursor = textFrame.getText().createTextCursor();

                   XParagraphCursor paragraphCursor = UnoRuntime.queryInterface(XParagraphCursor.class, localCursor);
                   XPropertySet paraSet = createXPropertySet(paragraphCursor);
                   paraSet.setPropertyValue("ParaStyleName", "Figure");
                localCursor.getText().insertTextContent(localCursor, graphicContent, false);
                   localCursor.gotoEnd(true);


                   // managing the caption, we need a text field master
                   XTextFieldsSupplier supplier = UnoRuntime.queryInterface(XTextFieldsSupplier.class, odtEditor.getXTextDocument());
                   XNameAccess masters = supplier.getTextFieldMasters();
                   Object masterSetExpressionForFigure = masters.getByName("com.sun.star.text.fieldmaster.SetExpression.Figure"); //$NON-NLS-1$

                   // create a dependent SetExpression
                   Object textFieldSetExpression = xMultiServiceFactory.createInstance("com.sun.star.text.textfield.SetExpression"); //$NON-NLS-1$
                   XPropertySet textFieldSetExpressionPropertySet = UnoRuntime.queryInterface(XPropertySet.class, textFieldSetExpression);
             textFieldSetExpressionPropertySet.setPropertyValue("NumberingType", com.sun.star.style.NumberingType.ARABIC); //$NON-NLS-1$
             textFieldSetExpressionPropertySet.setPropertyValue("NumberFormat", NumberFormat.NUMBER); //$NON-NLS-1$
             textFieldSetExpressionPropertySet.setPropertyValue("Content", "Figure + 1"); //$NON-NLS-1$ //$NON-NLS-2$
                   XDependentTextField xDependeantTextFIeld = UnoRuntime.queryInterface(XDependentTextField.class, textFieldSetExpression);

                   // attach the dependent SetExpression to its master
             xDependeantTextFIeld.attachTextFieldMaster(UnoRuntime.queryInterface(XPropertySet.class, masterSetExpressionForFigure));

                   localCursor.getText().insertString(localCursor, "Figure ", false); //$NON-NLS-1$

                   // insert the SetExpression
                localCursor.getText().insertTextContent(localCursor, xDependeantTextFIeld, true);


                   if (caption != null && !caption.isEmpty()) {
                         localCursor.getText().insertString(localCursor, ": " + caption, false); //$NON-NLS-1$
                   }

                   endParagraph(xTextCursor);
             } catch (Exception e) {
                   Activator.log.error(e);
             }
      }

Best regards,
--
Vincent Lorenzo

De : LibreOffice <libreoffice-bounces at lists.freedesktop.org> De la part de LORENZO Vincent
Envoyé : lundi 16 septembre 2019 11:10
À : Andrew Pitonyak <andrew at pitonyak.org>
Cc : libreoffice at lists.freedesktop.org; Miklos Vajna <vmiklos at collabora.com>; sos <sos at pmg.be>; Kaganski Mike <mikekaganski at hotmail.com>
Objet : [PROVENANCE INTERNET] RE: need help to insert an image with a caption with the Libo java API <text:sequence-ref instead of <text:sequence

Hi Andrew,
    Thank you very much, I succeded to make a caption and a table of figures last Friday !
I will post the final solution on this thread when I will have finished to clean my code.

Best Regards,
--
Vincent Lorenzo


De : Andrew Pitonyak <andrew at pitonyak.org<mailto:andrew at pitonyak.org>>
Envoyé : vendredi 6 septembre 2019 18:06
À : LORENZO Vincent <vincent.lorenzo at cea.fr<mailto:vincent.lorenzo at cea.fr>>
Cc : Kaganski Mike <mikekaganski at hotmail.com<mailto:mikekaganski at hotmail.com>>; Miklos Vajna <vmiklos at collabora.com<mailto:vmiklos at collabora.com>>; sos <sos at pmg.be<mailto:sos at pmg.be>>; libreoffice at lists.freedesktop.org<mailto:libreoffice at lists.freedesktop.org>
Objet : RE: need help to insert an image with a caption with the Libo java API <text:sequence-ref instead of <text:sequence

I am going from memory here, so I might be a bit off.

When you create a variable such as "Figure", it is of a specific type because they know how you want to use it. If I insert a caption manually, something like the following happens.

I enter the text "Figure " at the start of a paragraph and then I insert a field; one of those Figure fields (The type is SetExpression). I then set the value to be "Figure + 1". It turns out that we have a single MASTER for ALL of the Figure fields that you see. They are ALL a dependent, child, or what ever they call it, of this one master text field. In OOME.odt (on my web site), I have examples in Basic to create and manipulate these, but it is missing some details I think.

I thought that you wanted to know how to insert a reference to a specific SetExpression field, which is the part that says something like "Figure 1. ...". I was wrong. So, I assume that you want to insert one of those SetExpression fields (A caption field).

Lets consider what happens when I insert a caption using the UI. Note that I don't really know what it does, but, I am guessing and going from memory. So, some of my details may be wrong.

I paste a graphic into my document. By default, I think that it anchors itself to a paragraph and it can float a bit, but that is not very important for our purposes. This is my best guess as to what happens when you right click and say create caption.

1. A frame is created that uses the same anchor as the graphic.
2. The graphic is anchored as a character to the text object in the frame as a character.
3. The text "Figure " is inserted followed by a SetExpression type field with the expression "Figure + 1" followed by the rest of your text.

So, I think that by default there is a single paragraph in that text frame. I could not tell you how things are sized because I have never looked into it, but, that is how I think it is done. What ever they do, if you resize the frame, the image resizes with it. In other words, i think that they probably set some specific properties on the image and on the frame for that magic to occur.

Let me look again at what you say you want to do..

You insert an image and you know how to do that. Good.

I think you then say that you do not know how to create a caption for that image, but you use the term "Table Of Figure" so I think of creating a list of references to figures, which would really reference those SetExpression fields (I think that text field type is called a GetExpression). So I think you want to know how to insert a SetExpression field.

Also not that you are referencing the text field, which is associated to a graphic (or what ever) based on proximity (it is close).

The first question you must ask.... Does your Master text field already exist? You can create your own if you want, so, you could four different types of figures with their own Master field.

OOME (OpenOffice Macros Explained) demonstrates how to create a Master Field if you need to. You are probably using one that already exists, however. The example below creates one named "Listing".

A Listing sequence field is associated to the com.sun.star.text.FieldMaster.SetExpression.Listing master field, which must exist before the sequence field can be used. Listing 396 Demonstrates how to create a master field if it does not exist, and how to get the master field if it does.
Listing 396. Creating a master field.
oMasters = oDoc.getTextFieldMasters
If NOT oMasters.hasByName("com.sun.star.text.FieldMaster.SetExpression.Listing") Then
  oMasterField = oDoc.createInstance("com.sun.star.text.FieldMaster.SetExpression")
  oMasterField.Name = "Listing"
  oMasterField.SubType = com.sun.star.text.SetVariableType.SEQUENCE
Else
  oMasterField = oMasters.getByName("com.sun.star.text.FieldMaster.SetExpression.Listing")
End If
The master field sub-type determines the field type (see Table 154).
Table 154. Constants for com.sun.star.text.SetVariableType.
Value

Constant

Description

0

com.sun.star.text.SetVariableType.VAR

Simple variable.

1

com.sun.star.text.SetVariableType.SEQUENCE

Number sequence field.

2

com.sun.star.text.SetVariableType.FORMULA

Formula field.

3

com.sun.star.text.SetVariableType.STRING

String field.


This is the next section:

The sequence field is created by the document, then the numbering type is set to arabic. The number format is set, the master field is associated to the field (the master field is a sequence field), and then the field content is set to increment the value (Listing + 1). Note that when the field is inserted, the field may not be immediately updated. Either wait for the field to update, or force an update using Tools > Update > Fields.
Listing 397. Creating a set expression sequence field.
oField = oDoc.createInstance("com.sun.star.text.TextField.SetExpression")
oField.NumberingType = com.sun.star.style.NumberingType.ARABIC
oField.NumberFormat = FindCreateNumberFormatStyle("###0", oDoc)
oField.attachTextFieldMaster(oMasterField)
oField.Content = name & " + 1"
Constants used to set the numbering type are set as shown in the Table 150.

Things to note.

1. The document creates field that you want to insert.
2. The SetExpression field itself is directly attached to the associated Master field (attachTextFieldMaster).
3. I actually create my own numbering style, I don't know what the default is or what happens if you do not set the style.  I just don't remember.
4. This little snippet does not actually show you inserting the field. You do this by inserting text content with a text cursor.
5. You will not see anything displayed, at least not correctly, until after you update fields from the menu. In the example I posted, i used a dispatch to do this for you.

There is a full example that replaces text references for figures with actual fields such as this. I had a document with a bunch of manually entered values and I wanted to use fields. It was created before I knew how to do that using the UI.

So, I hope this helps some.

I am overdue for pushing a new copy of OOME4.odt; I made some changes in other areas, I just need to push it.



On Friday, September 06, 2019 09:51 EDT, LORENZO Vincent <vincent.lorenzo at cea.fr<mailto:vincent.lorenzo at cea.fr>> wrote:


Hi Andrew,
   Thank you for your answer. In fact I want to create a numbered caption (something like « Figure 1 : my nice caption ») under an inserted image and I want to create a table of figure with these captions to list all images.

-         The image is well inserted

-         The Table Of Figure is created and works fine when I add manually an image with a caption in the generated document.

-         This table of Figure doesn’t work for the image inserted programmatically, probably, because I don’t know how to create properly a numbered caption.

I’m a bit confused about the SetExpression, DependentField, field master, … I will continue to investigate about that, but if you have some references for me, please don’t hesitate.

Regards,
/Vincent Lorenzo

De : Andrew Pitonyak <andrew at pitonyak.org<mailto:andrew at pitonyak.org>>
Envoyé : jeudi 5 septembre 2019 22:57
À : LORENZO Vincent <vincent.lorenzo at cea.fr<mailto:vincent.lorenzo at cea.fr>>
Cc : Kaganski Mike <mikekaganski at hotmail.com<mailto:mikekaganski at hotmail.com>>; Miklos Vajna <vmiklos at collabora.com<mailto:vmiklos at collabora.com>>; sos <sos at pmg.be<mailto:sos at pmg.be>>; libreoffice at lists.freedesktop.org<mailto:libreoffice at lists.freedesktop.org>
Objet : RE: need help to insert an image with a caption with the Libo java API <text:sequence-ref instead of <text:sequence

Are you looking to insert the caption, or the reference to the caption? I think you want to insert a reference to a dependent field for a text field master. Does this sound correct?

The text field master, which is of type:

com.sun.star.text.fieldmaster.SetExpression.Figure

But you want to reference a Dependent field. If my guess is correct, this is how I insert a reference to the first Figure using Basic. Note that there is almost no error checking, so, this could go very wrong.

Sub Main
  Dim oField
  Dim oNewField
  Dim oText
  Dim oCurs
  Dim oReferencedField
  Dim oEnum
  Dim oDispatcher      ' Dispatcher object.
  Dim oFrame

 ' Setup to perform a dispatch.
  oFrame = ThisComponent.CurrentController.Frame
  oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

  oText = ThisComponent.getText()
  oCurs = ThisComponent.CurrentController.getViewCursor()

    oEnum = ThisComponent.getTextFields().createEnumeration()
    If Not IsNull(oEnum) Then
    Do While oEnum.hasMoreElements()


      oField = oEnum.nextElement()
      'Inspect oField
      If oField.supportsService("com.sun.star.text.TextField.SetExpression") Then
        If oField.VariableName = "Figure" Then
          Print "Found one"
          Exit Do
        End If
      End If
    Loop
  End If

  ' No error checking, I just assume that I found one. DANGER DANGER DANGER


  oNewField = ThisComponent.createInstance("com.sun.star.text.textfield.GetReference")
  oNewField.ReferenceFieldPart = com.sun.star.text.ReferenceFieldPart.CATEGORY_AND_NUMBER
  oNewField.ReferenceFieldSource = com.sun.star.text.ReferenceFieldSource.SEQUENCE_FIELD
  oNewField.SequenceNumber = oField.SequenceValue
  oNewField.SourceName = "Figure"
  oText.insertTextContent(oCurs, oNewField, True)

  ' This next part will NOT work until after all of the fields have updated.
  ' So, force an update.
  oDispatcher.executeDispatch(oFrame, ".uno:UpdateFields", "", 0, Array())
End Sub


On Thursday, September 05, 2019 10:59 EDT, LORENZO Vincent <vincent.lorenzo at cea.fr<mailto:vincent.lorenzo at cea.fr>> wrote:

Hello everybody,
Since my last question, I switched task. Now I come back to my LibreOffice task to resolve my caption problem.
I'm still blocked on this problem. I get a different result compared to my last email, but the reference for the caption is still not found.
I replace Illustration by Figure, but the problem keeps the same.

I obtain a text:sequence-ref instead of a text:sequence. Does someone know why or which is the API to use to generate a text:sequence...

My result for the numbering of the caption:
<text:sequence-ref text:reference-format="value" text:ref-name="refFigure0">Erreur : source de la référence non trouvée</text:sequence-ref> (the french text said "reference not found")

I would like to get :
<text:sequence text:ref-name="refFigure0" text:name="Figure" text:formula="ooow:Figure+1" style:num-format="1">1</text:sequence>

My java code for that :

Object oReference = xMultiServiceFactory.createInstance("com.sun.star.text.textfield.GetReference");
XPropertySet oReferencePropertySet = UnoRuntime.queryInterface(XPropertySet.class, oReference);
oReferencePropertySet.setPropertyValue("SourceName", "Figure");// I get <text:sequence-ref
oReferencePropertySet.setPropertyValue("ReferenceFieldPart", ReferenceFieldPart.ONLY_SEQUENCE_NUMBER);
oReferencePropertySet.setPropertyValue("ReferenceFieldSource", ReferenceFieldSource.SEQUENCE_FIELD);
oReferencePropertySet.setPropertyValue("SequenceNumber", 0);// (short) 2);//not requireed ?
XTextContent xRef = UnoRuntime.queryInterface(XTextContent.class, oReference);
localCursor.getText().insertTextContent(localCursor, xRef, false);

Regards,
--
Vincent Lorenzo


-----Message d'origine-----
De : LibreOffice <libreoffice-bounces at lists.freedesktop.org<mailto:libreoffice-bounces at lists.freedesktop.org>> De la part de LORENZO Vincent
Envoyé : vendredi 15 février 2019 14:37
À : Kaganski Mike <mikekaganski at hotmail.com<mailto:mikekaganski at hotmail.com>>; Miklos Vajna <vmiklos at collabora.com<mailto:vmiklos at collabora.com>>; sos <sos at pmg.be<mailto:sos at pmg.be>>
Cc : libreoffice at lists.freedesktop.org<mailto:libreoffice at lists.freedesktop.org>
Objet : [PROVENANCE INTERNET] RE: need help to insert an image with a caption with the Libo java API

Hello everybody,
I thank you for your answers. Yes I want to add an image with a caption programmatically and I want a caption like this "Illustration 1 : My caption"
To my mind, it would be better/cleaner if I was able to generate the same thing than I do the UI. Thanks to you, I succeed to build the expected structure for the image and to set a text below it, but I still have some problems, mainly with the GetReference.

The GetReference generates :
<text:reference-ref text:reference-format="text" text:ref-name="refIllustration0">Erreur : source de la référence non trouvée</text:reference-ref> (sorry my Libo is in French, it is written "Sorry : source of the reference not found").

And I would like to get a text:sequence instead of a text:reference, to get this result:
<text:sequence text:ref-name="refIllustration0" text:name="Illustration" text:formula="ooow:Illustration+1" style:num-format="1">1</text:sequence>

Please, do you know how I can build a text:sequence ?

Please found in attachment the java code I wrote [1], the expect xml structure [2] and the current xml structure [3].
I know I have some difference between [2] and [3] with the width/height properties and inside the draw frame, but it is not the subject of this thread.

[1] insertImage.java
[2] expectedResult.xml
[3] currentResult.xml

In addition, I have more generic question, do you know if it exists a documentation, indicating mapping, between the xml elements and how to get/create them from java ? For example, in xml svg:width is called Height in the java API, and my Textframe becomes <draw:text-box...<

Best regards,
--
Vincent Lorenzo









-----Message d'origine-----
De : Kaganski Mike [mailto:mikekaganski at hotmail.com]
Envoyé : mercredi 13 février 2019 11:42
À : Miklos Vajna <vmiklos at collabora.com<mailto:vmiklos at collabora.com>>; LORENZO Vincent <vincent.lorenzo at cea.fr<mailto:vincent.lorenzo at cea.fr>>
Cc : libreoffice at lists.freedesktop.org<mailto:libreoffice at lists.freedesktop.org>
Objet : Re: need help to insert an image with a caption with the Libo java API

On 13.02.2019 12:45, Miklos Vajna via LibreOffice wrote:
> Hi,
>
> On Tue, Feb 12, 2019 at 09:27:15AM +0000, LORENZO Vincent <vincent.lorenzo at cea.fr<mailto:vincent.lorenzo at cea.fr>> wrote:
>> I would like to add a caption, to an inserted image in a text
>> document, but I don't find how to do that... Please do you have
>> pointer/documentation for me ?
>
> I think captions are just a UI feature. The doc model just stores a
> text frame around the image and the image is followed by the caption itself.
>
> (I.e. later it's not possible to reliably detect if some content
> around an image in a frame was created by hand or using the captions
> UI code.)
>
> So you can do the same "manually" using the UNO API. When in doubt,
> see what UNO API the ODT import uses to create the doc model based on
> the input markup.

And actually, if an image is not intended to be floating, but (as seen in majority of cases) is a part of normal text flow, then the frame could be not needed at all - simply add an image anchored as character, then add a paragraph with required style and numbering range field, then continue with other paragraphs. This makes the document structure clearer. Just an advise based on own experience.

--
Best regards,
Mike Kaganski
_______________________________________________
LibreOffice mailing list
LibreOffice at lists.freedesktop.org<mailto:LibreOffice at lists.freedesktop.org>
https://lists.freedesktop.org/mailman/listinfo/libreoffice








-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/libreoffice/attachments/20191007/6641db2a/attachment.html>


More information about the LibreOffice mailing list