Import/export annotations

There are a few ways to import or export annotations such as from a file, a database, or a document. There are also more advanced loading options to help with finer control of the data.

Importing and exporting annotations using Salesforce

Another option for importing and exporting annotation is using Salesforce custom objects to store XFDF data if you are hosting WebViewer in the Salesforce platform. You can create custom object in Salesforce setting page or using sfdx command line utility tool. In this example we will create Apex class called AnnotationController.cls which will utilize Salesforce SOQL to store and retrieve XFDF data to and from custom objects.

Create custom object in Salesforce setting page, under Manage Objects section. Here is a sample custom object used in this sample

Apryse Docs Image

Apex class for storing and retrieving annotation XFDF data.

Java

1public with sharing class AnnotationController {
2 @AuraEnabled
3 public static String saveAnnotations(String action, String documentId, String annotationId, String xfdfString) {
4 PDFTronAnnotations__c[] annots = [SELECT Id, Content_Version_Id__c, Annotation_Id__c, xfdfString__c
5 FROM PDFTronAnnotations__c WHERE Annotation_Id__c = :annotationId];
6 Pattern MyPattern = Pattern.compile('<delete>(.*)</delete>');
7 Matcher MyMatcher = MyPattern.matcher(xfdfString);
8 Boolean isDeleteCommand = MyMatcher.matches();
9 if (action == 'delete') {
10 // Delete record
11 delete annots;
12 return 'Success';
13 } else if (annots.size() == 0) {
14 // Create new record
15 PDFTronAnnotations__c newAnnot = new PDFTronAnnotations__c(
16 Content_Version_Id__c=documentId,
17 Annotation_Id__c=annotationId,
18 xfdfString__c=xfdfString
19 );
20 insert newAnnot;
21 return newAnnot.Id;
22 } else {
23 // Update record
24 annots[0].xfdfString__c = xfdfString;
25 update annots;
26 return annots[0].Id;
27 }
28 }
29 @AuraEnabled
30 public static List<Map<String,String>> getAnnotations(String documentId) {
31 List<PDFTronAnnotations__c> temp = [SELECT Id, Content_Version_Id__c, Annotation_Id__c, xfdfString__c FROM PDFTronAnnotations__c WHERE Content_Version_Id__c = :documentId];
32 List<Map<String,String>> annotations = new List<Map<String,String>>();
33 for (Integer i = 0; i < temp.size(); i++) {
34 Map<String,String> annot = new Map<String, String>();
35 annot.put('xfdfString', temp[i].xfdfString__c);
36 annot.put('Annotation_Id', temp[i].Annotation_Id__c);
37 annot.put('ContentVersion_Id', temp[i].Content_Version_Id__c);
38 annotations.add(annot);
39 }
40 return annotations;
41 }
42}
43// Full sample is available at the end of this section.

Sample app in Lightning Web Component to showcase importing and exporting XFDF stirng to Salesforce custom object.

JavaScript

1/** AnnotationController.getAnnotations(id) Apex method */
2import getAnnotations from '@salesforce/apex/AnnotationController.getAnnotations';
3/** AnnotationController.saveAnnotations(params) Apex method */
4import saveAnnotations from '@salesforce/apex/AnnotationController.saveAnnotations';
5export default class WvInstance extends LightningElement {
6 // Snipped for brevity
7 initUI() {
8 // ...
9 const viewer = new PDFTron.WebViewer({...}, viewerElement);
10 viewerElement.addEventListener('ready', () => {
11 this.iframeWindow = viewerElement.querySelector('iframe').contentWindow;
12 })
13 }
14 // Snipped for brevity
15 handleReceiveMessage(event) {
16 if (event.isTrusted && typeof event.data === 'object') {
17 switch (event.data.type) {
18 case 'LOAD_ANNOTATIONS':
19 /**
20 * Retrieve/import annotation for the loaded document
21 *
22 * @param {string} documentId unique id of the loaded document
23 * @returns {string} xfdf string
24 */
25 getAnnotations(event.data.payload)
26 .then(result => {
27 this.iframeWindow.postMessage(
28 { type: 'LOAD_ANNOTATIONS_FINISHED', result },
29 window.location.origin
30 );
31 })
32 break;
33 case 'SAVE_ANNOTATIONS':
34 /**
35 * Storing/exporting XFDF string to Salesforce custom object
36 *
37 * @param {string} action type of action, e.g. delete, update,
38 * @param {string} documentId unique id of the document
39 * @param {string} annotationId id of the annotation
40 * @param {string} xfdfString xfdf string
41 */
42 saveAnnotations(event.data.payload);
43 break;
44 default:
45 break;
46 }
47 }
48 }
49 // Snipped for brevity
50}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales