# Question about authorId when making annotations

**URL:** <https://pdfjs.community/t/question-about-authorid-when-making-annotations/1251>\
**Category:** Technical Support\
**Created:** [December 14, 2021, 1:04am UTC](https://pdfjs.community/t/question-about-authorid-when-making-annotations/1251 "2021-12-14T01:04:29Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![andy](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@andy](https://pdfjs.community/u/andy)\
**Post date:** [December 14, 2021, 1:04am UTC](https://pdfjs.community/t/question-about-authorid-when-making-annotations/1251/1 "2021-12-14T01:04:29Z")

</div>

**Which product are you using?**  
PDF.js Express Plus

**PDF.js Express Version**  
8.1.0

**Detailed description of issue**  
We are preparing to setup a real-time collaboration for annotations. However I am a bit confused because I don’t see how or where you would set a users id into an annotation. I only see `annotationUser` as a setting on WebViewer but this is how you set the authors name. In your example [Real-time PDF collaboration](https://pdfjs.express/documentation/samples/realtime-collaboration) I see a reference to an author id being used in the server `annotation.authorId = data.val().authorId;` but this isn’t the same thing as setting an authorId right when the webviewer loads.

**Code snippet**  
Your example shows this:

```javascript
async function onAnnotationCreated(data) {
    // Import the annotation based on xfdf command
    const [annotation] = await annotationManager.importAnnotCommand(data.val().xfdf);
    // Set a custom field authorId to be used in client-side permission check
    annotation.authorId = data.val().authorId;
    annotationManager.redrawAnnotation(annotation);
 }

```

Guess I am confused because why can’t I simply set an author id directly on the webviewer like this?

```javascript
WebViewer({
    path: '...'
    annotationUser: 'Andy H.',
    authorId: 'idNumber'
})

```

Am I needing to manually add an authorId field into the XML for the annotation?

Please let me know thanks!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex031/uploads/pdfjscommunity/original/1X/ffbcd25c7d423db8d5acd5db201c272f4db306dd.png) [@system](https://pdfjs.community/u/system)\
**Post date:** [December 14, 2021, 1:04am UTC](https://pdfjs.community/t/question-about-authorid-when-making-annotations/1251/3 "2021-12-14T01:04:35Z")

</div>

**Hello, I’m Ron, an automated tech support bot 🤖**

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:
- [Customize display authors for annotations - Map a userId to a Display Author](https://pdfjs.express/documentation/annotation/display-authors#map-a-userid-to-a-display-author)
- [Annotation permissions - Default behavior](https://pdfjs.express/documentation/annotation/annotation-permissions#default-behavior)
- [PDF collaboration with PDF.js Express](https://pdfjs.express/documentation/collaboration/overview)

APIs:
- [CoreControls.AnnotationManager - getDisplayAuthor(annotation)](https://pdfjs.express/api/7.0/CoreControls.AnnotationManager.html#getDisplayAuthor__anchor)
- [Core.Annotations.CustomAnnotation - new CustomAnnotation(type [, initializer])](https://pdfjs.express/api/Core.Annotations.CustomAnnotation.html#CustomAnnotation__anchor)
- [Core.Annotations.RedactionAnnotation - new RedactionAnnotation( [options])](https://pdfjs.express/api/Core.Annotations.RedactionAnnotation.html#RedactionAnnotation__anchor)

Forums:
- [Old data to be extract from XFDF file and to store in firebase | Real Time Collaboration](https://pdfjs.community/t/old-data-to-be-extract-from-xfdf-file-and-to-store-in-firebase-real-time-collaboration/1184/5)
- [Unable to save the transparency to fillColor](https://pdfjs.community/t/unable-to-save-the-transparency-to-fillcolor/242/2)
- [Can I work with my database or other approaches for real-time annotation instead of firebase?](https://pdfjs.community/t/can-i-work-with-my-database-or-other-approaches-for-real-time-annotation-instead-of-firebase/842/3)

---

<div class="post-metadata">

**Author:** ![andy](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@andy](https://pdfjs.community/u/andy)\
**Post date:** [December 14, 2021, 8:30pm UTC](https://pdfjs.community/t/question-about-authorid-when-making-annotations/1251/4 "2021-12-14T20:30:57Z")

</div>

Also I noticed the real-time example doesn’t discuss how to differentiate imported annotations. Example would be me and another person had a discussion which was stored in the database. I return a week later to import these annotations, I then don’t have any way of knowing which ones are mine other than the author name, which we would not want to use since someone could change their name in the future.

What would be your solution for this?

---

<div class="post-metadata">

**Author:** ![Logan](https://yyz1.discourse-cdn.com/flex031/user_avatar/pdfjs.community/logan/32/13_2.png) [@Logan](https://pdfjs.community/u/Logan)\
**Post date:** [December 15, 2021, 5:38pm UTC](https://pdfjs.community/t/question-about-authorid-when-making-annotations/1251/5 "2021-12-15T17:38:30Z")

</div>

Hey!

You can set the current user by calling [setCurrentUser](https://pdfjs.express/api/Core.AnnotationManager.html#setCurrentUser__anchor). You can provide any string here so you could use their username, user id, etc. If you use the user Id, you can use [setAnnotationDisplayAuthorMap](https://pdfjs.express/api/Core.AnnotationManager.html#setAnnotationDisplayAuthorMap__anchor) to make sure their username appears in the viewer when creating annotations.

Thanks,  
Logan

---

<div class="post-metadata">

**Author:** ![andy](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@andy](https://pdfjs.community/u/andy)\
**Post date:** [December 15, 2021, 6:26pm UTC](https://pdfjs.community/t/question-about-authorid-when-making-annotations/1251/6 "2021-12-15T18:26:38Z")

</div>

Hey Logan, okay I understand what you mean here. But how does this reference loading in saved annotations from multiple users?

---

<div class="post-metadata">

**Author:** ![Logan](https://yyz1.discourse-cdn.com/flex031/user_avatar/pdfjs.community/logan/32/13_2.png) [@Logan](https://pdfjs.community/u/Logan)\
**Post date:** [December 15, 2021, 7:27pm UTC](https://pdfjs.community/t/question-about-authorid-when-making-annotations/1251/8 "2021-12-15T19:27:30Z")

</div>

Hey there,

I’m not sure what you mean by that, can you elaborate?

---

<div class="post-metadata">

**Author:** ![andy](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@andy](https://pdfjs.community/u/andy)\
**Post date:** [December 15, 2021, 7:42pm UTC](https://pdfjs.community/t/question-about-authorid-when-making-annotations/1251/9 "2021-12-15T19:42:29Z")

</div>

Yes, so if I open a document and import 20 saved annotation from 3 different authors, how do I determine which annotations are mine so that I may still edit them? When annotations are stored they only keep the author name in the xml, so when they are returned it is the only reference we have to use.

Here’s an example of a returned annotation with my own title

```xml
<underline color="#E44234" name="ff397c23-2b0c-2f00-8284-f76aadd89878" title="Andy Herman" subject="Underline"><contents>underlined text</contents></underline>

```

Here’s one with another users title

```xml
<underline color="#E44234" name="ff397c23-2b0c-2f00-8284-f76aadd89878" title="Eric Greggors" subject="Underline"><contents>underlined text</contents></underline>

```

So when these are returned and I need to determine which annotations are mine, I can only reference the titles `title="Andy Herman"` which is fine for a simple setup, but if I ever changed my own name to `Andy Begman`, then I wouldn’t be able to mark these annotation as mine to be able to edit them later.

Let me know if this clears it up.

---

<div class="post-metadata">

**Author:** ![Logan](https://yyz1.discourse-cdn.com/flex031/user_avatar/pdfjs.community/logan/32/13_2.png) [@Logan](https://pdfjs.community/u/Logan)\
**Post date:** [December 16, 2021, 10:37pm UTC](https://pdfjs.community/t/question-about-authorid-when-making-annotations/1251/10 "2021-12-16T22:37:00Z")

</div>

Hey,

If everything is set up correctly, the exported annotation `title` should be the user ID and not the username. You can test using this code:

```javascript
  // Here you should set the user ID, not the user name
  instance.Core.annotationManager.setCurrentUser('abcde');

  // This function maps a user id to a user name for display purposes
  instance.Core.annotationManager.setAnnotationDisplayAuthorMap((id) => {
    if(id === 'abcde') {
      return "Logan"
    }
  })

  instance.Core.annotationManager.addEventListener('annotationChanged', async (annots, action) => {
    // Every time an annotation is added, export it and console log it for debugging purposes
    if(action === 'add') {
      const xfdf = await instance.Core.annotationManager.exportAnnotations({ annotList: annots })
      console.log(xfdf)
    }
  })

```

You’ll see that the “title” property is now the user ID (in our case “abcde”) and not the user name

```javascript
<?xml version="1.0" encoding="UTF-8" ?><xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve"><annots><square page="0" rect="219.590,500.250,501.030,667.260" color="#E44234" flags="print" name="f346b08b-f7b0-e731-325a-c6ceb9bca7e3" title="abcde" subject="Rectangle" date="D:20211216143453-08'00'" creationdate="D:20211216143453-08'00'"/></annots></xfdf>

```

Thanks,  
Logan

---

<div class="post-metadata">

**Author:** ![andy](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@andy](https://pdfjs.community/u/andy)\
**Post date:** [December 16, 2021, 10:57pm UTC](https://pdfjs.community/t/question-about-authorid-when-making-annotations/1251/11 "2021-12-16T22:57:34Z")

</div>

Okay got it, I see what you mean.

Would this mean that I would need to keep track of all possible users to also set their names?

Example:

```javascript
instance.Core.annotationManager.setAnnotationDisplayAuthorMap((id) => {
    if(id === 'abcde') {
      return "Logan"
    } else if (id === 'efght') {
      return "John"
    }
  })

```

We have hundreds of possible users so this would get a bit hairy.

---

<div class="post-metadata">

**Author:** ![andy](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@andy](https://pdfjs.community/u/andy)\
**Post date:** [December 17, 2021, 10:24pm UTC](https://pdfjs.community/t/question-about-authorid-when-making-annotations/1251/12 "2021-12-17T22:24:28Z")

</div>

Can I request to have `authorId` as an optional field added into annotations?

```xml
<highlight title="Andy Herman" authorId="2332323" ...><contents>text</contents></highlight>

```

```javascript
WebViewer({
      path: '', // path to the PDF.js Express'lib' folder on your server
      annotationUser: "Andy Herman",
      annotationUserId: "2332323",

```

---

<div class="post-metadata">

**Author:** ![Logan](https://yyz1.discourse-cdn.com/flex031/user_avatar/pdfjs.community/logan/32/13_2.png) [@Logan](https://pdfjs.community/u/Logan)\
**Post date:** [January 4, 2022, 3:23pm UTC](https://pdfjs.community/t/question-about-authorid-when-making-annotations/1251/13 "2022-01-04T15:23:54Z")

</div>

Hey there and happy new year!

Sorry for the delay here, most the team was on vacation for the holidays.

We cannot add additional attributes to XFDF items because that is against the PDF specification.

I’m talking with the team today to see how they recommend approaching this. The idea with `setAnnotationDisplayAuthorMap` is that you use it to look up the username based on the ID passed. A very basic example would be something like this:

```javascript
  instance.Core.annotationManager.setAnnotationDisplayAuthorMap((id) => {
    return getUserNameFromId(id)
  })

```

where `getUserNameFromId` makes a call to your database or something to get the user name.

However, it seems that this API was poorly designed and does not allow asynchronous actions which means that is not possible.

I’ll get back to you today and let you know the best approach for this.

Logan

---

<div class="post-metadata">

**Author:** ![andy](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@andy](https://pdfjs.community/u/andy)\
**Post date:** [January 20, 2022, 7:50pm UTC](https://pdfjs.community/t/question-about-authorid-when-making-annotations/1251/14 "2022-01-20T19:50:37Z")

</div>

Thanks, we found a work around so no need to further help.

---

<div class="post-metadata">

**Author:** ![Logan](https://yyz1.discourse-cdn.com/flex031/user_avatar/pdfjs.community/logan/32/13_2.png) [@Logan](https://pdfjs.community/u/Logan)\
**Post date:** [January 24, 2022, 6:36pm UTC](https://pdfjs.community/t/question-about-authorid-when-making-annotations/1251/15 "2022-01-24T18:36:07Z")

</div>


