The challenge here is that PDF is a lot like SVG or PostScript. The order in which you position and display things need not have any relation to their logical/reading order.
As a terribly stilted example, one could draw all the 'a's on a page, then all the 'b's, and so on.
A far less stilted example (one I've seen in actual PDFs), is to draw all the text in a given font at once, then the next font, and so on. This is more challenging than you might think in that italic text is generally a distinct font, as is bold as is bold italic. If one is iterating through fonts in hash-table or alphabetic order, its reasonable to expect the title to not be the first text to be drawn by the page contents.
Having said all that, this is a solved problem, several times over.
The bad news: None of those solutions appear in the open source libraries linked in that first comment... 'cept maybe MuPDF, but it's not apparent from their online docs that they can.
The Good News: There's are several command-line driven applications, quite capable of extracting text from a PDF, all of which are described in an excellent answer here on SO: PDF Text Extraction with Coordinates
MuPDF's mutool is listed as one of the options, so it's clearly possible with MuPDF (built by the same company that does GhostScript).