Skip to content

Navigation Menu

Sign in
Sign up

Highlight text programatically #274

Unanswered
dinchu asked this question in Q&A
Nov 30, 2025 · 1 comments · 1 reply
Discussion options

is possible to highlight text passed programatically when the pdf is loaded?

You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

@dinchu I've been trying to make this work as well. If you know exact coordinates of the text you could create a text highlight with the annotation plugin. However, in my case I don't, I just have the exact text. There does not seem an official way to do it based on pure text match, however, you could combine search and annotation plugins to achieve this, not in a clean way... but possible.

const { provides: search, state: searchState } = useSearch(documentId)
const { provides: annotation, state: annotationState } = useAnnotation(documentId)
....
 // I did not find a way to scope search to just one page, however, search results can be filtered later.
 const matches = await search
 .searchAllPages('Text to match')
 .toPromise()
 
 const matchesPerPage = matches.filter(match => match.pageIndex === highlightPageIndex)
 for (const match of matchesPerPage) {
 annotation.createAnnotation(highlightPageIndex, { 
 type: PdfAnnotationSubtype.HIGHLIGHT, 
 segmentRects: match.rects, 
 //...other properties
 }) 
 }
 
You must be logged in to vote
1 reply
Comment options

@vasylenkoval I tried it, and when I created the Annotation, it didn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /