The following example demonstrates how to add a watermark on the first page of a PDF document.
Below this example is another example covering how to add a watermark on every page of a document.
| Add watermarks on a PDF document (C#) |
Copy Code |
|---|---|
public static void AddWatermarkToFirstPage() { Console.WriteLine( "=== ADD WATERMARKS TO FIRST PAGE ===" ); var outputFileName = "AddWatermarkToFirstPage.pdf"; var outputPath = WatermarksSample.WatermarksSampleOutputDirectory + outputFileName; // Create a pdf document. using( var pdfoutput = PdfDocument.Create( outputPath ) ) { // Get the first page of the document. var page = pdfoutput.Pages.First(); // Add 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 first page. page.AddText( "This is the first page of the document", new Point( 100, 100 ) ); // Add a second page and add text to it. page = pdfoutput.Pages.Add(); page.AddParagraph( "This is the second page of the document" ); // Define a watermark using the default watermark TextStyle and add it only for first page. var watermark = new Watermark( "Confidential" ); pdfoutput.Pages[ 0 ].Watermarks.Add( watermark ); // Save the output document. pdfoutput.Save(); Console.WriteLine( $"Info exported to path: {outputFileName}" ); } } | |
The following example demonstrates how to add a watermark on every page of a PDF document.
| Add watermarks on every page of a PDF document (C#) |
Copy Code |
|---|---|
public static void AddWatermarksToAllPages() { Console.WriteLine( "=== ADD WATERMARKS TO ALL PAGES ===" ); var outputFileName = "AddWatermarkToAllPages.pdf"; var outputPath = WatermarksSample.WatermarksSampleOutputDirectory + outputFileName; // Create a pdf document. using( var pdfoutput = PdfDocument.Create( outputPath ) ) { // Get the first page of the document. var page = pdfoutput.Pages.First(); // Add title. var textStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d ); page.AddParagraph( "Add Watermarks for all pages", textStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) ); // Add Text to first page. page.AddText( "This is the first page of the document", new Point( 100, 100 ) ); // Add a second page and add text to it. page = pdfoutput.Pages.Add(); page.AddParagraph( "This is the second page of the document" ); // Define a watermark and add it for all pages. var watermarkTextStyle = TextStyle.WithFontAndColor( pdfoutput.Fonts.GetStandardFont( StandardFontType.HelveticaBold ), 300d, Color.Red ); var watermark = new Watermark( "Big", watermarkTextStyle ); pdfoutput.Watermarks.Add( watermark ); // Save the output document. pdfoutput.Save(); Console.WriteLine( $"Info exported to path: {outputFileName}" ); } } | |