Citations (Bounding Boxes)

In addition to displaying citations in the UI, we include location references (when available) in the API response payload for workflow run outputs. The format of these references depends on your processor’s configuration type.

Importantly, these references are not the same as traditional OCR and we do not make a guarantee that 100% of the time our system is able to detect and return them for all extracted values. However, we are constantly improving our models and will continue to do so.

Right now location references are only available for Extract output fields, and are only supported for the following file/document types:

  • PDF
  • IMG (jpeg, png, etc)

Citation Schema

Citations are a way to reference a specific location in a document where a field is located. Citations are returned in the metadata object for each field. Learn more about Metadata here.

The polygon and referenceText fields are only returned if the citationsEnabled option is enabled in the processor config. This can be enabled through the Studio in the Build tab under “Advanced options”.

The shape of the polygon is as follows:

1export type Citation = {
2 page?: number;
3 referenceText?: string | null;
4 polygon?: Point[];
5};
6
7type Point = {
8 x: number;
9 y: number;
10};

How to use citations

How to use the bounding values in order to place a bounding box on a file, depends heavily on the file type. In general though, you will need to take the bounding box values we return and convert them to whatever coordinate system your rendering library uses or what you define if you are using a native Canvas element approach to drawing them over an image for instance.

The values we return in the polygon field represent the coordinates of the bounding box on the image or page. They indicate the points of the polygon that form the bounding box. You may need to convert these values into a format suitable for your rendering library. If your rendering library uses a coordinate system based on percentages of the total image or page size, you would need to perform an additional conversion step.

For PDFs, here is an example of how to apply a transform to the value in order to be rendered using react-pdf-viewer or a similar library:

1export type HighlightArea = {
2 left: number;
3 top: number;
4 width: number;
5 height: number;
6 pageIndex: number;
7};
8
9function convertPolygonToHighlightArea({
10 polygon,
11 pageIndex,
12 pageHeight,
13 pageWidth,
14}: {
15 polygon: Point[];
16 pageIndex: number;
17 pageHeight: number;
18 pageWidth: number;
19}): HighlightArea {
20 const boundingBox = polygon.reduce(
21 (acc, curr) => {
22 return {
23 left: Math.min(acc.left, curr.x),
24 top: Math.min(acc.top, curr.y),
25 right: Math.max(acc.right, curr.x),
26 bottom: Math.max(acc.bottom, curr.y),
27 };
28 },
29 { left: 0, top: 0, right: 0, bottom: 0 }
30 );
31
32 // If you are able to access the page height and width of the document, use them to calculate the highlight area
33 if (pageHeight && pageWidth) {
34 const highlightArea: HighlightArea = {
35 height: ((boundingBox.bottom - boundingBox.top) / pageHeight) * 100 + 2,
36 width: ((boundingBox.right - boundingBox.left) / pageWidth) * 100 + 2,
37 top: (boundingBox.top / pageHeight) * 100 - 1,
38 left: (boundingBox.left / pageWidth) * 100 - 1,
39 pageIndex,
40 };
41 return highlightArea;
42 }
43
44 const DPI = 72; // Default DPI - calibrate this based on your product
45
46 // Otherwise you can default to a conversion based on the zoom level of the document
47 const highlightArea: HighlightArea = {
48 left: boundingBox.left * 9,
49 top: boundingBox.top * 9,
50 width: ((boundingBox.right - boundingBox.left) * DPI) / 8,
51 height: ((boundingBox.bottom - boundingBox.top) * DPI) / 8,
52 pageIndex,
53 };
54 return highlightArea;
55}