unoil API:如何为插入的图像添加标题?

问题描述 投票:0回答:1

我:尝试以编程方式向图像添加标题,但没有成功...我希望获得与使用UI相同的结果,也就是说,此结果:

`<text:p text:style-name="Standard">
                <draw:frame draw:style-name="fr1" draw:name="Cadre1"
                    text:anchor-type="char" svg:width="16.432cm" draw:z-index="0">
                    <draw:text-box fo:min-height="14.841cm">
                        <text:p text:style-name="Illustration">
                            <draw:frame draw:style-name="fr2" draw:name="Image1"
                                text:anchor-type="as-char" svg:width="16.432cm"
                                style:rel-width="100%" svg:height="14.841cm"
                                style:rel-height="scale" draw:z-index="1">
                                <draw:image
                                    xlink:href="Pictures/100002010000026D000002310710B29486C9E685.png"
                                    xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"
                                    loext:mime-type="image/png" />
                            </draw:frame>
                            <text:span text:style-name="T1">
                                <text:line-break />
                            </text:span>
                            Illustration
                            <text:sequence text:ref-name="refIllustration0"
                                text:name="Illustration" text:formula="ooow:Illustration+1"
                                style:num-format="1">1</text:sequence>
                            : My caption
                        </text:p>
                    </draw:text-box>
                </draw:frame>
            </text:p>`

我的问题是如何获取xml元素'

<text:sequence ...> ?

我正在使用unoil-5.4.2(和其他Libo jar)。

java libreoffice caption
1个回答
0
投票

多亏了Libreoffice开发人员的邮件列表,我解决了我的问题。我需要一个textFieldMaster,然后创建一个SetExpression。我同意,理解起来并不明显...在这里,我发布了我的完整代码,其中包括带有图像的带有编号标题的完整代码:

    public static void addImageLink(String imageFilePath, String caption, XTextCursor xTextCursor, ODTEditor odtEditor) {
    if (false == imageFilePath.startsWith(odtFileIOService.getODTFilePrefix())) {
        imageFilePath = odtFileIOService.getODTFilePrefix() + imageFilePath;
    }
    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");// it works!!! in fact we can't push style which have not been declared and which don't exist by default //$NON-NLS-1$ //$NON-NLS-2$
        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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.