pypdf icon indicating copy to clipboard operation
pypdf copied to clipboard

Tutorials or better demos to manipulate annotations

Open ubuntuslave opened this issue 10 years ago • 12 comments

I'm trying to understand how PyPDF2 works with existing annotation objects, such as highlights, and Popups. The demos provided don't show how to _add_ new DictionaryObjects to the current list of annotations. I believe my problem resides around not having a way to obtain (find out) idnum for the new object that it's needed by its parent's to refer to. Here is what I've been doing so far... (To be honest, I gave up looking at the code inside pdf.py and generic.py because it's taking too long at the moment)

from typing import cast

from PyPDF2 import PdfReader, PdfWriter
from PyPDF2.generic import ArrayObject

writer = PdfWriter()
reader = PdfReader("commented.pdf")

# print how many pages input1 has:
print(f"document1.pdf has {len(reader.pages)} pages.")

# add page 1 from input1 to output document, unchanged
page = reader.pages[0]

annots = cast(ArrayObject, page["/Annots"])
annot0 = annots[0].getObject()
annot1 = annots[1].getObject()
annot2 = annots[2].getObject()
annot3 = annots[3].getObject()
annot4 = annots[4].getObject()

# TEST: changing postition of the Popup's rectangle (Works!)
from PyPDF2.generic import *

popup_size = (180, 120)

rect = RectangleObject(annot0["/Rect"])
rect_x0 = rect.left
rect_y0 = rect.top
annot2.update(
    {
        NameObject("/Open"): BooleanObject(False),
        NameObject("/Rect"): RectangleObject(
            [rect_x0, rect_y0, rect_x0 + popup_size[0], rect_y0 + popup_size[1]]
        ),
    }
)

# Test make new Popup Annotation
rect = RectangleObject(annot1["/Rect"])
rect_x0 = rect.left
rect_y0 = rect.top

popup = DictionaryObject()
popup.update(
    {
        NameObject("/Type"): NameObject("/Annot"),
        NameObject("/Subtype"): NameObject("/Popup"),
        NameObject("/Parent"): IndirectObject(
            15, 0, reader
        ),  # TODO: How to find the parent's idnum? Manually: this is 15 for this object
        NameObject("/Open"): BooleanObject(False),
        NameObject("/Rect"): RectangleObject(
            [rect_x0, rect_y0, rect_x0 + popup_size[0], rect_y0 + popup_size[1]]
        ),
        NameObject("/F"): NumberObject(28),  # The type of object? A popup?
    }
)

popup_ref = writer._add_object(popup)

if "/Annots" in page:
    page["/Annots"].append(popup_ref)
else:
    page[NameObject("/Annots")] = ArrayObject([popup_ref])

# Adding the reference to its new popup:
annot1.update(
    {NameObject("/Popup"): popup_ref}  # TODO: put the number for the new reference
)

annots_ref = writer._add_object(annots)

writer.add_page(page)  # FIXME: not adding the new Popup annotation
# finally, write "output" to document-output.pdf
with open("PyPDF2-output.pdf", "wb") as fp:
    writer.write(fp)

P.S: My motivation for this little script is to give it to users of docear since mind-mapping of highlights requires retroactively adding missing popups to highlight annotations (in adobe acrobat) like the proprietary program done by this guy.

ubuntuslave avatar Jun 11 '14 04:06 ubuntuslave

Thanks for the feedback. I'm currently working on better documentation for PyPDF2, and I intend for it to feature plenty of examples/demos that might answer questions like yours

mstamy2 avatar Jun 16 '14 17:06 mstamy2

Hi, just want to follow up on this issue. Are there demos/examples for adding annotations?

cheetah90 avatar Jan 04 '16 22:01 cheetah90

Here is an example for creating a highlight: https://gist.github.com/agentcooper/4c55133f5d95866acdee5017cd318558

@mstamy2 are you interested in adding methods for highlights? I can provide a pull request.

agentcooper avatar Nov 17 '16 22:11 agentcooper

Tried to add a new method in pdf.py

def hex_to_rgb(value):
    return tuple(int(value[i:i+2], 16)/255.0 for i in (0, 2 ,4))

