Convert images link to embed

From NOA Documentation Wiki

Jump to: navigation, search

Source: Markus Krüger (NOA developer)

The following code shows how to convert linked images (text graphic objects) into embedded ones:

  ...
  ITextDocument textDocument = ...;
  XTextGraphicObjectsSupplier graphicObjSupplier = (XTextGraphicObjectsSupplier) UnoRuntime.queryInterface(XTextGraphicObjectsSupplier.class,
      textDocument.getXTextDocument());
  XNameAccess nameAccess = graphicObjSupplier.getGraphicObjects();
  String[] names = nameAccess.getElementNames();
  for (int i = 0; i < names.length; i++) {
    Any xImageAny = (Any) nameAccess.getByName(names[i]);
    Object xImageObject = xImageAny.getObject();
    XTextContent xImage = (XTextContent) xImageObject;
    XServiceInfo xInfo = (XServiceInfo) UnoRuntime.queryInterface(XServiceInfo.class, xImage);
    if (xInfo.supportsService("com.sun.star.text.TextGraphicObject")) {
      XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
          xImage);
      String name = xPropSet.getPropertyValue("LinkDisplayName").toString();
      String graphicURL = xPropSet.getPropertyValue("GraphicURL").toString();
      //only ones that are not embedded
      if (graphicURL.indexOf("vnd.sun.") == -1) {
        XMultiServiceFactory multiServiceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class,
            textDocument.getXTextDocument());
        XNameContainer xBitmapContainer = (XNameContainer) UnoRuntime.queryInterface(XNameContainer.class,
            multiServiceFactory.createInstance("com.sun.star.drawing.BitmapTable"));
        if (!xBitmapContainer.hasByName(name)) {
          xBitmapContainer.insertByName(name, graphicURL);
          String newGraphicURL = xBitmapContainer.getByName(name).toString();
          xPropSet.setPropertyValue("GraphicURL", newGraphicURL);
        }
      }
    }
  }

This is not NOA, but may become part of it.

Personal tools