Selecting text with ctrl a goes wrong

I’m trying to highlight selected text in the pdf. When i select text and push a button, it becomes highlighted, even when i select all text on a page.

But when i select all text on the page with ctrl-a, the highlighting is totally messed up, like the quads are wrong…

public void AddHighlights(PDFViewCtrl.Selection selection)
{
if(m_document != null)
{
int quadNumber = 0;
int pageNumber = selection.GetPageNum();
double[] quads = selection.GetQuads();
int numQuads = quads.Length / 8;

if (quads.Length % 8 == 0)
{

for (int i = 0; i < numQuads; i++)
{
quadNumber = i * 8;

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(quads[quadNumber + 0], quads[quadNumber + 2]), quads[quadNumber + 4]), quads[quadNumber + 6]);
double rectX2 = Math.Max(Math.Max(Math.Max(quads[quadNumber + 0], quads[quadNumber + 2]), quads[quadNumber + 4]), quads[quadNumber + 6]);
double rectY1 = Math.Min(Math.Min(Math.Min(quads[quadNumber + 1], quads[quadNumber + 3]), quads[quadNumber + 5]), quads[quadNumber + 7]);
double rectY2 = Math.Max(Math.Max(Math.Max(quads[quadNumber + 1], quads[quadNumber + 3]), quads[quadNumber + 5]), quads[quadNumber + 7]);

Rect selectionRect = new Rect(rectX1, rectY1, rectX2, rectY2);

ColorPt defaultColour = new ColorPt(1, 1, 0);
Annot highlightAnnot = CreateHighlightAnnot(selectionRect, defaultColour);

m_pdfViewer.addHighlightAnnotationToPage(highlightAnnot,true);

//m_pdfViewer.Refresh(); //–> to see how this algorithm works when debugging
}
}
}
}

Could you please clarify what platform you are on? WPF, WindowsForms, UWP, Xamarin?

Also, does this happen with any PDF, or only certain ones? If certain ones, we would need access to one of them.