Add stamp to pdf is not working the second time - part 2

A while back I already wrote about this issue:

We discovered that it works with the newest version of aspose pdf. But the problem is that it requires framework 4.8.1 which in turn requires Windows server 2022 which our clients cannot install.

Your newest version still supporting 4.8 framework is 24.3 but stamping pdfs multiple times doesn’t work on that version.

I am sending link do example again (description is the same as in original topic): Send Anywhere

What do you recommend?

@Nejca

You can still use the latest version with the .NET Framework >4.8.1. You just need to download the supported DLLs from below link:

Thank you, but it doesn’t work with this DLL either. It looks like that in this case there is difference between framework 4.8 and 4.8.1

Do we have any other options? Are there other ways to add images to pdf?

We also tried: Add Image to PDF using C#|Aspose.PDF for .NET

But the issue is the same (and as a bonus, converting to pdf/a format later returns “Stack empty” error)

@Nejca

Is it possible for you to share some files and the code snippet you tried to add stamp in the PDF? We will evaluate it at our side and share our findings with you.

Here is the code that doesn’t work: Send Anywhere

Description form original post:
We are adding stamps to pdf. We are using fragments as “bookmarks” to locate the location of the stamp.
First time when we do it there is no issue. Stamp is added without a problem. We can add multiple stamps and they are all added without issue.

But as soon as we save the pdf document and load it again and try to add another stamp it doesn’t work. There is no stamp on the pdf.

This is an example of the code. In Data there is Podpis.pdf (original file for testing) and PodpisTest.pdf that is a result of the test.

@Nejca

We downloaded 24.7 DLL from the downloads section of our website (we have shared its link already with you) and tested the case using below code in Program.cs file:

string path = @"C:\Users\Downloads\AsposeStampProblem\Data\Podpis.pdf";
var file = File.ReadAllBytes(path);
var stampedFile = AsposeWrapper.AddStamp(file, "##Podpis1", "Test text");
File.WriteAllBytes(path, stampedFile);
var newFile = File.ReadAllBytes(path);
var stampedFile2 = AsposeWrapper.AddStamp(newFile, "##Podpis2", "Test2 text2");
File.WriteAllBytes(path, stampedFile2);

Podpis.pdf (171.3 KB)

Attached is the output PDF that we obtained and we could not replicate any issue or exception. Would you please try using 24.7 version and let us know in case you still notice any issues?

Sorry, I made a mistake. We tested on a different PDF then it was in example. We thought it was the same problem, but it was a little different. We also discovered that the problem is not on all PDFs.

Adding Stamps at different times is working now, but adding multiple stamp at the same time now doesn’t work on some PDFs.

In example with added three textStamps at the same time and only the last one is visible (same code work on other PDFs). If you remove any of the two added stamp or add them seperatly, then it work like it should.

What could be the problem?

Here is an example with problematic PDF: Send Anywhere

@Nejca

Please check the attached output PDF that we obtained in our environment while testing the case with 24.7 version of the API.
PodpisTest.pdf (241.4 KB)

We can see that there are two image stamps in there. Can you please try the 24.7 version at your side and let us know if that helps?

Image stamp are ok. They are not a problem. Problem are text stamps added at the same time (without saving to stream in between).

Your result looks different then mine, textstamps are added but at random location and they are added multiple times (but it looks like they weren’t added properly in your example either - check the text above images, there should be three text stamps not just one).

I assume you used different code. Can you please check method AddTextStamp. That is the one not working on our case with this pdf and 24.7 version.

Problem is adding three text stamps in the same instance. Only the last one appears.

See this example. Images are ok, but there should three text stamps above them. But they are not.
PodpisTest.pdf (240.6 KB)

This is how it should be. I added this ones seperatly (with saving in between). But we can’t use this method because in some PDFs we have to add a lot of text and saving to stream after every text stamp would cause preformance issues (even our little test tool is working noticibly slower in this scenario).
PodpisTestProper.pdf (395.2 KB)

@Nejca

The text stamps were not on correct position because they were being added on 1st page instead of 2nd. We changed the code snippet and specified 2nd page to add the text stamps. Please check the image attached.
image.png (154.3 KB)

After that, we also noticed an Argument Exception at the line where you are setting fragment.text = null;. When we ignored the exception by surrounding with with try/catch, we obtained attached output that looks exactly like the expected one.
image.png (160.7 KB)

PodpisTest.pdf (240.5 KB)

Can you please check at your side with the same changes so that we can be on the same page? We will log this issue in our internal issue tracking system and address it accordingly.

Try this code: Send Anywhere
I stripped it only to bare minimum to identify the problem.

You should only get Argument Exception if you run code for the second time on the same file. Then it’s not a valid test anyway, because bookmarks are deleted in the first run. In this case delete file PodpisTest.pdf, copy file Podpis.pdf and name the copy PodpisTest.pdf. Then run it again.

This is the result we get:
PodpisTest.pdf (175.1 KB)

Issue is two missing texts here:
MissingTexts.PNG (17.3 KB)

@Nejca

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-57794

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.