Aspose PDF question about replacing Text on PDF like the previous text

have attached a PDF sample. Would it be possible to replace a word in the PDF with another word in the exact same place, using the same font and size as the original content, and produce a new PDF? For example, if I have “Dogs” as one piece of content in the attached PDF, would it be possible to replace that word with “Mouse” or “Cats” with another word in the exact same place with the same font style? Does your library have any features to accomplish this?

Our requirement is to read the content of the PDF as input, then replace a word dynamically based on the input given, and produce the output PDF with the exact same style and font.

If your library has this feature, please let us know the cost of the license, and we would like to get a proof of concept sample as well

@simie123

Yes, it is possible to replace a word in a PDF with another word in the exact same position, using the same font and size as the original content, with Aspose.PDF for .NET. You can achieve this by using the TextFragmentAbsorber class to find the text you want to replace and then update its properties accordingly.

Here’s a step-by-step guide along with a code example to help you implement this:

  1. Load the PDF Document: Start by loading the PDF document that contains the text you want to replace.

  2. Create a Text Absorber: Use the TextFragmentAbsorber to search for the specific text you want to replace.

  3. Update the Text Fragment: Once you find the text fragment, you can update its text and properties such as font and size to match the original.

  4. Save the Updated PDF: Finally, save the modified document to create a new PDF with the changes.

Here’s a sample code snippet demonstrating how to replace text in a PDF:

using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Text;

namespace Aspose.Pdf.Examples.CSharp.AsposePDF.Text
{
    public class ReplaceTextExample
    {
        public static void Run()
        {
            // Load the PDF document
            Document pdfDocument = new Document("input.pdf");

            // Create TextAbsorber to find the text to replace
            TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Dogs");

            // Accept the absorber for all pages
            pdfDocument.Pages.Accept(textFragmentAbsorber);

            // Get the extracted text fragments
            TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

            // Loop through the fragments and update the text
            foreach (TextFragment textFragment in textFragmentCollection)
            {
                // Update text and other properties
                textFragment.Text = "Cats"; // Replace with the new word
                // Optionally, set the font and size to match the original
                textFragment.TextState.Font = FontRepository.FindFont("OriginalFontName"); // Use the original font name
                textFragment.TextState.FontSize = textFragment.TextState.FontSize; // Keep the same font size
            }

            // Save the updated PDF document
            pdfDocument.Save("output.pdf");
        }
    }
}

Important Notes:

  • Make sure to replace "OriginalFontName" with the actual font name used in the original PDF.
  • You can also adjust other properties like color if needed.
  • Aspose.PDF for .NET provides a free trial version, which you can use to evaluate the library before purchasing a license.

If you need further assistance or a proof of concept, feel free to ask!

Sources:
[1]: Replace Missing Fonts | Aspose.PDF for .NET API Reference
[2]: ReplaceTextPage.cs

I need to read the font from PDF dynamically

@simie123

If you want to extract or read fonts from a PDF, you can please check below help article:

In case you want to check the font of searched text, you can use same example shared in previous message and get it using TextFragment.TextState.Font property.

Thank you for the updates. This is the updated code I have, but it still returns “Times New Roman” and does not read the exact font of the word I am trying to replace. Please let me know if you have a solution for this:

foreach (TextFragment textFragment in textFragments)
 {
     textFragment.Text = replaceText;
     // ✅ Extract the correct original font
     string originalFontName = textFragment.TextState.Font?.FontName;
     bool isFontEmbedded = textFragment.TextState.Font?.IsEmbedded ?? false;
     Console.WriteLine($"Original Font: {originalFontName}, Embedded: {isFontEmbedded}");
     // ✅ Try to find the original font
     Font correctFont = null;
     if (!string.IsNullOrEmpty(originalFontName))
     {
         correctFont = FontRepository.FindFont(originalFontName);
     }
     // ✅ If FontRepository cannot find it, check manually from system fonts
     if (correctFont == null)
     {
         Console.WriteLine("Font not found in system. Trying to load manually...");
         correctFont = FontRepository.OpenFont(@"C:\Windows\Fonts\Aptos.ttf"); // Load manually
     }
     // ✅ If everything fails, use a fallback font (Avoid Times New Roman)
     if (correctFont == null)
     {
         Console.WriteLine("Using fallback font Arial...");
         correctFont = FontRepository.FindFont("Arial");
     }
     // ✅ Apply the detected font
     textFragment.TextState.Font = correctFont;
     // ✅ Preserve text properties
     textFragment.TextState.FontSize = textFragment.TextState.FontSize;
     textFragment.TextState.ForegroundColor = textFragment.TextState.ForegroundColor;
     textFragment.TextState.HorizontalScaling = textFragment.TextState.HorizontalScaling;
     textFragment.TextState.CharacterSpacing = textFragment.TextState.CharacterSpacing;
     // ✅ Force font embedding to prevent missing font issues
     textFragment.TextState.Font.IsEmbedded = true;
 }

@simie123

Can you please share your sample PDF document for our reference as well? We will test the scenario in our environment and address it accordingly. Also, have you made sure that you were using the API with a valid or 30-days free temporary license?

Yes it worked

@simie123

Its nice to know that things started working at your end. Please feel free to create a new topic in case you face any kind of issues.