Skip to main content

Creating a DOCX document by using a template


The following example describes how to create a processing package that will generate a WORD document based on a previously created template and data loaded from a CSV file.

image

Import documents​

  1. Go to archITekt Studio and load the previously prepared document templates along with the document data structure.

image

  1. View in the version server after file import.

image

Loading a CSV file with address data​

  1. Select the File menu, then New>processing package.
  2. The package editor and the Toolbox tab will be displayed, along with blocks for creating a graphical representation of the package.
  3. Drag the block CSV Input File onto the editor.
  4. Go to the details of the block - double-click on the block or go to the Details tab.
  5. Load the file and select Yes in the window that appears. It is important to select the correct encoding.

image

  1. Next, you need to specify the separator used in the CSV file (in this case, ;) and, in turn, the data structure based on a row from the file (in this case, row number 1).
  2. Go to the Table tab, select the row with the header and select the Create a data structure based on the selected button.
  3. On the Schemas panel, you can see what the resulting schema looks like after loading the data from the CSV file.

image

  1. Add the Browse Data block (the block is located in the Design Tools category) and connect it to the CSV Input File block. This is an optional action to check the result of loading the file.
  2. Run the package by pressing F5 or the Run option from the top menu.
  3. After running the package, you can preview the execution results by opening the editor for the Browse Data block.

image

Document generation​

  1. Add a Document generation block.
  2. If it has not been designated automatically then designate the root of the input data, which is the record in the data structure that will contain the source data.
  3. Go into the details of the block and select the record and then select the Select data root button highlighted in yellow.
  4. At this point, an additional structure responsible for storing the information needed when creating documents appears.

image

  1. Go to the Properties of the block and select the template from the version server.

image

  1. The structure retrieved from the template appears in the data schema.

image

  1. Next, merge the empty structure from the template file with the data structure from the CSV file.
  2. Drag a field in the schema from the file to the corresponding field from the template and select Set as the value source from the context menu.

image

  1. When processing starts, the data is saved in document_output\content This can be checked by adding the Broese Data block.

image

Saving the document​

  1. To save the generated files to disk, use the Run Code block (saving the generated documents to disk is not a standard action, usually the results of document generation are saved to the binary fields of the database).
  2. Drag the block and go to its details.
  3. Use the WriteFIle() function and indicate the path and name of the document, as well as the format and binary data of the document:

WriteFile(" C:VSOFT\Document "+" . "+ document_output\format , document_output\content )

image

Creating a document using data retrieved from a database​

In a similar way, you can build a processing package in which the data for the document is fed with data from a database.

  1. In this example, on the document template, we want to display a report on a client with a list of applications assigned to it.

image

  1. There is a CLIENT table defined in the database and a APPLICATIONS table that has a relationship to the client data.

image

  1. Create a processing package.
  2. To retrieve data from the database, drag the Database Reader from database block to the workspace.
  3. Go to the Details tab of the block and enter an SQL query to retrieve clinet data from the CLIENT table.

image

  1. Drag the Loop Join block onto the workspace and connect it to the client database reading block.
  2. The Loop Join block allows you to attach the results of a query from a secondary source to records from the primary source. In this case, we want to append a client's applications from the APPLICATIONS table to the client's data.
  3. The window Input data - from the 'Input' port shows a preview view of the schema of input data elements coming from the first source. The window next to it allows you to present a query to the port 'QueryInput' - data from an additional source. Only data that meets the set requirements will be passed through.
  4. Go to the details of the block and move the C_ID field to the right side to the output port.

image

  1. Drag another Database Reader block to retrieve the application data.
  2. Go to the details of the Database Reader - Applications block and in the SQL query enter the condition after which the data should be merged with the first data source. We want to display the applications of a particular client in the document. In this case, the data from the table CLIENT and APPLICATIONS are related to each other by a relationship.
  3. Drag the appropriate fields into the query or type: SELECT * FROM dbo.APPLICATIONS where A_CLIENT_ID= #(C_ID)#

image

  1. Then add the Document generation block and indicate the template on the Properties panel and the Run Code block and define where to save the document. Analogous to the previous example.
  2. hen defining the data source for the fields from the document, drag the fields from the data structure to the fields from the document as in the previous case. Fields from CLIENT database (record Database Reader- Client) to fields from the document structure - Client .
  3. Select Set as the value source from the context menu.

image

  1. The record Database Reader - Applications is a multiple record and corresponds to data from the document from the Applications table.
  2. Here you should remember to map the data to the correct record. This depends on how the fields from the document data structure were inserted into the template. In this case, a relationship was created in the document from the Client table to the RelApplications table and along this path the fields in the tags were inserted into the document.
  3. In this case, in order to map the data correctly, move the Database Reader - Applications record to the RelApplications record and select Clone.

image

  1. Then map the cloned data to the fields from the document and select Set as the value source from the context menu.

image

Save document to database​

  1. Add a Database Writer block (Output blocks category).
  2. On the root record, start typing Client and choose which database to save the data to, i.e. dbo.CLIENT

image

  1. Mark the Client record with the Write record to a database annotation (from the context menu, select Change/use record). After performing the action, new fields should appear on the tree suggesting what data should be in the database.

image

  1. On the annotation on the database record, select the action type as Auto_InsertUpdate.

image

  1. Mark the C_ID field with the Primary key annotation.
  2. From the document_output record, drag the binary content field to a field from the C_DOCUMENT client structure and select Set as the value source from the context menu.
  3. On the C_DOCUMENT_NAME field of the file, you can use the Constant annotation and enter how the document will be named.

image

See also​

Integration with Active Directory

Blocks