Replace Text in PDF throw a NullReferenceException

I want to replace text in pdf with Aspose.PDF, But throw a NullReferenceException.
the sample code :
var doc = new Aspose.Pdf.Document(@“D:\abc\1212\c++.1.pdf”);
var sourceText = “的”;
var targetText = “x”;
Regex ruleRegexp = new Regex(Regex.Escape(sourceText));
var textFragmentAbsorber = new TextFragmentAbsorber(ruleRegexp, new TextSearchOptions(true));
doc.Pages.Accept(textFragmentAbsorber);
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
foreach (TextFragment textFragment in textFragmentCollection)
{
textFragment.Text = targetText;
}
doc.Save(@“D:\abc\1212\demo1.pdf”);

the sample file:
c++.1.pdf (15.6 KB)

the version is Aspose.PDF for .Net 21.6.0.0.

@sullivan

Would you kindly try to use the latest version of the API i.e. 22.3 and make sure that all fonts are installed in the system. In case issue still persists, please share the complete exception message along with stack trace. We will further proceed accordingly.

image.png (108.2 KB)

在 #=zKzeu6yEv9nebofO9$xNNn9AJ9vD$cHgjjmSEti_LQypq.#=z$cT4rUbVvbf55yEN5A==(List`1 #=zbotslM0=)
在 #=zKzeu6yEv9nebofO9$xNNn9AJ9vD$cHgjjmSEti_LQypq.#=ztTH47bPEooY0$iqjzQ==(String #=zcbpqey0=, Int32 #=zIdthYxISpFrq, Int32 #=z9aDDAXe4HEDt, #=zbrsRehmRENmR #=z01wTOlirUkzq)
在 #=zKzeu6yEv9nebofO9$xNNn9AJ9vD$cHgjjmSEti_LQypq.#=zi4$Qp88z4coPXSLteg==(String #=zcbpqey0=, Int32 #=zIdthYxISpFrq, Int32 #=z9aDDAXe4HEDt, #=zbrsRehmRENmR #=z01wTOlirUkzq)
在 #=zyTB3XIyaBhej7MWu47CesvBmHk1QvfIXNSnh3ZD8WuYv.#=zEJb9qfDHDxqp(#=zBEAE4JBpHv2_BzlcmaubjtgdPaDQbOlhSA== #=zz5uTWL4=, Int32 #=zIdthYxISpFrq, Int32 #=z9aDDAXe4HEDt, Double #=zGtYRtPg=, Double& #=zYOiNGjs=, Double& #=zTSP0dDs=, #=zvhGFYF4WvYmsypCRm15eZkn9ACXZU_X0S9deFGI=[] #=zGlbRdlmjjhbB, Boolean& #=zfWcswMo=, Boolean #=zOHy5bmpp70Dv_Ydq$THs46k=)
在 #=zyTB3XIyaBhej7MWu47CesvBmHk1QvfIXNSnh3ZD8WuYv.#=z6hWdC0A=(#=zBEAE4JBpHv2_BzlcmaubjtgdPaDQbOlhSA== #=zz5uTWL4=, Int32 #=zIdthYxISpFrq, Int32 #=z9aDDAXe4HEDt, Double #=zGtYRtPg=, Boolean #=zo46$6XmoA4Ts1znQfg==, Double& #=zYOiNGjs=, Double& #=zTSP0dDs=, #=zvhGFYF4WvYmsypCRm15eZkn9ACXZU_X0S9deFGI=[]& #=zHb0W7KV2aNTq, Boolean& #=zfWcswMo=, Boolean #=zOHy5bmpp70Dv_Ydq$THs46k=)
在 #=zyTB3XIyaBhej7MWu47CesvBmHk1QvfIXNSnh3ZD8WuYv.#=z6hWdC0A=(#=zBEAE4JBpHv2_BzlcmaubjtgdPaDQbOlhSA== #=zz5uTWL4=, Int32 #=zIdthYxISpFrq, Int32 #=z9aDDAXe4HEDt, Double #=zGtYRtPg=, #=zvhGFYF4WvYmsypCRm15eZkn9ACXZU_X0S9deFGI=[]& #=zHb0W7KV2aNTq)
在 #=zJRoqtKfEX723e63CZPklJ9VLHaLto9Hk8pHsTzc=.#=z6hWdC0A=(#=zBEAE4JBpHv2_BzlcmaubjtgdPaDQbOlhSA== #=zz5uTWL4=, Int32 #=zIdthYxISpFrq, Int32 #=z9aDDAXe4HEDt, Double #=zGtYRtPg=, #=zvhGFYF4WvYmsypCRm15eZkn9ACXZU_X0S9deFGI=[]& #=zHb0W7KV2aNTq)
在 #=zNkc__3NRDeBqrjlmbikMZfSF1f39KZpvA$q_mPPGYuC2GRpCZfOgoVs=.#=z6hWdC0A=(#=zBEAE4JBpHv2_BzlcmaubjtgdPaDQbOlhSA== #=zz5uTWL4=, Int32& #=zhapHuKIwgqvG, Int32 #=zJvhIcTs=, Int32 #=z$Opc65M=, Boolean #=z6HtpULqCUJKiS394NaTxBow=)
在 #=z1n61tGKXQVHtqVBtOwBlS2GUD9VvMb71aXmV1X2aT7VUVerEgg==.#=zDcZ3Bnos78tX(Int32 #=zJvhIcTs=, Int32 #=z$Opc65M=, Boolean #=z6HtpULqCUJKiS394NaTxBow=, Int32& #=zva9yaZc=, Int32& #=zd9PENlmupgrXXCJf0g==)
在 #=z1n61tGKXQVHtqVBtOwBlS2GUD9VvMb71aXmV1X2aT7VUVerEgg==.#=zIdS0yMx7bCde(Int32 #=zJvhIcTs=, Int32 #=z$Opc65M=, Boolean #=z6HtpULqCUJKiS394NaTxBow=)
在 #=zNkc__3NRDeBqrjlmbikMZfSF1f39KZpvA$q_mPPGYuC2GRpCZfOgoVs=.#=zIdS0yMx7bCde(Boolean #=z6HtpULqCUJKiS394NaTxBow=)
在 #=zNkc__3NRDeBqrjlmbikMZfSF1f39KZpvA$q_mPPGYuC2GRpCZfOgoVs=.#=zqmPpsa1_Iljn(Int32 #=zigiUfwfiEelO, Int32 #=z9aDDAXe4HEDt, #=zNkc__3NRDeBqrjlmbikMZfSF1f39KZpvA$q_mPPGYuC2GRpCZfOgoVs=& #=zIEErsxNw3YsY)
在 Aspose.Pdf.Text.TextSegment.#=zs46i62qtyGuLpzKBkw==()
在 Aspose.Pdf.Text.TextFragment.#=zUDT3$bXgdBAwS_QlAUejpGc=()
在 Aspose.Pdf.Text.TextFragment.set_Text(String value)
在 UserQuery.Main() 位置 C:\Users\sullivan\AppData\Local\Temp\LINQPad5\_nbdyqycr\query_qdvzge.cs:行号 48
在 LINQPad.ExecutionModel.ClrQueryRunner.Run()
在 LINQPad.ExecutionModel.Server.RunQuery(QueryRunner runner)
在 LINQPad.ExecutionModel.Server.StartQuery(QueryRunner runner)
在 LINQPad.ExecutionModel.Server.<>c__DisplayClass151_0.<ExecuteClrQuery>b__0()
在 LINQPad.ExecutionModel.Server.SingleThreadExecuter.Work()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()

