TTMSFNCWXDocx
The TTMSFNCWXDocx is a non-visual component that let’s you build and generate Docx files on the
fly. Almost everything that can be done in Word, can be used in this component. You can add
images, tables, bookmarks, links and even a table of contents. TTMSFNCWXDocx makes it easy to
build your documents. This component uses the Docx.js library.
Notice
Using TTMSFNCWXDocx requires an active internet connection to load the Dockx.js JavaScript library.
Building a document
Before building a document it’s important to understand the structure of the document and all the available building blocks to build your document.
Document
First you have the document level. At this level you can set the creator, description, title, … of the document you want to generate. This is the top level of the document. At this level you can add sections.
Sections
Sections are basically chapters. Every section you add, will start a new page on your document. Most of the time, you’ll want different sections for a cover sheet, table of contents and your main content. At this level, you can set per section the headers and footers, the page orientation, … in these sections you can tables, paragraphs and tables of content.
Section in landscape mode:
Paragraphs
Paragraphs are basically lines where you put content. Every paragraph starts on a new line. In Paragraphs you will have the possibility to add tables, text, images, bookmarks, hyperlinks, page references, line breaks… On this level you can set the heading style, set the bullet points, Alignment, …
Paragraph with text and image right aligned and heading2 style:
paragraph := section.AddParagraph;
paragraph.Heading := hlHeading2;
paragraph.Alignment := taRight;
paragraph.AddText('image');
paragraph.AddImage(TMSFNCBitmapContainer1.Bitmaps[0],500,500);
Tables
Tables have a collection of rows. These rows contain a collection of cells. A Table always must have rows and cells otherwise it won’t be added to the document. At this level you can set table width and the indenting.
Example of a table:
table := section.AddTable;
for i := 0 to 3 do
begin
tableRow := Table.AddRow;
for j := 0 to 3 do
begin
tableCell := tableRow.AddCell;
paragraph := TableCell.AddParagraph;
paragraph.AddText('row ' + IntToStr(i) + ' cell ' + IntToStr(j));
end;
end;
This code snippet generates following table:

Rows
At row level you can set the height, if the row is the header, if the rows can’t be split if this doesn’t
fit on the page anymore, … a Row must always have at least 1 cell.
Cells
At cell level you can set the borders, the cell width, the ColumnSpan/RowSpan, shading, … Here you
can add Paragraphs, or another Table. Cells do not need to be filled with children.
Text
Here you can add your text. You can set a ton of properties like font, color, size, underline, … If you add linebreaks inside text, these will just be filtered away and not shown. It’s better to add a new paragraph or add a new text and set the linebreak property on how many lines of whitespace you want.
Text Example:
text := paragraph.AddText('Hello world');
text.Font.Color := gcRed;
text.Font.Size := 50;
Text.Font.Name := 'arial';
Image
You can add an image to a paragraph. You can set the bitmap and it will render the image. It’s
important to set the transformation (height and width) to the desired values. These are by default
at 100. You can choose where you want to position the image by using the floating property. To
enable this, you’ll have to set the FloatingEnabled to true. In the floating class you’ll find the
properties margins, horizontal and vertical positions. These will allow you to place the item where
you want.
This code snippet generates an image that will align in the top right corner:
i := Paragraph.AddImage;
i.Bitmap.Assign(WebImageControl1.Picture);
i.Transformation.Width := 100;
i.Transformation.Height := 100;
i.Floating.HorizontalPosition.Relative := hpRightMargin;
i.Floating.HorizontalPosition.Align := hpaInside;
i.Floating.VerticalPosition.Align := vpaInside;
i.Floating.VerticalPosition.Relative := vpTopMargin;
i.FloatingEnabled := true;
Bookmark & Internal Hyperlink
A Bookmark is used for an internal hyperlink. Adding a bookmark and setting its text will display it as simple text. However, with the internal hyperlink you will be able to link to this text. You can do this by setting the ID of the bookmark and referencing it in the internal hyperlink.
Bookmark example:
Referencing Bookmark:External Hyperlink
This is used to link to other documents, web pages, … You can set an alternative text by adding extra text elements to the children collection of the external hyperlink. This is done like so:
externalHyperlink := paragraph.AddExternalHyperlink;
externalHyperlink.Link := 'https://www.tmssoftware.com';
Table Of Contents
This is autogenerated content that will show all headers and their pages. You can set it to auto link to the corresponding heading or not. Note that using this autogenerated content will give a warning that this has to be generated when opening word.
PageNumber
Page numbers have the same properties as the text. The only exception is when adding a page number, you’ll have to supply a format to show the text.
Example
the first %s is the current page and the second one is the total pages. The total pages is optional here.
The standard format for pagenumbering is the following layout:

Headers and footers
For headers and footers, it’s important to know that they are only used inside that section. You have respectively the first, even and default headers and footers. So, you can set it however you want. These footers have the same options as the sections for adding items. You can add paragraphs and tables here as suited for your needs. You can access these headers and footers like this:
This will add a paragraph to all footers in that section.Templating
This component let you construct documents and export them to a JSON. You can use this json to
persist where you want. And when you need it, you can simply load it in. For every document child
you can set an ID. You can search the document for this ID. This will return the Object as a
TPersistent. This means that you will have to cast it to the correct object.
Example of using a template:
Generating Docx
You can generate the documents as a base64 string or save it directly to a file. To do this, you’ll
have to subscribe to the OnDownloadAsBase64 or OnDownloadAsFile events. To start the generation,
you’ll need to use the GetDocxAsFile and GetDocxAsBase64 procedures. The GetDocxAsFile requires
a filename with full path to start generating Docx files