export type HighlightArea = {
left: number;
top: number;
width: number;
height: number;
pageIndex: number;
};
function convertBoundingBoxToHighlightArea({
boundingBox,
pageIndex,
pageHeight,
pageWidth,
}: {
boundingBox: BoundingBox;
pageIndex: number; // The index of the page in the document (0 indexed)
pageHeight: number;
pageWidth: number;
}): HighlightArea {
// If you are able to access the page height and width of the document, use them to calculate the highlight area
if (pageHeight && pageWidth) {
const highlightArea: HighlightArea = {
height: ((boundingBox.bottom - boundingBox.top) / pageHeight) * 100 + 2,
left: (boundingBox.left / pageWidth) * 100 - 1,
width: ((boundingBox.right - boundingBox.left) / pageWidth) * 100 + 2,
top: (boundingBox.top / pageHeight) * 100 - 1,
pageIndex,
};
return highlightArea;
}
const DPI = 72; // Default DPI - calibrate this based on your product
// Otherwise you can default to a conversion based on the zoom level of the document
const highlightArea: HighlightArea = {
left: boundingBox.left * 9,
top: boundingBox.top * 9,
width: ((boundingBox.right - boundingBox.left) * DPI) / 8,
height: ((boundingBox.bottom - boundingBox.top) * DPI) / 8,
pageIndex,
};
return highlightArea;
}