/***************************************************************************************
Xceed PdfLibray for .NET – Xceed.PdfLibrary.NET.Examples – watermarks Sample Application
Copyright (c) 2026 - Xceed Software Inc.
This application demonstrates how to work with Watermarks when using the API
from the Xceed PdfLibrary for .NET.
This file is part of Xceed PdfLibrary for .NET. The source code in this file
is only intended as a supplement to the documentation, and is provided
"as is", without warranty of any kind, either expressed or implied.
*************************************************************************************/
using System;
using System.IO;
using System.Linq;
namespace Xceed.PdfLibrary.NET.Examples
{
public class WatermarksSample
{
#region Private Members
private const string WatermarksSampleResourcesDirectory = Program.SampleDirectory + @"Watermarks\Resources\";
private const string WatermarksSampleOutputDirectory = Program.SampleDirectory + @"Watermarks\Output\";
#endregion
#region Constructors
static WatermarksSample()
{
if( !Directory.Exists( WatermarksSample.WatermarksSampleResourcesDirectory ) )
{
Directory.CreateDirectory( WatermarksSample.WatermarksSampleResourcesDirectory );
}
if( !Directory.Exists( WatermarksSample.WatermarksSampleOutputDirectory ) )
{
Directory.CreateDirectory( WatermarksSample.WatermarksSampleOutputDirectory );
}
}
#endregion
#region Public Methods
#region Method: AddTextWatermarksToAllPages
public static void AddTextWatermarksToAllPages()
{
Console.WriteLine( "=== ADD TEXT WATERMARKS TO ALL PAGES ===" );
var outputFileName = "AddTextWatermarkToAllPages.pdf";
var outputPath = WatermarksSample.WatermarksSampleOutputDirectory + outputFileName;
// Creates a PdfDocument.
using( var pdfoutput = PdfDocument.Create( outputPath ) )
{
// Defines a WatermarkDefinition with text and adds it on all Pages.
var watermarkTextStyle = TextStyle.WithFontAndBrush( pdfoutput.Fonts.GetStandardFont( StandardFontType.HelveticaBold ), 300d, Brushes.Red );
var watermarkDefinition = new WatermarkDefinition();
watermarkDefinition.AddText( "Big", watermarkTextStyle );
pdfoutput.Watermarks.Add( watermarkDefinition );
// Gets the first Page of the document.
var page = pdfoutput.Pages.First();
// Adds a title.
var textStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d );
page.AddParagraph( "Add Text Watermarks for all pages", textStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) );
// Adds text to the first Page.
page.AddText( "This is the first page of the document", new Point( 100, 100 ) );
// Adds a second Page and adds text on it.
page = pdfoutput.Pages.Add();
page.AddParagraph( "This is the second page of the document" );
// Saves the output document.
pdfoutput.Save();
Console.WriteLine( $"Info exported to path: {outputFileName}" );
}
}
#endregion // Method: AddTextWatermarksToAllPages
#region Method: AddImageWatermarksToAllPages
public static void AddImageWatermarksToAllPages()
{
Console.WriteLine( "=== ADD IMAGE WATERMARKS TO ALL PAGES ===" );
var outputFileName = "AddImageWatermarkToAllPages.pdf";
var outputPath = WatermarksSample.WatermarksSampleOutputDirectory + outputFileName;
// Creates a PdfDocument.
using( var pdfoutput = PdfDocument.Create( outputPath ) )
{
// Gets the first Page of the document.
var page = pdfoutput.Pages.First();
// Adds a title.
var textStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d );
page.AddParagraph( "Add Image Watermarks for all pages", textStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) );
// Adds text to the first Page.
page.AddText( "This is the first page of the document", new Point( 100, 100 ) );
// Adds a second Page and adds text on it.
page = pdfoutput.Pages.Add();
page.AddParagraph( "This is the second page of the document" );
// Defines a WatermarkDefinition with an image and add it on all Pages.
var imageSource = ImageSource.FromPath( WatermarksSample.WatermarksSampleResourcesDirectory + @"balloon.jpg" );
var watermarkDefinition = new WatermarkDefinition();
watermarkDefinition.AddImage( imageSource );
pdfoutput.Watermarks.Add( watermarkDefinition );
// Saves the output document.
pdfoutput.Save();
Console.WriteLine( $"Info exported to path: {outputFileName}" );
}
}
#endregion // Method: AddImageAndTextWatermarksToAllPages
#region Method: AddWatermarkToFirstPage
public static void AddWatermarkToFirstPage()
{
Console.WriteLine( "=== ADD WATERMARKS TO FIRST PAGE ===" );
var outputFileName = "AddWatermarkToFirstPage.pdf";
var outputPath = WatermarksSample.WatermarksSampleOutputDirectory + outputFileName;
// Creates a PdfDocument.
using( var pdfoutput = PdfDocument.Create( outputPath ) )
{
// Gets the first Page of the document.
var page = pdfoutput.Pages.First();
// Adds a title.
var textStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d );
page.AddParagraph( "Add Watermarks to first page only", textStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) );
// Add text to the first Page.
page.AddText( "This is the first page of the document", new Point( 100, 100 ) );
// Adds a second Page and adds text on it.
page = pdfoutput.Pages.Add();
page.AddParagraph( "This is the second page of the document" );
// Defines a WatermarkDefinition with text using the default watermark's TextStyle and adds it on the first Page only.
var watermarkDefinition = new WatermarkDefinition();
watermarkDefinition.AddText( "Confidential" );
pdfoutput.Pages[ 0 ].Watermarks.Add( watermarkDefinition );
// Saves the output document.
pdfoutput.Save();
Console.WriteLine( $"Info exported to path: {outputFileName}" );
}
}
#endregion // Method: AddWatermarkToFirstPage
#region Method: AddImageAndTextWatermarksToAllPages
public static void AddImageAndTextWatermarksToAllPages()
{
Console.WriteLine( "=== ADD IMAGE AND TEXT WATERMARKS TO ALL PAGES ===" );
var outputFileName = "AddImageAndTextWatermarkToAllPages.pdf";
var outputPath = WatermarksSample.WatermarksSampleOutputDirectory + outputFileName;
// Creates a PdfDocument.
using( var pdfoutput = PdfDocument.Create( outputPath ) )
{
// Gets the first Page of the document.
var page = pdfoutput.Pages.First();
// Adds a title.
var textStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d );
page.AddParagraph( "Add Image and Text Watermarks for all pages", textStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) );
// Adds text to the first Page.
page.AddText( "This is the first page of the document", new Point( 100, 100 ) );
// Adds a second Page and adds text on it.
page = pdfoutput.Pages.Add();
page.AddParagraph( "This is the second page of the document" );
// Defines a WatermarkDefinition with an image.
// Its TopLeft is (0, 0) inside the Watermark.
var watermarkDefinition = new WatermarkDefinition();
var imageSource = ImageSource.FromPath( WatermarksSample.WatermarksSampleResourcesDirectory + @"balloon.jpg" );
var watermarkImage = watermarkDefinition.AddImage( imageSource );
// Add a Text to the newly created watermarkDefinition.
// Its TopLeft is (0, 0) inside the WatermarkDefinition.
var watermarkText = watermarkDefinition.AddText( "Confidential" );
// Set the watermarkText under the watermarkImage.
watermarkText.Location = new Point( 0, watermarkImage.Bounds.Height );
// Set the horizontalAlignment of the WatermarkImage in the center of the final Watermark.
var contentSize = watermarkDefinition.GetContentSize( pdfoutput );
watermarkImage.Bounds = new Rectangle( new Point( contentSize.Width / 2 - watermarkImage.Bounds.Width / 2, 0 ),
watermarkImage.Bounds.Size );
// Rotate for 45 degrees the final watermark(containing the image and text).
watermarkDefinition.RotationAngle = 45d;
// Add the watermarkDefinition (containing an image and a text) in the PdfDocument WatermarkDefinition collection.
pdfoutput.Watermarks.Add( watermarkDefinition );
// Saves the output document.
pdfoutput.Save();
Console.WriteLine( $"Info exported to path: {outputFileName}" );
}
}
#endregion // Method: AddImageWatermarksToAllPages
#region Method: GetWatermarks
public static void GetWatermarks()
{
Console.WriteLine( "=== GET WATERMARKS ===" );
// Loads a PdfDocument.
var loadedDocument = "DocWithWatermarks.pdf";
using( var pdfInput = PdfDocument.Load( WatermarksSample.WatermarksSampleResourcesDirectory + loadedDocument ) )
{
var outputFileName = "GetWatermarks.pdf";
var outputPath = WatermarksSample.WatermarksSampleOutputDirectory + outputFileName;
// Creates a PdfDocument.
using( var pdfOutput = PdfDocument.Create( outputPath ) )
{
// Gets the first Page of the document.
var page = pdfOutput.Pages.First();
// Adds a title.
var titleStyle = TextStyle.WithFont( pdfOutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d );
page.AddParagraph( "Get Watermarks", titleStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) );
// Adds text.
var textStyle = TextStyle.WithFontAndBrush( pdfOutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 12, Brushes.Red );
page.AddText( $"Getting watermarks from document: {loadedDocument}", new Point( 100, 100 ), textStyle );
// Gets the WatermarkDefinitions from the loaded document.
var watermarkDefinitions = pdfInput.Watermarks;
// For each WatermarkDefinition, displays some of its properties.
for( int i = 0; i < watermarkDefinitions.Count; ++i )
{
var watermarkDefinition = watermarkDefinitions[ i ];
foreach( var watermarkText in watermarkDefinition.Texts )
{
page.AddText( "WatermarkText " + ( i + 1 ) + ":", new Point( 30, 150 + ( i * 65 ) ) );
page.AddText( $" Text: {watermarkText.Text}", new Point( 30, 165 + ( i * 65 ) ) );
page.AddText( $" Font name: {watermarkText.TextStyle.Font.Name}", new Point( 30, 180 + ( i * 65 ) ) );
page.AddText( $" Font size: {watermarkText.TextStyle.FontSize}", new Point( 30, 195 + ( i * 65 ) ) );
page.AddText( $" text color: {( ( SolidColorBrush )watermarkText.TextStyle.Brush ).Color}", new Point( 30, 210 + ( i * 65 ) ) );
}
}
// Saves the output document.
pdfOutput.Save();
Console.WriteLine( $"Info exported to path: {outputFileName}" );
}
}
}
#endregion // Method: GetWatermarks
#endregion
}
}
|