How get a Rect that contains all the quads from TextSearch class?

Question:

How to convert quads to a Rect?

Answer:

Here is C# code, other languages should be sufficiently similar. The following code takes Orientated Bounding Boxes (OBB) quads to a single Axis Aligned Bounding Box (AABB) Rect instance.

This code would be useful for taking quads from TextSearch or viewer highlights to create an annotation. To create a PDF annotation you need a Rect that is a union of all the quads.

// Create a AABB Rect that is a union of all the OBB quads passed in
pdftron.PDF.Rect GetRectFromQuads(double[] quads)
{
pdftron.PDF.Rect result = null;
if(quads.Length % 8 != 0) throw Exception("incorrect number of quad numbers");
int numQuads = quads.Length / 8;
for(int quadNumber = 0; quadNumber < numQuads; ++quadNumber)
{
double x1 = quads[quadNumber + 0];
double y1 = quads[quadNumber + 1];

double x2 = quads[quadNumber + 2];
double y2 = quads[quadNumber + 3];

double x3 = quads[quadNumber + 4];
double y3 = quads[quadNumber + 5];

double x4 = quads[quadNumber + 6];
double y4 = quads[quadNumber + 7];

double rectX1 = Math.Min(Math.Min(Math.Min(x1, x2), x3), x4);
double rectX2 = Math.Max(Math.Max(Math.Max(x1, x2), x3), x4);
double rectY1 = Math.Min(Math.Min(Math.Min(y1, y2), y3), y4);
double rectY2 = Math.Max(Math.Max(Math.Max(y1, y2), y3), y4);

if(result == null)
{
result = new pdftron.PDF.Rect(rectX1, rectY1, rectX2, rectY2);
}
else
{
result.x1 = Math.Min(result.x1, rectX1);
result.y1 = Math.Min(result.y1, rectY1);
result.x2 = Math.Max(result.x2, rectX2);
result.y2 = Math.Max(result.y2, rectY2);
}
}
return result;
}

Here is JAVA code for the same.

// Create a AABB Rect that is a union of all the OBB quads passed in
static com.pdftron.pdf.Rect getRectFromQuads(double[] quads) throws Exception
{
    com.pdftron.pdf.Rect result = null;
    if(quads.length % 8 != 0) throw new Exception("incorrect number of quad numbers");

    int numQuads = quads.length / 8;
    for(int quadNumber = 0; quadNumber < numQuads; ++quadNumber)
    {
        double x1 = quads[quadNumber + 0];
        double y1 = quads[quadNumber + 1];

        double x2 = quads[quadNumber + 2];
        double y2 = quads[quadNumber + 3];

        double x3 = quads[quadNumber + 4];
        double y3 = quads[quadNumber + 5];

        double x4 = quads[quadNumber + 6];
        double y4 = quads[quadNumber + 7];

        double rectX1 = Math.min(Math.min(Math.min(x1, x2), x3), x4);
        double rectX2 = Math.max(Math.max(Math.max(x1, x2), x3), x4);
        double rectY1 = Math.min(Math.min(Math.min(y1, y2), y3), y4);
        double rectY2 = Math.max(Math.max(Math.max(y1, y2), y3), y4);

        if(result == null)
        {
            result = new com.pdftron.pdf.Rect(rectX1, rectY1, rectX2, rectY2);
        }
        else
        {
            result.setX1(Math.min(result.getX1(), rectX1));
            result.setY1(Math.min(result.getY1(), rectY1));
            result.setX2(Math.max(result.getX2(), rectX2));
            result.setY2(Math.max(result.getY2(), rectY2));
        }
    }
    return result;
}