New to Telerik Document Processing? Download free 30-day trial

Replace Text Content With Image | Telerik Document Processing

Product Version Product Author
2021.1.118 RadPdfProcessing Tanya Dimitrova

Description

A common scenario is to replace a temporary page content (a placeholder text) with an image. This allows already created PDF documents to be modified by adding an image on a position defined by the existing content of the document.

Solution

The following example demonstrates the approach of iterating the page content and finding TextFragment elements matching the $ImagePlaceholder text. For each match, an Image instance is created and the TextFragment is replaced with it. The Position property is used to correctly position the image on the page.

foreach (RadFixedPage page in pdfDocument.Pages) 
{ 
    List<TextFragment> contentToRemove = new List<TextFragment>(); 
 
    foreach (ContentElementBase contentElement in page.Content.ToList()) 
    { 
        // Find a TextFragment exactly matching the $ImagePlaceholder text  
        TextFragment textFragment = contentElement as TextFragment; 
        if (textFragment != null && textFragment.Text == "$ImagePlaceholder") 
        { 
            // Create and add an image 
            Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource source = new Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource(File.OpenRead("sample.png")); 
            Telerik.Windows.Documents.Fixed.Model.Objects.Image image = page.Content.AddImage(source); 
 
            // Set the desired size to the image 
            image.Width = 10; 
            image.Height = 10; 
 
            // Position the image 
            image.Position = textFragment.Position; 
            image.Position.Translate(0, -textFragment.FontSize); 
 
            // Indicate the TextFragment to be removed  
            contentToRemove.Add(textFragment); 
        } 
    } 
 
    foreach (TextFragment contentElement in contentToRemove) 
    { 
        page.Content.Remove(contentElement); 
    } 
} 
In this article