Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Last active January 27, 2022 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/475eadddf0dbe53a44c4b85670595c3c to your computer and use it in GitHub Desktop.
Save bjoerntx/475eadddf0dbe53a44c4b85670595c3c to your computer and use it in GitHub Desktop.
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment