from gi.repository import Poppler, GLib
import ctypes
import sys
import os.path
lib_poppler = ctypes.cdll.LoadLibrary("libpoppler-glib.so.8")

ctypes.pythonapi.PyCapsule_GetPointer.restype = ctypes.c_void_p
ctypes.pythonapi.PyCapsule_GetPointer.argtypes = [ctypes.py_object, ctypes.c_char_p]
PyCapsule_GetPointer = ctypes.pythonapi.PyCapsule_GetPointer

class Poppler_Rectangle(ctypes.Structure):
    _fields_ = [ ("x1", ctypes.c_double), ("y1", ctypes.c_double), ("x2", ctypes.c_double), ("y2", ctypes.c_double) ]
LP_Poppler_Rectangle = ctypes.POINTER(Poppler_Rectangle)
poppler_page_get_text_layout = ctypes.CFUNCTYPE(ctypes.c_int, 
                                                ctypes.c_void_p, 
                                                ctypes.POINTER(LP_Poppler_Rectangle), 
                                                ctypes.POINTER(ctypes.c_uint)
                                                )(lib_poppler.poppler_page_get_text_layout)

def get_page_layout(page):
    assert isinstance(page, Poppler.Page)
    capsule = page.__gpointer__
    page_addr = PyCapsule_GetPointer(capsule, None)
    rectangles = LP_Poppler_Rectangle()
    n_rectangles = ctypes.c_uint(0)
    has_text = poppler_page_get_text_layout(page_addr, ctypes.byref(rectangles), ctypes.byref(n_rectangles))
    try:
        result = []
        if has_text:
            assert n_rectangles.value > 0, "n_rectangles.value > 0: {}".format(n_rectangles.value)
            assert rectangles, "rectangles: {}".format(rectangles)
            for i in range(n_rectangles.value):
                r = rectangles[i]
                result.append((r.x1, r.y1, r.x2, r.y2))
        return result
    finally:
        if rectangles:
            GLib.free(ctypes.addressof(rectangles.contents))

def main():
    
    print ('Version:', Poppler.get_version())
    path=sys.argv[1]
    if not os.path.isabs(path):
        path=os.path.join(os.getcwd(), path)
    d=Poppler.Document.new_from_file('file:'+path)
    n=d.get_n_pages()
    for pg_no in range(n):
        p=d.get_page(pg_no)
        print ('Page %d' % (pg_no+1), 'size ', p.get_size())
        text=p.get_text().decode('UTF-8')
        locs=get_page_layout(p)
        fonts=p.get_text_attributes()
        offset=0
        cfont=0
        for line in text.splitlines(True):
            print (' ', line.encode('UTF-8'),)
            n=len(line)
            for i in range(n):
                if line[i]==u'\n':
                    continue
                font=fonts[cfont]
                while font.start_index > i+offset or font.end_index < i+offset:
                    cfont+=1
                    if cfont>= len(fonts):
                        font=None
                        break
                    font=fonts[cfont]
                
                bb=locs[offset+i]
                print (line[i].encode('UTF-8'), '(%0.2f, %0.2f, %0.2f, %0.2f)' % bb,)
                if font:
                    print (font.font_name, font.font_size, 'r=%d g=%d, b=%d'%(font.color.red, font.color.green, font.color.blue))
            offset+=n
            print ()      
        print()
        #p.free_text_attributes(fonts)

if __name__=='__main__':
    main()
aa88d5d211a0541e819a971cae8f533fee7783eb
<mask token>


class Poppler_Rectangle(ctypes.Structure):
    _fields_ = [('x1', ctypes.c_double), ('y1', ctypes.c_double), ('x2',
        ctypes.c_double), ('y2', ctypes.c_double)]


<mask token>


def get_page_layout(page):
    assert isinstance(page, Poppler.Page)
    capsule = page.__gpointer__
    page_addr = PyCapsule_GetPointer(capsule, None)
    rectangles = LP_Poppler_Rectangle()
    n_rectangles = ctypes.c_uint(0)
    has_text = poppler_page_get_text_layout(page_addr, ctypes.byref(
        rectangles), ctypes.byref(n_rectangles))
    try:
        result = []
        if has_text:
            assert n_rectangles.value > 0, 'n_rectangles.value > 0: {}'.format(
                n_rectangles.value)
            assert rectangles, 'rectangles: {}'.format(rectangles)
            for i in range(n_rectangles.value):
                r = rectangles[i]
                result.append((r.x1, r.y1, r.x2, r.y2))
        return result
    finally:
        if rectangles:
            GLib.free(ctypes.addressof(rectangles.contents))


