Skip to content

Question: how is word's origin computed? #840

Closed

Description

@VeryLazyBoy

I notice that there is a origin attribute in the TextPage content, but when I change the MediaBox and CropBox of the pdf file, the origin becomes very weird.

For example, the following code:

text = '123'

doc = fitz.Document()
page = doc.newPage()
page.setMediaBox(fitz.Rect(100, 50, 500, 500))
page.setCropBox(fitz.Rect(0, 0, 400, 400))
rc = page.insertText((0,0), text)

page.getText('dict')

outputs origin with the value: (-100, 50)

{
    'width': 2.0,
    'height': 2.0,
    'blocks': [
        {
            'number': 0,
            'type': 0,
            'bbox': (-100.0, 38.17499923706055, -81.6520004272461, 53.28900146484375),
            'lines': [
                {
                    'spans': [
                        {
                            'size': 11.0,
                            'flags': 0,
                            'font': 'Helvetica',
                            'color': 0,
                            'ascender': 1.0750000476837158,
                            'descender': -0.29899999499320984,
                            'text': '123',
                            'origin': (-100.0, 50.0),
                            'bbox': (-100.0, 38.17499923706055, -81.6520004272461, 53.28900146484375)
                        }
                    ],
                    'wmode': 0,
                    'dir': (1.0, 0.0),
                    'bbox': (-100.0, 38.17499923706055, -81.6520004272461, 53.28900146484375)
                }
            ]
        }
    ]
}

Could you help me to figure out how this origin is computed?

Activity

  1. JorjMcKie commented on Jan 19, 2021

    @JorjMcKie
    Collaborator

    Your combination of mediabox / cropbox data make no sense: cropbox must be inside mediabox, which in turn must start at (0,0).

    I will add checks to prevent things you have done.

  2. VeryLazyBoy commented on Jan 19, 2021

    @VeryLazyBoy
    Author

    Thanks for your reply, but I do encounter some pdf with abnormal mediabox and cropbox. For example, the file I attached:

    123.pdf

    file_name = '123.pdf'
    doc = fitz.open(file_name)
    
    page = doc.loadPage(0)
    print(page.MediaBox, page.CropBox)
    # gives Rect(9.0, 9.0, 621.0, 801.0), Rect(9.0, 0.0, 621.0, 792.0)

    if I want to insert a text to this file, I find it would be inserted to a weird place

  3. JorjMcKie commented on Jan 19, 2021

    @JorjMcKie
    Collaborator

    if I want to insert a text to this file, I find it would be inserted to a weird place

    This has other reasons:
    Inside the page definitions (/Contents object), geometry changes happen in a "non-localized" way - meaning they are not encapsulated properly by stacking / unstacking commands "q" / "Q".
    Heal this by doing page.clean_contents() or page.wrap_contents() before your inserts and all is roses 😎:

    >>> doc=fitz.open("123.pdf")
    >>> page=doc[0]
    >>> page.wrap_contents()
    >>> page.insertText((100,100),"Hello world")
    1
    >>> doc.saveIncr()
    >>> 

    grafik

  4. JorjMcKie commented on Jan 19, 2021

    @JorjMcKie
    Collaborator

    you can check for potential issues like that by looking at page.is_wrapped.

  5. JorjMcKie commented on Jan 19, 2021

    @JorjMcKie
    Collaborator

    For more background of this read section "Misplaced Item Insertions on PDF Pages" of this documentation chapter.

  6. VeryLazyBoy commented on Jan 19, 2021

    @VeryLazyBoy
    Author

    This pdf seems to have multiple problems. I have tried your commands:

    >>> doc=fitz.open("123.pdf")
    >>> page=doc[0]
    >>> page.wrap_contents()
    >>> page.insertText((100,100),"Hello World!")
    1
    >>> page.getText('dict')['blocks'][1]['lines']
    [{'spans': [{'size': 11.0,
        'flags': 0,
        'font': 'Helvetica',
        'color': 0,
        'ascender': 1.0750000476837158,
        'descender': -0.29899999499320984,
        'text': 'Hello World!',
        'origin': (100.0, 109.0),
        'bbox': (100.0,
         97.17500305175781,
         159.89498901367188,
         112.28900146484375)}],
      'wmode': 0,
      'dir': (1.0, 0.0),
      'bbox': (100.0, 97.17500305175781, 159.89498901367188, 112.28900146484375)}]

    I am wondering if I need to correct the media box and crop box first to insert a text properly? Given the media box is Rect(9.0, 9.0, 621.0, 801.0) and crop box is Rect(9.0, 0.0, 621.0, 792.0), the text's bottom left is located at (100, 109) instead of (100, 100).

  7. JorjMcKie commented on Jan 19, 2021

    @JorjMcKie
    Collaborator

    I am wondering if I need to correct the media box and crop box first to insert a text properly?

    No, don't do that. This will break the position of other stuff on the page.
    I will need to adjust PyMuPDF to accomodate this weird situation.
    In the meantime subtract page.MediaBox.y0 from the desired insertion y-position as a workaround. In "normal" cases this value will be zero and do no harm.
    So in your case, page.insertText((100, 100 - page.MediaBox.y0), "hello") should make it.

  8. VeryLazyBoy commented on Jan 19, 2021

    @VeryLazyBoy
    Author

    Thanks for your kindly help!

  9. locked and limited conversation to collaborators on Jan 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions