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; // 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 Watermark using the default watermark's TextStyle and adds it on the first Page only. var watermark = new Watermark( "Confidential" ); pdfoutput.Pages[ 0 ].Watermarks.Add( watermark ); // Saves 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; // 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 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 Watermark and adds it on all Pages. var watermarkTextStyle = TextStyle.WithFontAndColor( pdfoutput.Fonts.GetStandardFont( StandardFontType.HelveticaBold ), 300d, Brushes.Red ); var watermark = new Watermark( "Big", watermarkTextStyle ); pdfoutput.Watermarks.Add( watermark ); // Saves the output document. pdfoutput.Save(); Console.WriteLine( $"Info exported to path: {outputFileName}" ); } } | |