Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Last active November 15, 2021 06:56
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 conholdate-gists/c8f2a9a3364bda3b3656eca5cdc7d466 to your computer and use it in GitHub Desktop.
Save conholdate-gists/c8f2a9a3364bda3b3656eca5cdc7d466 to your computer and use it in GitHub Desktop.
Add Text or Image Watermarks to Word Documents using C#
// Create watermarker
Watermarker watermarker = new Watermarker(@"C:\Files\Watermark\sample.docx");
// Create the watermark object
ImageWatermark watermark = new ImageWatermark(@"C:\Files\Watermark\logo.png");
// Set watermark alignment
watermark.HorizontalAlignment = HorizontalAlignment.Center;
watermark.VerticalAlignment = VerticalAlignment.Bottom;
// Set watermark size
watermark.Width = 100;
watermark.Height = 100;
// Add watermark
watermarker.Add(watermark);
// Save the output file
watermarker.Save(@"C:\Files\Watermark\AddImageWatermark_output.docx");
// Creater watermarker
Watermarker watermarker = new Watermarker(@"C:\Files\Watermark\sample.docx");
// Initialize the font to be used for watermark
Font font = new Font("Arial", 19, FontStyle.Bold | FontStyle.Italic);
// Create the watermark object
TextWatermark watermark = new TextWatermark("Simple Text Watermark", font);
// Set watermark properties
watermark.ForegroundColor = Color.Red;
watermark.BackgroundColor = Color.Blue;
watermark.TextAlignment = TextAlignment.Right;
watermark.HorizontalAlignment = HorizontalAlignment.Center;
watermark.VerticalAlignment = VerticalAlignment.Bottom;
// Set watermark size
watermark.Width = 150;
watermark.Height = 40;
// Set opacity level
watermark.Opacity = 0.9;
// Add watermark
watermarker.Add(watermark);
// Save the output file
watermarker.Save(@"C:\Files\Watermark\addTextWatermark_output.docx");
// Create watermarker
Watermarker watermarker = new Watermarker(@"C:\Files\Watermark\sample.docx");
// Create image watermark
using (ImageWatermark watermark = new ImageWatermark(@"C:\Files\Watermark\logo.png"))
{
// Set watermark properties
watermark.Height = 100;
watermark.Width = 100;
watermark.HorizontalAlignment = HorizontalAlignment.Right;
// Add watermark to all headers of the first section
WordProcessingWatermarkSectionOptions options = new WordProcessingWatermarkSectionOptions();
options.SectionIndex = 0;
watermarker.Add(watermark, options);
}
// Link all other headers&footers to corresponding headers&footers of the first section
WordProcessingContent content = watermarker.GetContent<WordProcessingContent>();
for (int i = 1; i < content.Sections.Count; i++)
{
content.Sections[i].HeadersFooters.LinkToPrevious(true);
}
// Save the output file
watermarker.Save(@"C:\Files\Watermark\AddWatermarkToHeadersFooters_output.docx");
// Create watermarker
Watermarker watermarker = new Watermarker(@"C:\Files\Watermark\sample.docx");
// Creater text watermark
TextWatermark watermark = new TextWatermark("Protected image", new Font("Arial", 8));
// Set watermark properties
watermark.ForegroundColor = Color.Black;
watermark.HorizontalAlignment = HorizontalAlignment.Center;
watermark.VerticalAlignment = VerticalAlignment.Center;
watermark.RotateAngle = 45;
watermark.SizingType = SizingType.ScaleToParentDimensions;
watermark.ScaleFactor = 1;
// Find all images in the content.
WatermarkableImageCollection images = watermarker.GetImages();
// Add watermark.
foreach (WatermarkableImage watermarkableImage in images)
{
watermarkableImage.Add(watermark);
}
// Save the output file
watermarker.Save(@"C:\Files\Watermark\AddWatermarkToImages_output.docx");
// Create watermarker
Watermarker watermarker = new Watermarker(@"C:\Files\Watermark\sample.docx");
// Create text watermark
TextWatermark watermark = new TextWatermark("This is simple watermark!", new Font("Arial", 26));
// Set watermark properties
watermark.ForegroundColor = Color.Red;
watermark.BackgroundColor = Color.Blue;
watermark.TextAlignment = TextAlignment.Right;
watermark.HorizontalAlignment = HorizontalAlignment.Left;
watermark.VerticalAlignment = VerticalAlignment.Top;
// Add watermark to the last page
WordProcessingContent content = watermarker.GetContent<WordProcessingContent>();
WordProcessingWatermarkPagesOptions options = new WordProcessingWatermarkPagesOptions();
options.PageNumbers = new int[] { content.PageCount };
// Add watermark
watermarker.Add(watermark, options);
// Save the output file
watermarker.Save(@"C:\Files\Watermark\AddToSpecificPage_output.docx");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment