Replace text not working for OCR Pdf from scanned pdf

Repleace text not working properly on OCR PDF file [we have created this file from scanned pdf]. I am using .net library

@Deepak_Singh_Rautela

Summary
The PDF you created from a scanned document contains only images. Without an OCR‑generated text layer there is no searchable text for Aspose.PDF’s replace‑text APIs to work on. In addition, the older SetText method shown in some examples no longer exists in recent Aspose.PDF for .NET releases – you should use the TextFragment.Text property (or the high‑level PdfContentEditor.ReplaceText method) instead.

Why it happens

  1. Scanned PDF – the file has no hidden text; therefore a replace operation cannot find any matches.
  2. Removed APISetText was part of older SDK versions and has been removed. Attempting to call it results in a compile‑time error, as you observed.

What to do next

  1. Create a searchable text layer (if you haven’t already) by running OCR on the scanned PDF. Aspose.PDF provides an OcrEngine that can add a hidden text layer to the document.
  2. Replace the text using one of the supported approaches:
  • High‑level editor
var editor = new Aspose.Pdf.Text.PdfContentEditor();
editor.BindPdf("scanned_ocr.pdf");
editor.ReplaceText("oldText", "newText", new Aspose.Pdf.Text.TextReplaceOptions { MatchCase = false });
editor.Save("scanned_ocr_replaced.pdf");
  • TextFragment manipulation (as suggested in the forum)
var doc = new Aspose.Pdf.Document("scanned_ocr.pdf");
foreach (var page in doc.Pages)
{
foreach (var fragment in page.Paragraphs.OfType<Aspose.Pdf.Text.TextFragment>())
{
if (fragment.Text.Contains("oldText"))
fragment.Text = fragment.Text.Replace("oldText", "newText");
}
}
doc.Save("scanned_ocr_replaced.pdf");
  1. Validate the output by opening the PDF in a viewer and confirming that the new text can be selected, or by extracting the text programmatically (doc.ExtractText()).

Checklist

  • ☐ Run OCR on the original scanned PDF to generate a hidden text layer.
  • ☐ Use either PdfContentEditor.ReplaceText or TextFragment.Text manipulation to replace the desired strings.
  • ☐ Save the modified PDF and verify the changes.
  • ☐ Ensure your project references a recent Aspose.PDF for .NET package (the SetText method is not available in current versions).

If the replacement still does not work, extract the OCR layer’s text to see the exact strings present and adjust your search pattern accordingly. Provide a minimal reproducible example and the SDK version you are using when seeking further assistance.

:warning: Please validate this behaviour with your exact SDK version and a minimal repro, as support can vary by release.