Adding SVG Watermarks to Documents
This article shows how to add SVG images to document section headers that repeat automatically on each page. This watermark will be inserted vertically and horizontally centered on each section page.

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. Frame
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.
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.
public bool Add(Image image,
int pageNumber,
System.Drawing.Point location,
ImageInsertionMode insertionMode);
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);
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:
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);
}
}
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);
}
Related Posts
How to Mail Merge MS Word DOCX Documents in ASP.NET Core C#
Mail merge is the process of merging data, such as Json or IEnumerable objects, into a template document, such as a DOC or DOCX file. This tutorial is a walkthrough of the steps necessary to…
Using MailMerge in ASP.NET Core 6 Web Applications
This article shows how to use the TX Text Control ASP.NET MailMerge class to merge templates with JSON data within a .NET 6 application in Visual Studio 2022.
Use MailMerge in .NET on Linux to Generate Pixel-Perfect PDFs from DOCX…
This article explores how to use the TX Text Control MailMerge feature in .NET applications on Linux to generate pixel-perfect PDFs from DOCX templates. This powerful combination enables…
Generating Dynamic NDAs Using TX Text Control MailMerge in C# .NET
This article demonstrates how to generate dynamic NDAs using TX Text Control MailMerge in C# .NET. It covers the process of creating a template, binding data, and generating the final document.
Designing a Maintainable PDF Generation Web API in ASP.NET Core (Linux) C#…
This article shows how to create a PDF generation Web API in ASP.NET Core on Linux using TX Text Control .NET Server. The clean architecture is used to create a maintainable and testable solution.