In This Topic
    Get the watermarks
    In This Topic

    Introduction

    The following example demonstrates how to get the watermarks used in a PDF document & then display some of their properties (like the text, font, character size, etc.).

    Get watermarks from a PDF document (C#)
    Copy Code
    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.WithFontAndColor( pdfOutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 12, Brushes.Red );
          page.AddText( $"Getting watermarks from document: {loadedDocument}", new Point( 100, 100 ), textStyle );
    
          // Gets the Watermarks from the loaded document.
          var watermarks = pdfInput.Watermarks;
    
          // For each Watermark, displays some of its properties.
          for( int i = 0; i < watermarks.Count; ++i )
          {
            var watermark = watermarks[ i ];
    
            page.AddText( "Watermark " + ( i + 1 ) + ":", new Point( 30, 150 + ( i * 65 ) ) );
            page.AddText( $"  Text: {watermark.Text}", new Point( 30, 165 + ( i * 65 ) ) );
            page.AddText( $"  Font name: {watermark.TextStyle.Font.Name}", new Point( 30, 180 + ( i * 65 ) ) );
            page.AddText( $"  Font size: {watermark.TextStyle.FontSize}", new Point( 30, 195 + ( i * 65 ) ) );
            page.AddText( $"  text color: {( ( SolidColorBrush )watermark.TextStyle.Brush ).Color}", new Point( 30, 210 + ( i * 65 ) ) );
          }
    
          // Saves the output document.
          pdfOutput.Save();
          Console.WriteLine( $"Info exported to path: {outputFileName}" );
        }
      }
    }
    

     

    See Also

    Add elements in a PDF document