Get/Set custom PDF property

Hi,

I am currently migrating existing functionality from another 3rd party component to Aspose. What I need is to get/set a custom property of a pdf document. What I have found so far is:

var document = new Aspose.Pdf.Document(filename);

document.Metadata.RegisterNamespaceUri("xmp", "http://ns.adobe.com/xap/1.0/");
document.Metadata["xmp:IsWatermarked"] = "true";

var watermarked = document.Metadata.Contains("xmp:IsWatermarked");

This works fine, but the problem is that we marked many documents with IsWatermarked and not with xmp:IsWatermarked. Is it possible to get/set a custom property without the xmp prefix?

@profiler

To get or set a custom property in a PDF document without using the xmp prefix, you can directly manipulate the metadata of the document. While the Aspose.PDF library typically uses the xmp namespace for standard properties, you can still add custom properties without the prefix.

Here’s how you can do it:

  1. Setting a Custom Property: You can simply assign a value to a property name without the xmp prefix.

    var document = new Aspose.Pdf.Document("yourfile.pdf");
    
    // Set a custom property without the xmp prefix
    document.Metadata["IsWatermarked"] = "true";
    
    // Save the document
    document.Save("yourfile_with_custom_property.pdf");
    
  2. Getting a Custom Property: Similarly, you can retrieve the value of a custom property by using its name directly.

    var document = new Aspose.Pdf.Document("yourfile_with_custom_property.pdf");
    
    // Get the custom property value
    var isWatermarked = document.Metadata["IsWatermarked"];
    Console.WriteLine($"Is Watermarked: {isWatermarked}");
    

This approach allows you to manage custom properties without the need for the xmp prefix, making it easier to maintain consistency with your existing documents that were marked with properties like IsWatermarked.

If you need further assistance or examples, feel free to ask!

Sources:
[1]: Get PDF Properties | Aspose.PDF for .NET API Reference

I got the following exception with this approach:

System.ArgumentException: ‘Key is not valid.’

@profiler

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

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.