and a method in PdfFileWriter class
def addFreeTextAnno(self, pagenum, txt, rect, font="Helvetica", bold=False,
                        italic=False, fontSize="14pt", fontColor="ff0000",
                        borderColor="ff0000", bgColor="ffffff"):
        '''
            Helvetica
            Times New Roman
            Courier
            bold italic
            \r return code

        frtxt = DictionaryObject()
        frtxt.update({
            NameObject('/Type'): NameObject('/Annot'),
            NameObject('/Subtype'): NameObject('/FreeText'),
            NameObject('/P'): pageLink,
            NameObject('/Rect'): RectangleObject(rect),
            NameObject('/Contents'): TextStringObject(txt),
            #font size color
            NameObject('/DS'): TextStringObject("font: bold italic Times New Roman 20pt;color:#ff0000"),
            #border color
            NameObject('/DA'): TextStringObject("1 0 0 rg "),
            #background color
            NameObject('/C'): ArrayObject([FloatObject(1), FloatObject(1), FloatObject(1)])
        })
        '''
        pageLink = self.getObject(self._pages)['/Kids'][pagenum]
        pageRef = self.getObject(pageLink)

        fontStr = "font: "
        if bold == True : fontStr = fontStr + "bold "
        if italic == True : fontStr = fontStr + "italic "
        fontStr = fontStr + font + " " + fontSize
        fontStr = fontStr + ";text-align:left;color:#" + fontColor

        bgColorStr = ""
        for st in hex_to_rgb(borderColor):
            bgColorStr = bgColorStr + str(st) + " "
        bgColorStr = bgColorStr + "rg"

        frtxt = DictionaryObject()
        frtxt.update({
            NameObject('/Type'): NameObject('/Annot'),
            NameObject('/Subtype'): NameObject('/FreeText'),
            NameObject('/P'): pageLink,
            NameObject('/Rect'): RectangleObject(rect),
            NameObject('/Contents'): TextStringObject(txt),
            #font size color
            NameObject('/DS'): TextStringObject(fontStr),
            #border color
            NameObject('/DA'): TextStringObject(bgColorStr),
            #background color
            NameObject('/C'): ArrayObject([FloatObject(n) for n in hex_to_rgb(bgColor)])
        })

        lnkRef = self._addObject(frtxt)

        if "/Annots" in pageRef:
            pageRef['/Annots'].append(lnkRef)
        else:
            pageRef[NameObject('/Annots')] = ArrayObject([lnkRef])



from PyPDF2 import PdfFileReader, PdfFileWriter

and following is how to call that method.

def main():
    output = PdfFileWriter()
    input1 = PdfFileReader(file("aaa.pdf", "rb"))

    numPages = input1.getNumPages()

    for index in range(0, numPages):
        pageObj = input1.getPage(index)

        output.addPage(pageObj)
        output.addFreeTextAnno(index, "My Free Text Annotate", [100,100,350,150],
            bold=True, italic=True, fontSize="20pt", fontColor="ffffff", borderColor="ffaa00",
            bgColor="000000", font="Times")

        annot = pageObj["/Annots"][0].getObject()
        annot1 = pageObj["/Annots"][1].getObject()
        print annot
        print annot1
        output.write(open("xx.pdf", 'wb'))

snakemicro avatar Apr 07 '19 10:04 snakemicro

And @snakemicro? Did it work?

rien333 avatar Sep 02 '19 12:09 rien333

Hoping someone can offer some assistance on font size/style.

I'm trying to detect when a value is too long for a field I've got & adjust the font size, but the font doesn't change.

    # Add data to a page
    page = pdf_writer.getPage(0)
    pdf_writer.updatePageFormFieldValues(page, data_dict)

    for j in range(0, len(page['/Annots'])):
        writer_annot = page['/Annots'][j].getObject()
        writer_annot.update({
            NameObject("/DS"): TextStringObject(
                "font: 12.0pt; color:#E52237"
            )
        })

marksweb avatar Mar 19 '20 15:03 marksweb

We might also want to add a PyPDF answer to https://stackoverflow.com/q/47497309/562769

MartinThoma avatar Apr 16 '22 06:04 MartinThoma

I've just added some basic examples. Feel free to add more by creating PRs :-)

MartinThoma avatar Apr 16 '22 11:04 MartinThoma

The demo is here: https://pypdf2.readthedocs.io/en/latest/user/adding-pdf-annotations.html

What I miss:

  1. How to read comments
  2. How to add comments (text + location)
  3. An overview over different types of annotations

MartinThoma avatar Jun 04 '22 09:06 MartinThoma

@snakemicro I would add it https://github.com/py-pdf/PyPDF2/pull/981 :-) I just want to wait for some feedback.

MartinThoma avatar Jun 12 '22 13:06 MartinThoma

@ubuntuslave @snakemicro I've started working on improving the situation in #1198 . Somehow the popup doesn't show... your help would be very appreciated :pray:

MartinThoma avatar Aug 03 '22 21:08 MartinThoma

Number of files with at least one annotations in my big dataset:

:heavy_check_mark: /Link: 2694x :x: /Widget: 431x - https://github.com/py-pdf/PyPDF2/pull/1207 :hourglass_flowing_sand: /Popup: 189x - https://github.com/py-pdf/PyPDF2/pull/1198 - now #1665 :heavy_check_mark: /FreeText: 85x :heavy_check_mark: /Text: 77x :heavy_check_mark: /Square: 47x - #1388 /Stamp: 40x /Highlight: 22x - https://stackoverflow.com/q/9099497/562769 :heavy_check_mark: /Line: 22x /DGAP:RedaxBox: 3x :hourglass_flowing_sand: /Circle: 2x - #1556 /FileAttachment: 2x - don' confuse it with /EmbeddedFiles /StrikeOut: 2x /Underline: 2x /Ink: 1x /Caret: 1x :heavy_check_mark: /Polygon - #1557 /PolyLine

MartinThoma avatar Aug 05 '22 18:08 MartinThoma