QPdfView how to select text by mouse
-
I can load and render a PDF file using QPdfView:
// inside MainWindow constructor QPdfView *pdf = new QPdfView(); pdf->setPageMode(QPdfView::PageMode::MultiPage); pdf->setZoomMode(QPdfView::ZoomMode::FitToWidth); setCentralWidget(pdf); QPdfDocument *doc = new QPdfDocument(this); pdf->setDocument(doc); doc->load("C:\\path\\to\\sample.pdf");How to make text selectable using mouse?
Do I need to convert mousePress/Move/Release position to pdf coordinate (and how)? And manually construct QPdfSelection object using start/end location?
-
I can load and render a PDF file using QPdfView:
// inside MainWindow constructor QPdfView *pdf = new QPdfView(); pdf->setPageMode(QPdfView::PageMode::MultiPage); pdf->setZoomMode(QPdfView::ZoomMode::FitToWidth); setCentralWidget(pdf); QPdfDocument *doc = new QPdfDocument(this); pdf->setDocument(doc); doc->load("C:\\path\\to\\sample.pdf");How to make text selectable using mouse?
Do I need to convert mousePress/Move/Release position to pdf coordinate (and how)? And manually construct QPdfSelection object using start/end location?
@songziming
GooglingQPdfView select textgives e.g. https://askubuntu.com/questions/1071771/where-is-the-text-selection-tool-in-qpdfview. And see https://forum.qt.io/topic/115458/how-can-i-select-and-copy-text-from-a-qpdfview-widget. -
@songziming
GooglingQPdfView select textgives e.g. https://askubuntu.com/questions/1071771/where-is-the-text-selection-tool-in-qpdfview. And see https://forum.qt.io/topic/115458/how-can-i-select-and-copy-text-from-a-qpdfview-widget. -
(disclaimer: I have not used either the standalone program nor the QPdfView class)
Although @JonB appears to have provided links that refer to the standalone utility viewer, said viewer is open source and built using Qt. Therefore, (if no better option or no quicker tip appears in this forum), we can probably find clues about how to achieve text selection by looking at the code for the standalone utility and learning how it achieves this.
Source code: https://git.launchpad.net/qpdfview
(Learning from source code of qpdfview utility may or may not have been what @JonB had in mind, but either way the point stands. I do apologize if I am appearing to just "copy" or "regurgitate" JonB's answer as my own.)
-
It is not an easy ask.
The first challenge is obtaining the
QPdfSelection. Those can't be created directly, butQPdfDocument::getSelectionis mostly what you want. The problem is that it needs to be called for a single page, and the start and end points are relative to the top-left of the page.That means that you need access to the geometry of the pages (for each page, its' bounding rectangle in viewport coordinates) after the mouse is released and you have the selected rectangle (in widget or viewport coordinates). That is needed to determine which pages intersect with the selection and to convert the intersection with each into page coordinates.
QPdfViewof course manages that data, but does not expose it.It can be reasonably independently calculated from information in the
QPdfDocumenttogether with information about the current screen DPI. But if you do that, you'll probably recognize like me that that is actually most of whatQPdfViewdoes, so you might as well stop using it and implement your own viewing widget anyway.The second and larger challenge is providing feedback to the user as to what text they selected.
QPdfSelection::bounds()has that information, but showing it in the traditional style of rendering the background of the selected text in the highlight color and the selected text itself in the highlighted text color can't really be done with the QPdf module. I think that most PDF readers do that correctly by adding a transient annotation to the PDF and feeding that to the rendering pipeline, but there are no facilities for that in the module.The bottom line is that
QPdfViewis really convenient for what it does, but if you need anything even slightly more advanced than that - you should look at a more full featured PDF library like Poppler or MuPDF.