In DotNet, how can I add a flag to an `XImage` in a pdf File?

Hi there!
I’m working with an existing PDF file and need to add a flag or marker to one of its images so that I can easily locate it later, when the PDF is modified.

I tried renaming the image, but that ended up throwing an ArgumentNullException.
I was also hoping to add metadata to the image, but it seems like the Metadata property is read-only and always null unless the image was added programmatically using Aspose.

Could someone help clarify if I’m missing something, or suggest an alternative approach to mark the image?

@jadenwu39

If you are looking for some marker or pointer to locate the image later, you can use its position and page number for it. Later you can extract images from PDF and locate your target image by checking and matching the position and page number. Please check below example of extracting images:

Hi asad.ali, and Ramadan Kareem!

Thank you so much for getting back to me. I really appreciate your help. Unfortunately, using the image’s position doesn’t quite fit my needs. I need to allow users to add extra text, thus shifting the target image position or additional images to the PDF, and sometimes they might add an image in the same spot as the one I’m trying to mark. My plan is to locate the target image and move it to a place on the page without content—or even on top of the user-added image if needed.

It would be a lot easier if I could add a marker using the image’s Name or Metadata. I tried using reflection to set an instance to the null Metadata property of the target XImage and added a marker to its Metadata dictionary, but unfortunately, it doesn’t save when I save the PDF document.

using var pdfDocument = new Aspose.Pdf.Document(pdfStream);
var img = pdfDocument.Pages.Last().Resources.Images.Last();

var metadataInstance = Activator.CreateInstance(typeof(Metadata), nonPublic: true) as Metadata;
var propertyInfo = typeof(XImage).GetRuntimeFields().FirstOrDefault(x => x.FieldType == typeof(Metadata));
propertyInfo.SetValue(img, metadataInstance);

img.Metadata.RegisterNamespaceUri("flag", "CustomImageMarker");
img.Metadata["flag:IsTarget"] = "true";

pdfDocument.Save(@"\.Output.pdf");

Any ideas or suggestions would be greatly appreciated. Thanks again for your time and assistance!

@jadenwu39

We need to perform detailed investigation about this feature. Hence, 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-59588

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.