Some test text!

menu
How to save and load a custom property on an annotation?keyboard_arrow_down

How to save and load a custom property on an annotation?

You can extend the serialize and deserialize functions of Annotations.Annotation so that it adds a custom attribute to exported XFDF. For example:

// Save a reference to the old serialize function
const serialize = Annotations.Annotation.prototype.serialize;

Annotations.Annotation.prototype.serialize = function() {
  // call the old serialize function.
  // Returns an Element
  const el = serialize.apply(this, arguments);

  // Add our custom attribute to the element
  if (this.myCustomAttribute) {
    el.setAttribute('attribute-name', this.myCustomAttribute);
  }

  // Make sure to return our new custom element
  return el;
};

// Save a reference to the old deserialize function
const deserialize = Annotations.Annotation.prototype.deserialize;

Annotations.Annotation.prototype.deserialize = function(el) {
  // call the old deserialze function
  deserialize.apply(this, arguments);

  // Apply the custom attribute to the Annotation
  this.myCustomAttribute = el.getAttribute('attribute-name');
};

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.