Skip to content

ENH: Add PDF annotation /IRT ("in reply to") #2065

@C-monC

Description

@C-monC

I am working on getting the "in reply to" working for annotations using pypdf but I think there is something that I am misunderstanding. Here's a basic script I thought should've worked.

from pypdf import PageObject, PdfWriter
from pypdf.generic import (
    DictionaryObject,
    NameObject,
    RectangleObject,
    ArrayObject,
    TextStringObject,
    NumberObject,
    FloatObject,
)

writer = PdfWriter()
writer.add_page(PageObject.create_blank_page(writer, 1000, 1000))


test_anot = DictionaryObject(
    {
        NameObject("/Type"): NameObject("/Annot"),
        NameObject("/Subtype"): NameObject("/Text"),
        NameObject("/Rect"): RectangleObject(
            [
                FloatObject(100.0),
                FloatObject(100.0),
                FloatObject(125.0),
                FloatObject(125.0),
            ]
        ),
        NameObject("/Contents"): TextStringObject("Testcomment"),
        NameObject("/P"): NumberObject(0),
        NameObject("/C"): ArrayObject(
            [FloatObject(0.97), FloatObject(0.98), FloatObject(0.31)]
        ),
        NameObject("/Name"): TextStringObject("Help"),
        NameObject("/T"): TextStringObject("Me"),
        NameObject("/RC"): TextStringObject("Testcomment"),
    }
)
added_annot = writer.add_annotation(page_number=0, annotation=test_anot)
test_anot2 = DictionaryObject(
    {
        NameObject("/Type"): NameObject("/Annot"),
        NameObject("/Subtype"): NameObject("/Text"),
        NameObject("/Contents"): TextStringObject("Reply"),
        NameObject("/P"): NumberObject(0),
        NameObject("/C"): ArrayObject(
            [FloatObject(0.97), FloatObject(0.98), FloatObject(0.31)]
        ),
        NameObject("/Name"): TextStringObject("Help"),
        NameObject("/T"): TextStringObject("Me"),
        NameObject("/IRT"): test_anot.indirect_reference,
        NameObject("/RT"): NameObject("/R"),
        NameObject("/RC"): TextStringObject("Reply"),
    }
)


writer.add_annotation(page_number=0, annotation=test_anot2)
with open("annotated-pdf.pdf", "wb") as fp:
    writer.write(fp)

Only the parent annotation is visible in the pdf.
image

If I compare annotations and annotation replies in Adobe Acrobat to the script I can see the IRT gets populated correctly.

Snippet of adobe annotation with /IRT:

<</AP<</N 200 0 R/R 201 0 R>>/C[1.0 0.819611 0.0]/Contents(Comment 2)/CreationDate(D:20230803120428+02'00')/F 28/IRT 196 0 R/M

script annotation:

5 0 obj
<<
/Type /Annot
/Subtype /Text
/Rect [ 100 100 125 125 ]
/Contents (Testcomment)
/P 4 0 R
/C [ 0.97 0.98 0.31 ]
/Name (Help)
/T (Me)
/RC (Testcomment)

endobj
6 0 obj
<<
/Type /Annot
/Subtype /Text
/Contents (Reply)
/P 4 0 R
/C [ 0.97 0.98 0.31 ]
/Name (Help)
/T (Me)
/IRT 5 0 R
/RT /R
/RC (Reply)

endobj

Could anyone provide guidance on getting this work. I'd be happy to work on adding it to pypdf if it does.

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedWe appreciate help everywhere - this one might be an easy start!is-featureA feature requestworkflow-annotationEverything about annotating PDF files

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions