How to export and import PDFViewWPF.Selections?

Question:

For my project I need to temporarily store a users text selection, and then reload it, even when selection is across multiple pages. I thought I could do the following, as a minimal example, but only the last selection shows up.

int selectionStartPage = currentView.GetSelectionBeginPage(); int selectionEndPage = currentView.GetSelectionEndPage(); for (int i = selectionStartPage; i <= selectionEndPage; i++) { PDFViewWPF.Selection selection = currentView.GetSelection(i); if (selection != null) this.selections.Add(selection); } currentView.ClearSelection(); // then later on foreach (PDFViewWPF.Selection selection in this.selections) { currentView.Select(selection); }

Answer:

You need to use the following PDFViewWPF.Select method
https://www.pdftron.com/api/PDFNet/?topic=html/M_pdftron_PDF_PDFViewWPF_Select_4.htm

All that matters is the starting point on the first page, and the last point of the last page. Everything in between is implicit (such as all text is selected anyway on all the middle pages).

The following code determines the start and end points and calls the appropriate PDFViewWPF.Select call.

`
// if first_quad true return rect for first quad, otherwise return rect for last quad
pdftron.PDF.Rect GetRectFromQuads(double[] quads, bool first_quad)
{
if (quads.Length % 8 == 0)
{
int numQuads = quads.Length / 8;
int quadNumber = first_quad ? 0 : (numQuads - 1) * 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]);

return new pdftron.PDF.Rect(rectX1, rectY1, rectX2, rectY2);
}
}
return null;
}

// To save the selection and (optionally) clear the selections in the viewer
int selectionStartPage = Current_View.GetSelectionBeginPage();
int selectionEndPage = Current_View.GetSelectionEndPage();
selections.Add(Current_View.GetSelection(selectionStartPage));
selections.Add(Current_View.GetSelection(selectionEndPage));
// option
Current_View.ClearSelection();
this.ToolManager.CurrentTool.ClearAllTextHighlights();

// load selections and update view
var p1 = GetRectFromQuads(selections[0].GetQuads(), true);
var p2 = GetRectFromQuads(selections[1].GetQuads(), false);
var page1 = selections[0].GetPageNum();
var page2 = selections[1].GetPageNum();
Current_View.Select(p1.x1, p1.y2, page1, p2.x2, p2.y1, page2);
this.ToolManager.CurrentTool.TextSelectionHasChanged();
`