[Libreoffice-bugs] [Bug 121536] Embedding external (links) images programmatically no longer works

bugzilla-daemon at bugs.documentfoundation.org bugzilla-daemon at bugs.documentfoundation.org
Fri Nov 23 04:45:29 UTC 2018


https://bugs.documentfoundation.org/show_bug.cgi?id=121536

--- Comment #2 from Simon <ikharus at hotmail.com> ---
Thanks a million Oliver. 

What I was trying to do is the opposite of your example, but it helped me a
lot! I don't like being force to check the product and version but it works.
Thanks again.

Here's my new function:

  private static void convertLinkedImagesToEmbeded(
      final XComponentContext context, final XComponent document) throws
Exception {

    // Create a GraphicProvider.
    final XGraphicProvider graphicProvider =
        UnoRuntime.queryInterface(
            XGraphicProvider.class,
            context
                .getServiceManager()
               
.createInstanceWithContext("com.sun.star.graphic.GraphicProvider", context));
    final XIndexAccess indexAccess =
        UnoRuntime.queryInterface(
            XIndexAccess.class,
            UnoRuntime.queryInterface(XTextGraphicObjectsSupplier.class,
document)
                .getGraphicObjects());
    for (int i = 0; i < indexAccess.getCount(); i++) {
      final Any xImageAny = (Any) indexAccess.getByIndex(i);
      final Object xImageObject = xImageAny.getObject();
      final XTextContent xImage = (XTextContent) xImageObject;
      final XServiceInfo xInfo = UnoRuntime.queryInterface(XServiceInfo.class,
xImage);
      if (xInfo.supportsService("com.sun.star.text.TextGraphicObject")) {
        final XPropertySet xPropSet =
UnoRuntime.queryInterface(XPropertySet.class, xImage);
        if (is LibreOffice and Version >= 6.1) { ***
          final XGraphic xGraphic =
              (XGraphic)
                  AnyConverter.toObject(XGraphic.class,
xPropSet.getPropertyValue("Graphic"));
          // Only ones that are not embedded
          final XPropertySet xGraphixPropSet =
UnoRuntime.queryInterface(XPropertySet.class, xGraphic);
          boolean linked = (boolean)
xGraphixPropSet.getPropertyValue("Linked");
          if (linked) {
            // Since 6.1, we must use "Graphic" instead of "GraphicURL"
            final PropertyValue[] props =
                new PropertyValue[] {new PropertyValue(), new PropertyValue()};
            props[0].Name = "URL";
            props[0].Value =
xGraphixPropSet.getPropertyValue("OriginURL").toString();
            props[1].Name = "LoadAsLink";
            props[1].Value = false;
            xPropSet.setPropertyValue("Graphic",
graphicProvider.queryGraphic(props));
          }
        } else {
          final String name =
xPropSet.getPropertyValue("LinkDisplayName").toString();
          final String graphicURL =
xPropSet.getPropertyValue("GraphicURL").toString();
          // Only ones that are not embedded
          if (graphicURL.indexOf("vnd.sun.") == -1) {
            // Creating bitmap container service
            final XNameContainer bitmapContainer =
                UnoRuntime.queryInterface(
                    XNameContainer.class,
                    UnoRuntime.queryInterface(XMultiServiceFactory.class,
document)
                        .createInstance("com.sun.star.drawing.BitmapTable"));
            if (!bitmapContainer.hasByName(name)) {
              bitmapContainer.insertByName(name, graphicURL);
              xPropSet.setPropertyValue("GraphicURL",
bitmapContainer.getByName(name).toString());
            }
          }
        }
      }
    }
  }

*** I didn't show how to check the product and version, the function would have
been too long, but I've been inspired by the Utilities made by Dr. Andrew
Davison that can be found here:
http://fivedots.coe.psu.ac.th/~ad/jlop

-- 
You are receiving this mail because:
You are the assignee for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/libreoffice-bugs/attachments/20181123/d49dcdff/attachment.html>


More information about the Libreoffice-bugs mailing list