How to replace ole with images?

Hello!

We have inserted some ole objects and InlineLvlSdt as placeholders in the onlyoffice Text Document. We hope to find these ole objects and InlineLvlSdt later and replace them with what we want.

So I would like to ask:

  1. After getting all ole objects, how to use the API to replace the ole object with an image at the location of the ole object?

  2. After getting all ole objects, how to use the API to replace the ole object with a table at the location of the ole object?

  3. After getting all InlineLvlSdt, how to use the API to replace the location of the InlineLvlSdt with ordinary text.

Looking forward to your reply, thank you!

Hello @szmtcjm

I don’t think that OLE objects are good solution for that. Do I understand correctly that you are developing custom plugin to replace placeholders in the template document with proper text/images? For instance, the template contains regular text that always remains unchanged and specific areas, i.e. placeholders, for text and images that must be replaced with specific data.

Yes, you understand it correctly. I choose ole object because ole object can use data to store some information, store the relationship between placeholder and replacement content. At the same time, ole object can replace preview image when making template, because preview image needs to be replaced frequently during the production process. So what do you recommend to use as placeholder? What API is used to complete the above operations?

I think you should only rely on Content Controls in such scenario.

For instance, I have a document with several Content Controls with tag Name and another with tag Image and I want to replace them, I’d go with such approach for the plugin with callCommand:

(function(window, undefined){
    window.Asc.plugin.init = function()
    {
      this.callCommand(function() {
        // Replace text
        var oDocument = Api.GetDocument();
        var aContentControls = oDocument.GetContentControlsByTag("Name");
        aContentControls.forEach(function(cc, index) {
          cc.RemoveElement(0);
          cc.AddText("John Doe");
          console.log("Name inserted");
        });
        // Replace image
        var oDocument = Api.GetDocument();
        var aContentControls = oDocument.GetContentControlsByTag("Image");
        var oRun = Api.CreateRun();
        var oDrawing = Api.CreateImage("https://legacy-api.onlyoffice.com/content/img/docbuilder/examples/coordinate_aspects.png", 60 * 36000, 35 * 36000);
        oRun.AddDrawing(oDrawing);
        aContentControls.forEach(function(cc, index) {
          cc.RemoveElement(0);
          cc.AddElement(oRun, 0);
          console.log("Image inserted");
        });
      }, true);
    },

Basically, I am getting all Content Controls with GetContentControlsByTag, removing simple placeholder with RemoveElement text and placing necessary item in there with AddText for simple text or AddElement for image (actually all other objects can be added with Run and this method).

Image is created with some adjacent methods here and there, but I think you got the idea. For each of your tags you can set specific data to insert - generally, tags are used to distinguish your placeholder to define which data must be inserted and where. Of course, you can re-use these tags to provide new data for replacement as long as the tag remains unchanged.

Results:

  1. Before replacement:
    before

  2. After replacement:

Note: I am using only ApiInlineLvlSdt for that as you can insert it right into the paragraph. Another type of CC represents whole paragraph instead.

I hope it helps.

@Constantine Thank you for your reply. The operation of replacing the placeholder is in the backend, so it cannot be done in the form of a plug-in, and the plug-in API cannot be used. I understand that it needs to be done using OnlyOffice Builder. If it is to be done using Builder, how do I write the Builder script?

I’m quite confused - are you willing to use Document Builder to replace placeholders and generate the resulting document?