Text replacement breaks layout in this PDF

Hello,
I have a problem when trying to replace text in this PDF.
If I try to replace the word “Les”, the remaining of the line move to the left border of the page.
If I try to replace “numérisation” there is a large gap between the edited word and the remaining of the line.
I tried different settings for TextReplaceOptions.ReplaceAdjustment and TextEditOptions.ClippingPathsProcessingMode but nothing works in this PDF.

I can easily reproduce the problem starting from your sample codes.

        public static void Run()
        {
            // ExStart:ReplaceFirstOccurrence 
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_AsposePdf_Text();

            // Open document
            Document pdfDocument = new Document(dataDir + "ReplaceBug.pdf");

            // Create TextAbsorber object to find all instances of the input search phrase
            //TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Les");
            TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("numérisation");
            // Accept the absorber for all the pages
            pdfDocument.Pages.Accept(textFragmentAbsorber);
            // Get the extracted text fragments
            TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
            if (textFragmentCollection.Count > 0)
            {
                // Get first occurance of text and replace
                TextFragment textFragment = textFragmentCollection[1];
                // Update text and other properties
                textFragment.Text = "XXX";

                dataDir = dataDir + "ReplaceFirstOccurrence_out.pdf";
                pdfDocument.Save(dataDir);                 

                Console.WriteLine("\nText replaced successfully.\nFile saved at " + dataDir);
            }
            // ExEnd:ReplaceFirstOccurrence 
        }

replacebug.pdf (1.1 MB)

@louis.a

It seems you are experiencing layout issues when replacing text in your PDF document using Aspose.PDF. This can happen due to the way text is rendered and positioned in the PDF. Here are some suggestions to help you address the problem:

  1. Text Replacement Options: You mentioned trying different settings for TextReplaceOptions.ReplaceAdjustment and TextEditOptions.ClippingPathsProcessingMode. Make sure you are using the correct options that suit your specific case. For instance, adjusting the ReplaceAdjustment can help in aligning the text correctly after replacement.

  2. Font and Size Consistency: When replacing text, ensure that the font and size of the new text match the original text. This can help maintain the layout. You can set the font and size of the TextFragment after replacement like this:

    textFragment.TextState.Font = FontRepository.FindFont("YourFontName");
    textFragment.TextState.FontSize = originalFontSize; // Set to the original font size
    
  3. Testing with Different Text: Sometimes, the specific text being replaced can cause issues. Try replacing with different words or phrases to see if the problem persists. This can help identify if the issue is related to specific characters or formatting.

  4. Using TextFragmentAbsorber: Ensure that you are correctly using the TextFragmentAbsorber to find and replace text. If you are replacing multiple occurrences, consider looping through all found fragments to ensure each is handled properly.

  5. Example Code: Here’s a simplified version of how you might implement text replacement:

    Document pdfDocument = new Document("path/to/your/document.pdf");
    TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("textToReplace");
    pdfDocument.Pages.Accept(textFragmentAbsorber);
    TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
    
    foreach (TextFragment textFragment in textFragmentCollection)
    {
        textFragment.Text = "NewText";
        textFragment.TextState.Font = FontRepository.FindFont("YourFontName");
        textFragment.TextState.FontSize = 12; // Set to desired size
    }
    
    pdfDocument.Save("path/to/your/output.pdf");
    

If these suggestions do not resolve the issue, consider sharing a sample PDF that reproduces the problem on the forum for further assistance. This will help others to better understand the layout issues you are facing.

Sources:
[1]: [ReplaceFirstOccurrence .cs](https://github.com/aspose-pdf/Aspose.PDF-for-.NET/tree/master/Examples/CSharp/AsposePDF/Text/ReplaceFirstOccurrence .cs)
[2]: ReplaceTextAll.cs