Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created December 20, 2023 13:44
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/2cad9ac416bc408b7e8cfb3c684a01bd to your computer and use it in GitHub Desktop.
Save bjoerntx/2cad9ac416bc408b7e8cfb3c684a01bd to your computer and use it in GitHub Desktop.
public void AddWatermark(TXTextControl.TextControl tx, byte[] watermark)
{
tx.PageUnit = TXTextControl.MeasuringUnit.Twips;
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;
using (MemoryStream ms = new MemoryStream(watermark, 0, watermark.Length, writable: false, publiclyVisible: true))
{
image = new TXTextControl.Image(ms);
hf.Images.Add(
image,
1,
new System.Drawing.Point(0, 0),
TXTextControl.ImageInsertionMode.FixedOnPage |
TXTextControl.ImageInsertionMode.BelowTheText);
}
// Calculate the horizontal center location
int locationX = (int)((section.Format.PageSize.Width - image.Size.Width -
(section.Format.PageMargins.Left + section.Format.PageMargins.Right)) / 2);
// Calculate the vertical center location
int locationY = (int)((section.Format.PageSize.Height - image.Size.Height -
(section.Format.PageMargins.Top + section.Format.PageMargins.Bottom)) / 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