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

23 views
Skip to first unread message

Ryan

unread,
Mar 31, 2020, 2:59:58 PM3/31/20
to PDFTron PDFNet SDK
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;
}

Reply all
Reply to author
Forward
0 new messages