[Demo] Page background and border in RTF and DocX

Demos, code samples. Only questions related to the existing topics are allowed here.
Post Reply
Sergey Tkachenko
Site Admin
Posts: 17555
Joined: Sat Aug 27, 2005 10:28 am
Contact:

[Demo] Page background and border in RTF and DocX

Post by Sergey Tkachenko »

Microsoft Word Documents (DocX and RTF) do not support page background directly.
However, there is a workaround: you can insert a text box in a header or a footer, and add an image to this textbox. Additionally, this textbox can have a border.
Actually, Microsoft Word itself uses this technique when adding a page watermark.

This file contains demos showing how to generate a document with page background image and borders:
PageBackground.zip
(94.51 KiB) Downloaded 4836 times
This document is saved to DocX and RTF files.

There are 4 versions of the demo: (for TRichView and for ScaleRichView) x (for Delphi and for C++Builder 11)
The demos for C++Builder 11 can be used in older versions of C++Builder, you just need to change references to packages in the CBProj file.

PageBackgroundScaleRichView.png
PageBackgroundScaleRichView.png (103.72 KiB) Viewed 123836 times
Note 1: A text box is positioned relative to the whole page, its size and position are calculated in absolute values (depending on page size and margins).
Why don't we use sizing and positioning relative to the main text area? It was done for compatibility with Libre Office. Unfortunately, its current version has problems with advanced options for textbox positioning.

Note 2: This demo shows the simplest case: the same background image for all pages. TRichView and ScaleRichView support special headers for the first page and odd/even pages, so they can have different background images and borders.
Post Reply