@sullivan

We are checking it and will get back to you shortly.

thank you! :kissing_heart:

@sullivan

We were able to notice the similar exception in our environment. Hence, an issue as PDFNET-51572 has been created in our issue tracking system. We will further look into its details and keep you posted with the status of its rectification. Please be patient and spare us some time.

We are sorry for the inconvenience.

Has PDFNET-51572 been fixed?
I have one PDF where I get a NullReferenceException when replacing a text with a new text, when the new text contains a point or comma, but it works when the new text is a single word. It seems to affect only some PDF created by InDesign, but test-documents created with Microsoft Word work. Is this issue related?

@Benjamin_Freitag

No, the earlier logged ticket has not been yet resolved sadly. Would you please share your sample PDF as well? We will log a dedicated issue for it and share the ticket ID with you.

Thanks @asad.ali for your feedback. I shared our PDF in a private message to you.

Interestingly I can just wrap textFragment.Text = targetText; in a try-catch and everything seems to work well; so maybe it’s caused by some paragraph-optimizations which are not relevant in our case.

@Benjamin_Freitag

We tested your PDF with 23.12 version of the API and did not notice any issues in our environment. Please check the below code snippet and attached output generated in our environment:

string DocPath = dataDir + @"Callgeld.pdf";
Document pdfDocument = new Document(DocPath);
string searchParagraphText = @"ZinsChf";
string repParagraphSearch = @"1.2345";
string regSearchText = searchParagraphText.Replace(" ", @"\s*");