<mask token>

<mask token>


class Poppler_Rectangle(ctypes.Structure):
    _fields_ = [('x1', ctypes.c_double), ('y1', ctypes.c_double), ('x2',
        ctypes.c_double), ('y2', ctypes.c_double)]


<mask token>


def get_page_layout(page):
    assert isinstance(page, Poppler.Page)
    capsule = page.__gpointer__
    page_addr = PyCapsule_GetPointer(capsule, None)
    rectangles = LP_Poppler_Rectangle()
    n_rectangles = ctypes.c_uint(0)
    has_text = poppler_page_get_text_layout(page_addr, ctypes.byref(
        rectangles), ctypes.byref(n_rectangles))
    try:
        result = []
        if has_text:
            assert n_rectangles.value > 0, 'n_rectangles.value > 0: {}'.format(
                n_rectangles.value)
            assert rectangles, 'rectangles: {}'.format(rectangles)
            for i in range(n_rectangles.value):
                r = rectangles[i]
                result.append((r.x1, r.y1, r.x2, r.y2))
        return result
    finally:
        if rectangles:
            GLib.free(ctypes.addressof(rectangles.contents))


def main():
    print('Version:', Poppler.get_version())
    path = sys.argv[1]
    if not os.path.isabs(path):
        path = os.path.join(os.getcwd(), path)
    d = Poppler.Document.new_from_file('file:' + path)
    n = d.get_n_pages()
    for pg_no in range(n):
        p = d.get_page(pg_no)
        print('Page %d' % (pg_no + 1), 'size ', p.get_size())
        text = p.get_text().decode('UTF-8')
        locs = get_page_layout(p)
        fonts = p.get_text_attributes()
        offset = 0
        cfont = 0
        for line in text.splitlines(True):
            print(' ', line.encode('UTF-8'))
            n = len(line)
            for i in range(n):
                if line[i] == u'\n':
                    continue
                font = fonts[cfont]
                while (font.start_index > i + offset or font.end_index < i +
                    offset):
                    cfont += 1
                    if cfont >= len(fonts):
                        font = None
                        break
                    font = fonts[cfont]
                bb = locs[offset + i]
                print(line[i].encode('UTF-8'), 
                    '(%0.2f, %0.2f, %0.2f, %0.2f)' % bb)
                if font:
                    print(font.font_name, font.font_size, 'r=%d g=%d, b=%d' %
                        (font.color.red, font.color.green, font.color.blue))
            offset += n
            print()
        print()


if __name__ == '__main__':
    main()

<mask token>
lib_poppler = ctypes.cdll.LoadLibrary('libpoppler-glib.so.8')
ctypes.pythonapi.PyCapsule_GetPointer.restype = ctypes.c_void_p
ctypes.pythonapi.PyCapsule_GetPointer.argtypes = [ctypes.py_object, ctypes.
    c_char_p]
PyCapsule_GetPointer = ctypes.pythonapi.PyCapsule_GetPointer


class Poppler_Rectangle(ctypes.Structure):
    _fields_ = [('x1', ctypes.c_double), ('y1', ctypes.c_double), ('x2',
        ctypes.c_double), ('y2', ctypes.c_double)]


LP_Poppler_Rectangle = ctypes.POINTER(Poppler_Rectangle)
poppler_page_get_text_layout = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.
    c_void_p, ctypes.POINTER(LP_Poppler_Rectangle), ctypes.POINTER(ctypes.
    c_uint))(lib_poppler.poppler_page_get_text_layout)


