Posted 18 June 2025, 8:18 pm EST
How do you get the start and end index (char) of the selected text in the pdf document?
Forums Home / Document Solutions / Document Solutions for PDF
Posted by: gsnow on 18 June 2025, 8:18 pm EST
Posted 18 June 2025, 8:18 pm EST
How do you get the start and end index (char) of the selected text in the pdf document?
Posted 20 June 2025, 1:19 am EST - Updated 20 June 2025, 1:24 am EST
Hi,
In DsPdfViewer, the concept of start and end character indices does not directly apply like it would in plain-text editors, because PDF documents are not stored as linear text. Instead, selected text is based on visual regions (bounding rectangles) rather than index positions.
However, it is possible to extract the visual region of the selected text using the viewer.selectionCopier object.
<button onclick="getPDFSelection()">Get Selection</button>
<div id="host"></div>
<script>
var viewer = new DsPdfViewer("#host");
viewer.addDefaultPanels();
function getPDFSelection() {
console.log(viewer.getSelectedText()); // Get the selected text
console.log(viewer.selectionCopier); // Get the selected text information
}
</script>
So while you can’t get character-level start/end indices, you can identify exactly where the selection is visually on the PDF, which is how DsPdfViewer internally represents and processes selections.
You can further refer to the attached sample that uses the above code snippet and gets the bounding rectangles containing the selected text (see below).
Please let us know if you need any further guidance. If you could share more about your use case, we can provide further assistance tailored to your requirements.
Regards,
Prabhat Sharma.
Posted 23 June 2025, 5:12 pm EST
Many thanks for your feedback. I was hoping to store the start and end index of the highlighted text so I could use the “highlightTextSegment” method to re-highlight text that had been previously stored. I want to store these highlighted text segments outside of the document. It is much more cumbersome to store all the bounding rectangles.
Posted 24 June 2025, 4:10 am EST - Updated 24 June 2025, 4:15 am EST
Hi,
Thanks for sharing your user story.
While DsPdfViewer does not provide a direct method to retrieve the start and end indices of selected text, it is possible to extract them using the following approach:
Please refer to the attached sample, which demonstrates this approach and highlights the selected text as expected (see below).
Please note that this is a proof-of-concept sample. If the same text appears multiple times on a page, additional logic may be required to compare the selection rectangles and accurately identify the intended text segment.
Let us know if any further assistance is needed.
Kind Regards,
Chirag Gupta
Attachment: TextSelection.zip
Posted 10 July 2025, 7:07 pm EST
Many thanks for your feedback! Easily the best I have received on any developer forum! I can’t thank you enough!!!
I have a couple more questions:
Posted 14 July 2025, 1:53 am EST
Hi,
We are delighted that the previous solution worked for your use case.
Regarding the follow-ups:
window.onload = function () {
var viewer = new DsPdfViewer('#viewer', { restoreViewStateOnLoad: false });
viewer.addDefaultPanels();
var pdf = "/path/to/your/file.pdf";
loadPdf(viewer, pdf, "shorelines");
}
async function loadPdf(viewer, pdf, searchText) {
await viewer.open(pdf);
var findOptions = {
Text: searchText,
MatchCase: true,
WholeWord: true
};
const searchIterator = await viewer.searcher.search(findOptions);
const result = await searchIterator.next();
const item = result.value;
// Use bounding box of the found text to calculate scroll position
const x = item.ItemArea.left;
const y = item.ItemArea.top + item.ItemArea.height;
// Scroll to the text position with optional padding
viewer.loadAndScrollPageIntoView(item.PageIndex, [
null,
{ name: 'XYZ' },
x - 10,
y + 10,
1.0 // Zoom factor (1.0 = 100%)
]);
}
You can adapt this to scroll to any previously stored location — for example, from saved highlight metadata — by storing the page index and bounding rectangle or offset coordinates.
References:
Please let us know if you require any further assistance.
Kind Regards,
Chirag Gupta