foreach (Page page in pdfDocument.Pages)
{
    var textFragmentAbsorber = new TextFragmentAbsorber("(?i)(" + regSearchText + ")", new TextSearchOptions(true));
    textFragmentAbsorber.TextReplaceOptions = new TextReplaceOptions(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation);
    page.Accept(textFragmentAbsorber);
    TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
    foreach (TextFragment textFragment in textFragmentCollection)
    {
        textFragment.Text = repParagraphSearch;
    }
}
string newDocPath = dataDir + @"Callgeld_out.pdf"; //New Generate Pdf
pdfDocument.Save(newDocPath);

Callgeld_out.pdf (97.7 KB)

Would you please try using the latest version and let us know if issue still persists.

@asad.ali your code snipped works, but as soon as I change repParagraphSearch = @"1.2345"; to repParagraphSearch = @"1.234"; (one decimal place less) I get this exception:

System.NullReferenceException at #=zsQRKjjRQ2B7PQqWjEV$RcLW8gWtZNEh4kw==.#=zoGNRJAzImkGbUUo7KA==(#=zqw9ISzvB3tQKO7TL$dspb9S8X4865xHNVrCHNeI1oiwZrjx9NpV5aSo= #=zYckbUQrNUhNV) at #=zsQRKjjRQ2B7PQqWjEV$RcLW8gWtZNEh4kw==.#=zBRM5K4HBcVgOfLnv5A==() at Aspose.Pdf.Text.TextFragment.#=zkDzW84pnqukH9oq$1OIxUF4=(Double #=z_WjsE8mxoEZq) at Aspose.Pdf.Text.TextFragment.set_Text(String value)

Tested with Aspose.PDF Version 23.12.0.

@Benjamin_Freitag

We are still not able to replicate the issue. Can you please make sure that all Windows Fonts are installed and present in your system? In case you are working in non-Windows environment, please make sure to install msttcorefonts package and try again. Please feel free to let us know the details of your environment if issue still persists.
Callgeld_out.pdf (97.1 KB)

@asad.ali All Windows Fonts are installed and present in my system (as OpenType font). I’m working in a Windows 11 environment with ASP.Net 4.8 and IIS Express. The issue occurs whether the fonts are installed or not. I’ll send you the font as private message.

@Benjamin_Freitag

Are you referencing the assembly from GAC? Can you please confirm?

@Benjamin_Freitag

Can you please share a sample application for our reference that we can use to reproduce the issue? Also, we checked the fonts that you shared and it did not help to replicate the issue.

@asad.ali here’s the sample application, having a reference to the NuGet Package. Run it in Visual Studio 2022 (17.8.5), IIS Express, and open https://localhost:44375/api/Pdf

AsposePdfTest.zip (8.1 KB)

@sullivan

We tested your project. Had to set the <HintPath> in project file to run it. We were able to notice the issue in our environment using the latest version of the API.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-56333

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

The issues you have found earlier (filed as PDFNET-56333) have been fixed in Aspose.PDF for .NET 24.12.