def get_page_layout(page):
    assert isinstance(page, Poppler.Page)
    capsule = page.__gpointer__
    page_addr = PyCapsule_GetPointer(capsule, None)
    rectangles = LP_Poppler_Rectangle()
    n_rectangles = ctypes.c_uint(0)
    has_text = poppler_page_get_text_layout(page_addr, ctypes.byref(
        rectangles), ctypes.byref(n_rectangles))
    try:
        result = []
        if has_text:
            assert n_rectangles.value > 0, 'n_rectangles.value > 0: {}'.format(
                n_rectangles.value)
            assert rectangles, 'rectangles: {}'.format(rectangles)
            for i in range(n_rectangles.value):
                r = rectangles[i]
                result.append((r.x1, r.y1, r.x2, r.y2))
        return result
    finally:
        if rectangles:
            GLib.free(ctypes.addressof(rectangles.contents))


def main():
    print('Version:', Poppler.get_version())
    path = sys.argv[1]
    if not os.path.isabs(path):
        path = os.path.join(os.getcwd(), path)
    d = Poppler.Document.new_from_file('file:' + path)
    n = d.get_n_pages()
    for pg_no in range(n):
        p = d.get_page(pg_no)
        print('Page %d' % (pg_no + 1), 'size ', p.get_size())
        text = p.get_text().decode('UTF-8')
        locs = get_page_layout(p)
        fonts = p.get_text_attributes()
        offset = 0
        cfont = 0
        for line in text.splitlines(True):
            print(' ', line.encode('UTF-8'))
            n = len(line)
            for i in range(n):
                if line[i] == u'\n':
                    continue
                font = fonts[cfont]
                while (font.start_index > i + offset or font.end_index < i +
                    offset):
                    cfont += 1
                    if cfont >= len(fonts):
                        font = None
                        break
                    font = fonts[cfont]
                bb = locs[offset + i]
                print(line[i].encode('UTF-8'), 
                    '(%0.2f, %0.2f, %0.2f, %0.2f)' % bb)
                if font:
                    print(font.font_name, font.font_size, 'r=%d g=%d, b=%d' %
                        (font.color.red, font.color.green, font.color.blue))
            offset += n
            print()
        print()


if __name__ == '__main__':
    main()

from gi.repository import Poppler, GLib
import ctypes
import sys
import os.path
lib_poppler = ctypes.cdll.LoadLibrary('libpoppler-glib.so.8')
ctypes.pythonapi.PyCapsule_GetPointer.restype = ctypes.c_void_p
ctypes.pythonapi.PyCapsule_GetPointer.argtypes = [ctypes.py_object, ctypes.
    c_char_p]
PyCapsule_GetPointer = ctypes.pythonapi.PyCapsule_GetPointer


class Poppler_Rectangle(ctypes.Structure):
    _fields_ = [('x1', ctypes.c_double), ('y1', ctypes.c_double), ('x2',
        ctypes.c_double), ('y2', ctypes.c_double)]


LP_Poppler_Rectangle = ctypes.POINTER(Poppler_Rectangle)
poppler_page_get_text_layout = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.
    c_void_p, ctypes.POINTER(LP_Poppler_Rectangle), ctypes.POINTER(ctypes.
    c_uint))(lib_poppler.poppler_page_get_text_layout)


def get_page_layout(page):
    assert isinstance(page, Poppler.Page)
    capsule = page.__gpointer__
    page_addr = PyCapsule_GetPointer(capsule, None)
    rectangles = LP_Poppler_Rectangle()
    n_rectangles = ctypes.c_uint(0)
    has_text = poppler_page_get_text_layout(page_addr, ctypes.byref(
        rectangles), ctypes.byref(n_rectangles))
    try:
        result = []
        if has_text:
            assert n_rectangles.value > 0, 'n_rectangles.value > 0: {}'.format(
                n_rectangles.value)
            assert rectangles, 'rectangles: {}'.format(rectangles)
            for i in range(n_rectangles.value):
                r = rectangles[i]
                result.append((r.x1, r.y1, r.x2, r.y2))
        return result
    finally:
        if rectangles:
            GLib.free(ctypes.addressof(rectangles.contents))


