Search code examples
mupdf

MuPDF get every image coordinates from pdf page


As the title say's i want to retrive all the coordinates of the images that are inside the pdf page. What i tried until now is:

                    dev = fz_new_text_device(ctx, sheet, page);

                    fz_disable_device_hints(dev , FZ_IGNORE_IMAGE);

                    fz_run_page(doc, pg->currentPage, dev , &ctm,
                    NULL);

                    // iterate over the page_blocks (can be image and text);
                    fz_page_block *block;

                    for (block = page->blocks;
                            block < page->blocks + page->len; block++) {

                        if  (block->type == FZ_PAGE_BLOCK_IMAGE) {
                            fz_image_block *iBlock = block->u.image;
                            fz_irect iRect;
                            fz_irect_from_rect(&iRect, &iBlock->bbox);
                            // the resulting irect from here has x0 = 0 and also 
                            // y0 = 0 even if there are no images aligned in [0,0]
                            }
                    }

So my question is if i started doing this the right way and if not could someone give some pointers on how can i get the coordinates for the image ?


Solution

  • So the code there is making a text extraction device, and is then running the page contents through that device. This returns you a structured list of text blocks on the page, together with the images on the page.

    The structured data returned does NOT include the positions/scales at which the images were placed. The width/height data you are accessing is that of the images themselves, not their placement on the page.

    It sounds like you'll need to update the text extraction device to store the fz_matrix's used for each image too. It's not a huge job. Pop into the #ghostscript irc channel for some pointers.

    Robin