pypdf icon indicating copy to clipboard operation
pypdf copied to clipboard

ENH: Add AnnotationBuilder.widget(...)

Open MartinThoma opened this issue 1 year ago • 2 comments

See #107

MartinThoma avatar Aug 05 '22 18:08 MartinThoma

Looking at some examples:

>>> from PyPDF2 import PdfReader
>>> reader = PdfReader("pdf/04b33e0ac3e1dbfa2c575280319abf27.pdf")
>>> reader.pages[0].annotations
[IndirectObject(23, 0, 140234144846704), IndirectObject(25, 0, 140234144846704), IndirectObject(27, 0, 140234144846704), IndirectObject(29, 0, 140234144846704), IndirectObject(31, 0, 140234144846704), IndirectObject(33, 0, 140234144846704), IndirectObject(35, 0, 140234144846704), IndirectObject(37, 0, 140234144846704), IndirectObject(39, 0, 140234144846704), IndirectObject(41, 0, 140234144846704), IndirectObject(43, 0, 140234144846704), IndirectObject(45, 0, 140234144846704), IndirectObject(47, 0, 140234144846704), IndirectObject(49, 0, 140234144846704), IndirectObject(51, 0, 140234144846704), IndirectObject(53, 0, 140234144846704), IndirectObject(55, 0, 140234144846704), IndirectObject(57, 0, 140234144846704), IndirectObject(59, 0, 140234144846704), IndirectObject(61, 0, 140234144846704), IndirectObject(63, 0, 140234144846704), IndirectObject(65, 0, 140234144846704), IndirectObject(67, 0, 140234144846704), IndirectObject(69, 0, 140234144846704)]
>>> reader.pages[0].annotations[0]
IndirectObject(23, 0, 140234144846704)
>>> reader.pages[0].annotations[0].get_object()
{'/Rect': [191.775, 642.631, 504.437, 652.758], '/Subtype': '/Widget', '/BS': {'/S': '/S', '/W': 2}, '/Parent': IndirectObject(8, 0, 140234144846704), '/P': IndirectObject(21, 0, 140234144846704), '/Type': '/Annot', '/MK': {'/BG': [1, 1, 0]}, '/AP': {'/N': IndirectObject(24, 0, 140234144846704)}}
>>> reader.pages[0].annotations[0].get_object()['/Parent']
{'/T': 'Back', '/DA': '/Times 8 Tf 0 g', '/FT': '/Tx', '/Ff': 1, '/Kids': [IndirectObject(23, 0, 140234144846704), IndirectObject(25, 0, 140234144846704), IndirectObject(27, 0, 140234144846704), IndirectObject(29, 0, 140234144846704), IndirectObject(31, 0, 140234144846704), IndirectObject(33, 0, 140234144846704), IndirectObject(35, 0, 140234144846704), IndirectObject(37, 0, 140234144846704), IndirectObject(39, 0, 140234144846704), IndirectObject(41, 0, 140234144846704), IndirectObject(43, 0, 140234144846704), IndirectObject(45, 0, 140234144846704)]}
>>> reader.pages[0].annotations[1].get_object()
{'/Rect': [191.775, 630.606, 504.437, 640.732], '/Subtype': '/Widget', '/BS': {'/S': '/S', '/W': 2}, '/Parent': IndirectObject(8, 0, 140234144846704), '/P': IndirectObject(21, 0, 140234144846704), '/Type': '/Annot', '/MK': {'/BG': [1, 1, 0]}, '/AP': {'/N': IndirectObject(26, 0, 140234144846704)}}
>>> reader.pages[0].annotations[2].get_object()
{'/Rect': [91.1404, 607.82, 503.804, 617.947], '/Subtype': '/Widget', '/BS': {'/S': '/S', '/W': 2}, '/Parent': IndirectObject(8, 0, 140234144846704), '/P': IndirectObject(21, 0, 140234144846704), '/Type': '/Annot', '/MK': {'/BG': [1, 1, 0]}, '/AP': {'/N': IndirectObject(28, 0, 140234144846704)}}
>>> reader.pages[0].annotations[3].get_object()
{'/Rect': [192.407, 593.896, 505.07, 604.023], '/Subtype': '/Widget', '/BS': {'/S': '/S', '/W': 2}, '/Parent': IndirectObject(8, 0, 140234144846704), '/P': IndirectObject(21, 0, 140234144846704), '/Type': '/Annot', '/MK': {'/BG': [1, 1, 0]}, '/AP': {'/N': IndirectObject(30, 0, 140234144846704)}}

Beautifying the first:

{
    "/Type": "/Annot",
    "/Subtype": "/Widget",
    "/Rect": [
        191.775,
        642.631,
        504.437,
        652.758
    ],
    "/BS": {
        "/S": "/S",
        "/W": 2
    },
    "/Parent": "IndirectObject(...)",
    "/P": "IndirectObject(...)",
    "/MK": {
        "/BG": [
            1,
            1,
            0
        ]
    },
    "/AP": {
        "/N": "IndirectObject(...)"
    }
}

And looking at its indirect objects:

>>> reader.pages[0].annotations[0].get_object()['/P']
{'/CropBox': [0, 0, 612, 792], '/Annots': IndirectObject(22, 0, 140234144846704), '/Parent': IndirectObject(15, 0, 140234144846704), '/Contents': IndirectObject(75, 0, 140234144846704), '/Rotate': 0, '/MediaBox': [0, 0, 612, 792], '/Resources': IndirectObject(71, 0, 140234144846704), '/Type': '/Page'}

>>> reader.pages[0].annotations[0].get_object()['/Parent']
{'/T': 'Back', '/DA': '/Times 8 Tf 0 g', '/FT': '/Tx', '/Ff': 1, '/Kids': [IndirectObject(23, 0, 140234144846704), IndirectObject(25, 0, 140234144846704), IndirectObject(27, 0, 140234144846704), IndirectObject(29, 0, 140234144846704), IndirectObject(31, 0, 140234144846704), IndirectObject(33, 0, 140234144846704), IndirectObject(35, 0, 140234144846704), IndirectObject(37, 0, 140234144846704), IndirectObject(39, 0, 140234144846704), IndirectObject(41, 0, 140234144846704), IndirectObject(43, 0, 140234144846704), IndirectObject(45, 0, 140234144846704)]}

>>> reader.pages[0].annotations[0].get_object()['/AP']["/N"]
{'/Subtype': '/Form', '/Resources': {'/ProcSet': ['/PDF']}, '/BBox': [0, 0, 312.662, 10.1267]}

MartinThoma avatar Aug 05 '22 18:08 MartinThoma

Codecov Report

Merging #1207 (4e9d300) into main (759cbc3) will decrease coverage by 0.03%. The diff coverage is 78.57%.

@@            Coverage Diff             @@
##             main    #1207      +/-   ##
==========================================
- Coverage   92.15%   92.12%   -0.04%     
==========================================
  Files          24       24              
  Lines        4948     4962      +14     
  Branches     1024     1029       +5     
==========================================
+ Hits         4560     4571      +11     
  Misses        244      244              
- Partials      144      147       +3     
Impacted Files Coverage Δ
PyPDF2/generic.py 91.43% <78.57%> (-0.18%) :arrow_down:

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

codecov[bot] avatar Aug 06 '22 05:08 codecov[bot]

I'm closing this PR as I couldn't make it work

MartinThoma avatar Jan 06 '23 18:01 MartinThoma