def main():
    print('Version:', Poppler.get_version())
    path = sys.argv[1]
    if not os.path.isabs(path):
        path = os.path.join(os.getcwd(), path)
    d = Poppler.Document.new_from_file('file:' + path)
    n = d.get_n_pages()
    for pg_no in range(n):
        p = d.get_page(pg_no)
        print('Page %d' % (pg_no + 1), 'size ', p.get_size())
        text = p.get_text().decode('UTF-8')
        locs = get_page_layout(p)
        fonts = p.get_text_attributes()
        offset = 0
        cfont = 0
        for line in text.splitlines(True):
            print(' ', line.encode('UTF-8'))
            n = len(line)
            for i in range(n):
                if line[i] == u'\n':
                    continue
                font = fonts[cfont]
                while (font.start_index > i + offset or font.end_index < i +
                    offset):
                    cfont += 1
                    if cfont >= len(fonts):
                        font = None
                        break
                    font = fonts[cfont]
                bb = locs[offset + i]
                print(line[i].encode('UTF-8'), 
                    '(%0.2f, %0.2f, %0.2f, %0.2f)' % bb)
                if font:
                    print(font.font_name, font.font_size, 'r=%d g=%d, b=%d' %
                        (font.color.red, font.color.green, font.color.blue))
            offset += n
            print()
        print()


if __name__ == '__main__':
    main()

from gi.repository import Poppler, GLib
import ctypes
import sys
import os.path
lib_poppler = ctypes.cdll.LoadLibrary("libpoppler-glib.so.8")

ctypes.pythonapi.PyCapsule_GetPointer.restype = ctypes.c_void_p
ctypes.pythonapi.PyCapsule_GetPointer.argtypes = [ctypes.py_object, ctypes.c_char_p]
PyCapsule_GetPointer = ctypes.pythonapi.PyCapsule_GetPointer

class Poppler_Rectangle(ctypes.Structure):
    _fields_ = [ ("x1", ctypes.c_double), ("y1", ctypes.c_double), ("x2", ctypes.c_double), ("y2", ctypes.c_double) ]
LP_Poppler_Rectangle = ctypes.POINTER(Poppler_Rectangle)
poppler_page_get_text_layout = ctypes.CFUNCTYPE(ctypes.c_int, 
                                                ctypes.c_void_p, 
                                                ctypes.POINTER(LP_Poppler_Rectangle), 
                                                ctypes.POINTER(ctypes.c_uint)
                                                )(lib_poppler.poppler_page_get_text_layout)

def get_page_layout(page):
    assert isinstance(page, Poppler.Page)
    capsule = page.__gpointer__
    page_addr = PyCapsule_GetPointer(capsule, None)
    rectangles = LP_Poppler_Rectangle()
    n_rectangles = ctypes.c_uint(0)
    has_text = poppler_page_get_text_layout(page_addr, ctypes.byref(rectangles), ctypes.byref(n_rectangles))
    try:
        result = []
        if has_text:
            assert n_rectangles.value > 0, "n_rectangles.value > 0: {}".format(n_rectangles.value)
            assert rectangles, "rectangles: {}".format(rectangles)
            for i in range(n_rectangles.value):
                r = rectangles[i]
                result.append((r.x1, r.y1, r.x2, r.y2))
        return result
    finally:
        if rectangles:
            GLib.free(ctypes.addressof(rectangles.contents))

def main():
    
    print ('Version:', Poppler.get_version())
    path=sys.argv[1]
    if not os.path.isabs(path):
        path=os.path.join(os.getcwd(), path)
    d=Poppler.Document.new_from_file('file:'+path)
    n=d.get_n_pages()
    for pg_no in range(n):
        p=d.get_page(pg_no)
        print ('Page %d' % (pg_no+1), 'size ', p.get_size())
        text=p.get_text().decode('UTF-8')
        locs=get_page_layout(p)
        fonts=p.get_text_attributes()
        offset=0
        cfont=0
        for line in text.splitlines(True):
            print (' ', line.encode('UTF-8'),)
            n=len(line)
            for i in range(n):
                if line[i]==u'\n':
                    continue
                font=fonts[cfont]
                while font.start_index > i+offset or font.end_index < i+offset:
                    cfont+=1
                    if cfont>= len(fonts):
                        font=None
                        break
                    font=fonts[cfont]
                
                bb=locs[offset+i]
                print (line[i].encode('UTF-8'), '(%0.2f, %0.2f, %0.2f, %0.2f)' % bb,)
                if font:
                    print (font.font_name, font.font_size, 'r=%d g=%d, b=%d'%(font.color.red, font.color.green, font.color.blue))
            offset+=n
            print ()      
        print()
        #p.free_text_attributes(fonts)

if __name__=='__main__':
    main()