We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

PdfContentEditor.ReplaceText failure

Hello,

I used the following code to replace text in a PDF document. However, it fails with the attached file with the error message: "Object reference not set to an instance of an object"

PdfContentEditor pdfContentEditor = new PdfContentEditor();
pdfContentEditor.BindPdf(filePath);
// Replace all occurences of text by another text
pdfContentEditor.TextReplaceOptions.ReplaceScope = TextReplaceOptions.Scope.REPLACE_ALL;
pdfContentEditor.ReplaceText("old", "new");
pdfContentEditor.Save(filePath);
pdfContentEditor.Close();

Please help me investigate the problem.
Thanks in advance.
Best regards,
Dung

Hi Dung,


Thanks for contacting support.

I have tested the scenario with latest version of API (i.e Aspose.Pdf for .NET 17.4.0) and was able to notice the issue. However, I have run the scenario using new DOM approach because Aspose.Pdf.Facades approach is going to be obsolete soon.

It seemed that the issue is related with the input document which you have shared because when I tried to add simple text inside it, the API threw an exception. Please check the following code snippet which I have used to add the text inside PDF.

Document doc = new Document(dataDir + “Apose_PdfContentEditor.ReplaceText_failure.pdf”);<o:p></o:p>

doc.Pages[1].Paragraphs.Add(new TextFragment("This is old"));

doc.Save(dataDir + "Apose_PdfContentEditor.ReplaceText_failure_out.pdf");

doc = new Document(dataDir + "Apose_PdfContentEditor.ReplaceText_failure_out.pdf");



Furthermore, when I created a new PDF and run the following code snippet to replace the text inside it, the code executed just fine.

Document doc = new Document();

doc.Pages.Add().Paragraphs.Add(new TextFragment("This is old"));

doc.Save(dataDir + "Apose_PdfContentEditor.ReplaceText_failure_out.pdf");

doc = new Document(dataDir + "Apose_PdfContentEditor.ReplaceText_failure_out.pdf");

TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("old");

doc.Pages.Accept(textFragmentAbsorber);

TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

foreach (TextFragment textFragment in textFragmentCollection)

{

textFragment.Text = "new";

}

doc.Save(dataDir + "Apose_PdfContentEditor.ReplaceText_failure_out.pdf");



Therefore, for the sake of detailed investigation, I have logged an issue as PDFNET-42654 in our issue tracking system. We will further investigate the issue and keep you updated on the status of its resolution. Please be patient and spare us a little time.

We are sorry for the inconvenience.


Best Regards,