How to change the highlight color in pdf using fitz module in python

霸气de小男生 提交于 2021-02-19 03:56:07

问题


Hi I am trying to change the highlight color in a pdf but not able to do so. The default highlight color is yellow but i want to change it Following is my code:

    import fitz

    doc = fitz.open(r"path\input.pdf")

    page=doc[0]
    text="some text"
    text_instances = page.searchFor(text)


    for inst in text_instances:
        highlight = page.addHighlightAnnot(inst)
        highlight.setColors(colors='Red')
        highlight.update()


    doc.save(r"path\output.pdf")    

Also how do i search for the entire pdf together and not just one page

and how can i highlight text on an image given in a pdf


回答1:


I think the setColors expects a dictionary, check the documentation here

import fitz

doc = fitz.open("test.pdf")


page = doc[0]

text = "result"

text_instances = page.searchFor(text)

for inst in text_instances:
    highlight = page.addHighlightAnnot(inst)
    highlight.setColors({"stroke":(0, 0, 1), "fill":(0.75, 0.8, 0.95)})
    highlight.update()


doc.save("output.pdf")



来源:https://stackoverflow.com/questions/60557977/how-to-change-the-highlight-color-in-pdf-using-fitz-module-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!