In This Topic
    Update the style of target text
    In This Topic

    Introduction

    The following example demonstrates how to modify the style of some of the text found in a PDF document. It also briefly covers how to find specific text in a page.

    Modify the text style (C#)
    Copy Code
    public static void UpdateTextStyle()
    {
      Console.WriteLine( "=== UPDATE TEXT STYLE ===" );
      var outputFileName = "UpdateTextStyle.pdf";
      var outputPath = TextsSample.TextsSampleOutputDirectory + outputFileName;
    
      // Loads a PdfDocument.
      using( var pdfInput = PdfDocument.Load( TextsSample.TextsSampleResourcesDirectory + @"Two Page Text Only - from libre office.pdf" ) )
      {
        // Gets the first Page. 
        var page = pdfInput.Pages.First();
    
        // Set the text to find.
        var textSearchOptions = new TextSearchOptions( "License" );         
    
        // Get all the texts matching the search options in page 1.
        var textMatches = page.FindText( textSearchOptions );
    
        // Update the TextStyle of the found texts with a red CourierBoldOblique font of size 16.
        var newTextStyle = TextStyle.WithFontAndBrush( pdfInput.Fonts.GetStandardFont( StandardFontType.CourierBoldOblique ), 16d, Brushes.Red );
        page.UpdateTextStyle( textMatches, newTextStyle );
    
        // Saves to the output document.
        pdfInput.SaveAs( outputPath );
        Console.WriteLine( $"Info exported to path: {outputFileName}" );
      }
    }


    See Also