In order to add watermarks to documents, images are inserted into a page headers of each section. The content of headers (and footers) are repeated on each page automatically. FrameBase TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
FrameBase Class
The FrameBase class is the base class of the Image, TextFrame, ChartFrame, BarcodeFrame and DrawingFrame classes.
objects including images can be inserted into a header by overlapping the main text.

Scalable SVG Images

Since version 30.0, TX Text Control supports the insertion of SVG images which is the preferred image format for watermarks. The advantage of using SVG images is that they, as the name "Scalable Vector Graphics" implies, are scalable. One SVG image can be used for all required sizes in any document.

Watermark in TX Text Control

Positioned Behind Text

Additionally, objects can be positioned behind the actual text, so that the text is still visible and the watermark can be seen virtually in the background of a document. To achieve this, the following implementation of the Images.Add TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
ImageCollection Class
Add Method
Inserts a new image in a Text Control document.
method must be used:

public bool Add(Image image,
int pageNumber,
System.Drawing.Point location,
ImageInsertionMode insertionMode);
view raw overload.cs hosted with ❤ by GitHub

The required enumeration combination for the InsertionMode is FixedOnPage and BelowTheText:

hf.Images.Add(image,
1,
new System.Drawing.Point(0, 0),
TXTextControl.ImageInsertionMode.FixedOnPage |
TXTextControl.ImageInsertionMode.BelowTheText);
view raw test.cs hosted with ❤ by GitHub

Center the Image

Finally, the image must be centered vertically and horizontally on each page of each section in case there are different page sizes and orientations. The following formula is used to calculate the location of the background images:

([Page width] - [image width] - [both page margins]) / 2

The following diagram shows the various values that must be considered into the calculation of the exact horizontal location:

Center image

The following method AddWatermark creates headers for each section in order to insert a watermark centered on each page of the document:

public void AddWatermark(TXTextControl.ServerTextControl tx, string imagePath)
{
foreach (TXTextControl.Section section in tx.Sections)
{
// remove existing headers for demo purposes
section.HeadersAndFooters.Remove(TXTextControl.HeaderFooterType.All);
// add new header for each section
section.HeadersAndFooters.Add(TXTextControl.HeaderFooterType.Header);
TXTextControl.HeaderFooter hf =
(section.HeadersAndFooters.GetItem(TXTextControl.HeaderFooterType.Header));
// add the watermark
TXTextControl.Image image = new TXTextControl.Image();
image.FileName = imagePath;
hf.Images.Add(
image,
1,
new System.Drawing.Point(0, 0),
TXTextControl.ImageInsertionMode.FixedOnPage |
TXTextControl.ImageInsertionMode.BelowTheText);
// calculate the horizontal center location
var pageWidth = (int)section.Format.PageSize.Width;
var pageMarginsHorizontal = (int)(section.Format.PageMargins.Left +
section.Format.PageMargins.Right);
var imageWidth = image.Size.Width;
int locationX = (pageWidth - imageWidth - pageMarginsHorizontal) / 2;
// calculate the vertical center location
var pageHeight = (int)section.Format.PageSize.Height;
var pageMarginsVertical = (int)(section.Format.PageMargins.Top +
section.Format.PageMargins.Bottom);
var imageHeight = image.Size.Height;
int locationY = (pageHeight - imageHeight - pageMarginsVertical) / 2;
// set the location
image.Location = new System.Drawing.Point(locationX, locationY);
}
}
view raw test.cs hosted with ❤ by GitHub

This method is called with an instance of a ServerTextControl (same code is compatible to Windows Forms and WPF) and a path to the image. In the following code, an HttpGet method is implemented to load any document to add a watermark to the document pages:

[HttpGet]
public IActionResult AddWatermark()
{
byte[] bDocument;
// create a ServerTextControl
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
tx.Create();
tx.PageUnit = TXTextControl.MeasuringUnit.Twips;
// load the template
tx.Load("App_Data/nda.tx", TXTextControl.StreamType.InternalUnicodeFormat);
AddWatermark(tx, "App_Data/draft.svg");
// save in the internal format
tx.Save(out bDocument, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
}
return Ok(bDocument);
}
view raw test.cs hosted with ❤ by GitHub