Read documents contents on save

I am in the process of writing a node based document server. While writing the function to save the updated document from the document editor, I am wanting to read the contents of the document being saved to find [Example.token] tokens in the document to be saved in the database along with the record of the document. Later the document will be copied, and the tokens replaced with actual data. Is this something that the document API can be used for?

At this point in the code I have a url for the document to be saved, or I can directly reference the file where it is saved.

1 Like

Hi @Brack :wave:
Let’s see if I can find something to help you.

Hey, @Brack

I can confirm that we don’t allow interference with the save process.
For your task, you might be able to use the Builder’s features to replace content in the resulting file using tokens or fields.

It would be helpful to understand the main task you are trying to accomplish.

I think I may have had a misunderstanding about the automation API. I was thinking it allowed for the reading/editing of a closed document that is sitting somewhere on a server, it instead allows for access to the editor’s contents when the document is open, just outside the actual editor on the front end. So to read the contents of a document, it needs to be open in the editor. I could then have the front end read through the contents, find the tokens, inform the server of what tokens the document has.

Then later when the template is ready to be used, I can use the document builder to create a script that modifies the existing template to replace the tokens with the actual data.

The main flow I am trying to accomplish is that in one part of the application a user uploads/creates a template that will be populated with some number of tokens. In another part of the application a user selects a template, and that template is copied, the tokens replaced with the relevant user data, and saved under the other user’s account as a document.

I had been thinking I could do all the scanning for tokens on the server side when the document is saved, but it appears that OnlyOffice doesn’t have tools for reading the contents of a document server side

What I have since tried doing is to use the Automation API on the frontend to try and read the contents of the document. I’ve tried executing the ConvertDocument method, which works mostly, except that it misses all the text in text boxes.

connector.executeMethod(“ConvertDocument”, [“markdown”, false, false, true, false], function (data) {

@Brack :handshake:

If my understanding is correct, what you need is essentially change some content within a document with some other content.
For this, you can use these plugins:

However, if you need to have more control over the data, you can use content controls, with the following plugin examples given here:

In case you have a valid license with AutomationApi, please let us know, since this system is for community users only, licensed users are eligible to receive support in our separate ticketing system.