SelectAll Text from current page

54 views
Skip to first unread message

Daniel Lutz

unread,
Aug 20, 2014, 5:43:12 AM8/20/14
to pdfne...@googlegroups.com
Hello


i use your sdk for windows desktop applications. So what i want is to select all text from a selected page. With the following code i select the text:

var page = Viewer.GetDoc().GetPage(Viewer.CurrentPageNumber);
var cropBox = page.GetCropBox();

Viewer.Select(cropBox.x1, cropBox.y1, Viewer.CurrentPageNumber, cropBox.x2, cropBox.y2, Viewer.CurrentPageNumber);

After this i call this

double[] quads = selection.GetQuads();

int sz = quads.Length / 8;

selection is the PdfViewerWPF.Selection and the sz the count of Quads, The example in the attachments contains 56 lines but after the selection with the code i get a sz value of 55. The last line of text is forgotten to select by pdftron. So i hope you can help to resolve this problem.

Best regards
Daniel
test.pdf
Message has been deleted

Aaron

unread,
Aug 21, 2014, 2:36:49 PM8/21/14
to pdfne...@googlegroups.com
Hello Daniel,

Since the WPF viewer's Selection APIs expect screen coordinates instead of PDF page coordinates, you will need to call ConvPagePtToScreenPt on the crop box coordinates before calling Select.  You may want to look at PDFViewViewer.HighlightSelection() in PDFViewControl.xaml.cs for an example.  Note also that there are variants of Select that may be a little more convenient for your use case, such as one that doesn't require a page number, as well as a SelectAll().

Daniel Lutz

unread,
Aug 22, 2014, 2:45:13 AM8/22/14
to pdfne...@googlegroups.com
Hello Aaron,

the problem is that the api documentation not differing exactly when the developer should use screen coordinates or page coordinates. I try the same example which i have send you with converting page coordinates before selecting text:


var page = _viewer.GetDoc().GetPage(_viewer.CurrentPageNumber);
          var cropBox = page.GetCropBox();
 
          double x1 = cropBox.x1;
          double y1 = cropBox.y1;
 
          _viewer.ConvPagePtToScreenPt(ref x1,ref y1,_viewer.CurrentPageNumber);
 
          double x2 = cropBox.x2;
          double y2 = cropBox.y2;
 
          _viewer.ConvPagePtToScreenPt(ref x2,ref y2,_viewer.CurrentPageNumber);
 
          var selected = _viewer.Select(x1, y1, x2, y2);

But then nothing is selected. I try the select method with screen coordinates and with page coordinates and also the following code

var selected = _viewer.Select(cropBox.x1, cropBox.y1, _viewer.CurrentPageNumber, cropBox.x2, cropBox.y2, _viewer.CurrentPageNumber);

What i want is to select the text from a page which i have set not from all page or other it should be the page "I" want! So i hope you can help!

Best Regards
Daniel

Aaron

unread,
Aug 22, 2014, 6:39:32 PM8/22/14
to pdfne...@googlegroups.com
Hello Daniel,

After some further investigation, it appears you will need to slightly deflate the crop box (so that all points are within the page) and normalize them after conversion as well.  The following code should work for you:

            var page = _viewer.GetDoc().GetPage(_viewer.CurrentPageNumber);
            var cropBox = page.GetCropBox();
            cropBox.Inflate(-1);


            double x1 = cropBox.x1;
            double y1 = cropBox.y1;
            _viewer.ConvPagePtToScreenPt(ref x1,ref y1,_viewer.CurrentPageNumber);

            double x2 = cropBox.x2;
            double y2 = cropBox.y2;
            _viewer.ConvPagePtToScreenPt(ref x2,ref y2,_viewer.CurrentPageNumber);
 
            pdftron.PDF.Rect r = new pdftron.PDF.Rect(x1, y1, x2, y2);
            r.Normalize();
            var selected = _viewer.Select(r.x1, r.y1, r.x2, r.y2);

Reply all
Reply to author
Forward
0 new messages