Created
December 20, 2023 13:44
-
-
Save bjoerntx/2cad9ac416bc408b7e8cfb3c684a01bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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