Skip to content

merge_transformed_page: Allow transforming annotation rectangles #3445

@vitek

Description

@vitek

I'm using pdfly to create a book from single-page pdf file. Link on the right side does not work.

I've nailed it down. When attributes are merged /Rect is not translated. I made a workaround in my own script:

...
        page = reader.pages[first]
        other = reader.pages[second]

        tx = float(page.mediabox.width)
        other[NameObject(PG.ANNOTS)] = ArrayObject(
            list(
                _transform_annots(get_annotations(other), tx=tx)
            )
        )

        page.merge_translated_page(
            other, tx=tx, ty=0, expand=True,
        )
        writer.add_page(page)

def _transform_annots(annots, tx):
    for annot in annots:
        obj = annot.get_object()
        #obj = copy.deepcopy(obj)
        if '/Rect' in obj:
            rect = obj['/Rect']
            obj[NameObject('/Rect')] = ArrayObject([
                FloatObject(rect[0] + tx),
                rect[1],
                FloatObject(rect[2] + tx),
                rect[3],
            ])
        yield annot


def get_annotations(page):
    if PG.ANNOTS in page:
        annots = page[PG.ANNOTS]
        if isinstance(annots, ArrayObject):
            return annots
    return []

I'd like to have this fixed in upstream version of pypdf. I tried to do it myself and ran into a problem. I can translate existing indirect object (as in code above). But it should be copied since attribute is mutated. And I'm not sure how to do it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    is-featureA feature requestworkflow-mergeFrom a users perspective, merging is the affected feature/workflow

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions