简体   繁体   中英

PyMuPDF how do I remove annotations?

I am using PyMuPDF and trying to loop through a list of strings and highlight them before taking an image and moving to the next string.

The code below does what I need but the annotation remains after each loop and I would like to remove them after the image is taken.
An example image below shows the word "command" highlighted but the previous strings "Images" and "filename" are still highlighted, since I will have hundreds of these images compiled into a report, I would like to make it stand out more clearly.

Is there something like page.remove(highlight)?

pymupdf 输出示例图像

for pi in range(doc.pageCount):
    page = doc[pi]
    for tag in text_list:

        text = tag
        text_instances = page.searchFor(text)

        five_percent_height = (page.rect.br.y - page.rect.tl.y)*0.05
        five_percent_width = (page.rect.br.x - page.rect.tl.x)*0.05

        for inst in text_instances:
            inst_counter += 1
            highlight = page.addSquigglyAnnot(inst)            

            tl_pt = fitz.Point(max(page.rect.tl.x, inst.tl.x - five_percent_width), max(page.rect.tl.y, inst.tl.y - five_percent_height))
            br_pt = fitz.Point(min(page.rect.br.x, inst.br.x + five_percent_width), min(page.rect.br.y, inst.br.y + five_percent_height))

            hl_clip = fitz.Rect(tl_pt, br_pt)

            zoom_mat = fitz.Matrix(4, 4)
            pix = page.getPixmap(matrix=zoom_mat, clip = hl_clip)
            >I want to remove the annotation here

I found an acceptable solution was to just set the opacity to 0% after taking the screenshot.

pix = page.getPixmap(matrix=zoom_mat, clip = hl_clip)
highlight.setOpacity(0)
highlight.update()

Do this:

annot = page.firstAnnot
while annot:
    annot = page.deleteAnnot(annot)

The method delivers the annotation following the deleted one.

Jorj's approach is good. However, from the documentation there are other options:

https://pymupdf.readthedocs.io/en/latest/faq.html#how-to-read-and-update-pdf-objects

This method can also be used to remove a key from the xref dictionary by setting its value to null: The following will remove the rotation specification from the page: doc.xref_set_key(page.xref, "Rotate", "null") . Similarly, to remove all links, annotations and fields from a page, use doc.xref_set_key(page.xref, "Annots", "null") . Because Annots by definition is an array, setting en empty array with the statement doc.xref_set_key(page.xref, "Annots", "[]") would do the same job in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM