# Annotation's getContext() always returns undefined for text annotations

**URL:** <https://pdfjs.community/t/annotations-getcontext-always-returns-undefined-for-text-annotations/2210>\
**Category:** Technical Support\
**Tags:** pdfjs-express\
**Created:** [November 29, 2022, 2:45pm UTC](https://pdfjs.community/t/annotations-getcontext-always-returns-undefined-for-text-annotations/2210 "2022-11-29T14:45:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![polina.meleca](https://avatars.discourse-cdn.com/v4/letter/p/edb3f5/32.png) [@polina.meleca](https://pdfjs.community/u/polina.meleca)\
**Post date:** [November 29, 2022, 2:45pm UTC](https://pdfjs.community/t/annotations-getcontext-always-returns-undefined-for-text-annotations/2210/1 "2022-11-29T14:45:16Z")

</div>

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

**PDF.js Express Version**  
8.4.1

**Detailed description of issue**  
Whenever I try to access the selected text for a text annotation using annotation.getContext(), it returns undefined. However I’m able to get this text using annotation.getCustomData(‘trn-annot-preview’)

**Expected behaviour**  
annotation.getContext() returns the selected text snippet

**Does your issue happen with every document, or just one?**  
It happens with every document

**Link to document**  
Any PDF document

**Code snippet**

```javascript
import WebViewer from '@pdftron/pdfjs-express';

function loadDocumentViewer(blob, params, viewer) {
    WebViewer(
        params,
        viewer.nativeElement
    ).then((instance) => {
        instance.UI.loadDocument(...);
        customizeAnnotationsPanel(instance);
    });
}

function customizeAnnotationsPanel(viewerInstance) {
    viewerInstance.UI.dangerouslySetNoteTransformFunction(
        (wrapper, state, createElement) => {
            const div = createElement('div');
            const copySnippetButton = createElement('button');

            copySnippetButton.onclick = () => {
                copySnippet(state);      
            };

            div.appendChild(copySnippetButton);
            wrapper.appendChild(div);
        }
    );
}

function copySnippet(state) {
    const annotationMetadata = {
        source_text_snippet: state.annotation.getContents(),
    };
    navigator.clipboard.writeText(JSON.stringify(annotationMetadata));
}

```

---

<div class="post-metadata">

**Author:** ![tgordon](https://avatars.discourse-cdn.com/v4/letter/t/aca169/32.png) [@tgordon](https://pdfjs.community/u/tgordon)\
**Post date:** [November 29, 2022, 6:07pm UTC](https://pdfjs.community/t/annotations-getcontext-always-returns-undefined-for-text-annotations/2210/3 "2022-11-29T18:07:01Z")

</div>

Hello polina.meleca,

I was able to reproduce getContents() returning undefined, we have added this to our backlog.

Best regards,  
Tyler Gordon  
Web Development Support Engineer  
PDFTron

Share how you are using PDF.js Express in your organization you could win a **$500 Amazon gift card. All participants will receive 6 months of PDF.js Express+ for free**. Learn more [here](https://utm.guru/ueQNw)

---

<div class="post-metadata">

**Author:** ![ons.othmen](https://avatars.discourse-cdn.com/v4/letter/o/6de8d8/32.png) [@ons.othmen](https://pdfjs.community/u/ons.othmen)\
**Post date:** [January 9, 2023, 10:10am UTC](https://pdfjs.community/t/annotations-getcontext-always-returns-undefined-for-text-annotations/2210/4 "2023-01-09T10:10:55Z")

</div>

@tgordon I am writing to follow up on the issue reported by my colleagues, has it been adddressed?  
Thanks,  
Ons.

---

<div class="post-metadata">

**Author:** ![tgordon](https://avatars.discourse-cdn.com/v4/letter/t/aca169/32.png) [@tgordon](https://pdfjs.community/u/tgordon)\
**Post date:** [January 11, 2023, 12:56am UTC](https://pdfjs.community/t/annotations-getcontext-always-returns-undefined-for-text-annotations/2210/5 "2023-01-11T00:56:45Z")

</div>

Hello ons.othmen,

Looks like it has been fixed, can you try with the latest build?  
 ![image](https://canada1.discourse-cdn.com/flex031/uploads/pdfjscommunity/original/2X/8/827dedd524667b85cf94fc211e2f97522a7c466c.png)

Best regards,  
Tyler Gordon

---

<div class="post-metadata">

**Author:** ![polina.meleca](https://avatars.discourse-cdn.com/v4/letter/p/edb3f5/32.png) [@polina.meleca](https://pdfjs.community/u/polina.meleca)\
**Post date:** [April 26, 2023, 3:48pm UTC](https://pdfjs.community/t/annotations-getcontext-always-returns-undefined-for-text-annotations/2210/6 "2023-04-26T15:48:31Z")

</div>

The contents is still blank for 8.7.0. We create annotations using text highlight tool.

---

<div class="post-metadata">

**Author:** ![tgordon](https://avatars.discourse-cdn.com/v4/letter/t/aca169/32.png) [@tgordon](https://pdfjs.community/u/tgordon)\
**Post date:** [May 1, 2023, 10:26pm UTC](https://pdfjs.community/t/annotations-getcontext-always-returns-undefined-for-text-annotations/2210/7 "2023-05-01T22:26:04Z")

</div>

Hello polina.meleca,

This thread is specifically for the FreeText tool, in your case you’re using the highlighting tool

Best regards,  
Tyler

---

<div class="post-metadata">

**Author:** ![polina.meleca](https://avatars.discourse-cdn.com/v4/letter/p/edb3f5/32.png) [@polina.meleca](https://pdfjs.community/u/polina.meleca)\
**Post date:** [May 2, 2023, 5:34pm UTC](https://pdfjs.community/t/annotations-getcontext-always-returns-undefined-for-text-annotations/2210/8 "2023-05-02T17:34:55Z")

</div>

Sorry for unclear description, but the initial problem was for the AnnotationCreateTextHighlight tool
