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

Free Support Forum - aspose.com

How do I get an inserted ImageStamp using its ID?

I have a document where I’m adding an ImageStamp to each page, and I’m giving it properties such as opacity, setBackground, and stampId.

Now, I would like to retrieve the ImageStamp from a page (or pages) using the stampId that I assigned to it. Assuming that the ID assigned to the ImageStamp is 999, how do I do that?

For clarification, I want to retrieve the ImageStamp so that I may change its properties, or outright delete it.

@mohammed.allulu

You can please try using the below code snippet in order to retrieve the stamps using Stamp ID:

// Create PDF content editor.
Facades.PdfContentEditor contentEditor = new Facades.PdfContentEditor();
// Open the temp file.
contentEditor.BindPdf(dataDir + "input.pdf");

// Process all pages.
foreach (Page page in contentEditor.Document.Pages)
{
 // Get the stamp infos.
 Facades.StampInfo[] stampInfos = contentEditor.GetStamps(page.Number);

 // Process all stamp infos
 foreach (Facades.StampInfo stampInfo in stampInfos)
 {
  try
  {
   contentEditor.DeleteStamp(page.Number, new int[] { stampInfo.IndexOnPage });//.DeleteStampById(stampInfo.StampId);
  }
  catch (Exception e)
  {
   Console.WriteLine(e);
  }
 }
}
1 Like

Thank you for the snippet, @asad.ali.

The code snippet you provided seems to be for the .Net version. I forgot to specify that I was using the Java version, my apologies.

I took the snippet and rewrote it in Java, and added contentEditor.save(fileName) at the end, to save the changes to a file, and it worked.

However, I tried using contentEditor.save(contentEditor.getDocument().getFileName()) to save the changes to the same file, but doing so gets me this error when I try to open the file in Adobe Acrobat Reader:
Screenshot 2021-08-24 153106.png (9.1 KB)

Note that this issue only happens when I save the changes to the same file, using contentEditor.save(contentEditor.getDocument().getFileName()). It does not occur when I save the changes to a new file. Do you have any idea why this issue might be occurring?

@mohammed.allulu

It seems like the error is occurring because the file does not get released from contentEditor and saving/overwriting the same file is corrupting the output. A possible solution can be - create a copy stream of the file in the start and initialize the Document object using that stream. Once all operation is done, you can then save the file with the same name to update the existing file. Please let us know if it does not help. We will further investigate the scenario to assist you accordingly.

Following your directions, I’ve managed to make it work.

Much thanks.

1 Like

@mohammed.allulu

It is nice to know that you were able to sort out the issue. Please keep using our API and feel free to create a new topic in case you need any assistance.

1 Like