Add an oval shape into a PDF surrounding the highlighted text
Question:
import fitz
def highlight_text(pdf_file, room_id):
for page in pdf_file:
### SEARCH
text = str(room_id)
print(text)
text_instances = page.search_for(text)
### HIGHLIGHT
for inst in text_instances:
highlight = page.add_highlight_annot(inst)
highlight.set_colors({"stroke":(0, 1, 0), "thickness": 2}) # <-- change thickness here
highlight.update()
print(highlight)
# create a shape object
shape = page.new_shape()
# draw a circle with width=5 (change it to whatever you want)
shape.draw_circle(((inst.x0+inst.x1)/2, (inst.y0+inst.y1)/2), 70)
shape.finish(color=(0,1,0), fill=None, width=5)
# update the page with the drawing
shape.commit()
### OUTPUT
pdf_file.save("highlighted.pdf", garbage=4, deflate=True, clean=True)
pdf = fitz.open("OK31017-ML0100-DWG-ZA-B1.pdf")
highlight_text(pdf, "MECH PUMP ROOM (WATER FEATURE 12)")
I have tried a rectangle to surround the highlighted text. Now, I would like to change it to a circle, and increase the stroke thickness, so that it will be more visible.
I’m using Python 3
.
Answers:
You can use Shape
object to draw circle:
def highlight_text(self, pdf_file, room_id):
for page in pdf_file:
### SEARCH
text = str(room_id)
print(text)
text_instances = page.search_for(text)
### HIGHLIGHT
for inst in text_instances:
highlight = page.add_highlight_annot(inst)
highlight.set_colors({"stroke":(0, 1, 0), "thickness": 2}) # <-- change thickness here
highlight.update()
print(highlight)
# create a shape object
shape = page.new_shape()
# draw a circle with width=5 (change it to whatever you want)
shape.draw_circle(((inst.x0+inst.x1)/2, (inst.y0+inst.y1)/2), 70)
shape.finish(color=(0,1,0), fill=None, width=5)
# update the page with the drawing
shape.commit()
### OUTPUT
pdf_file.save("highlighted.pdf", garbage=4, deflate=True, clean=True)
Above is the result of below code:
import fitz
...
pdf = fitz.open("table.pdf")
self.highlight_text(pdf, "12345678")
...
Found an answer,
circle_center = fitz.Point((inst.tl.x + inst.br.x)/2, (inst.tl.y + inst.br.y)/2)
circle_width = 300
circle_height = 150
circle_bbox = fitz.Rect(circle_center.x - circle_width/2, circle_center.y - circle_height/2,
circle_center.x + circle_width/2, circle_center.y + circle_height/2)
circle_annot = page.add_circle_annot(circle_bbox)
circle_annot.set_colors(stroke=(1,0,0), fill=(1, 1, 0))
circle_annot.set_border(width=10)
circle_annot.set_opacity(0.7)
circle_annot.border_width = 10
circle_annot.update() ```
import fitz
def highlight_text(pdf_file, room_id):
for page in pdf_file:
### SEARCH
text = str(room_id)
print(text)
text_instances = page.search_for(text)
### HIGHLIGHT
for inst in text_instances:
highlight = page.add_highlight_annot(inst)
highlight.set_colors({"stroke":(0, 1, 0), "thickness": 2}) # <-- change thickness here
highlight.update()
print(highlight)
# create a shape object
shape = page.new_shape()
# draw a circle with width=5 (change it to whatever you want)
shape.draw_circle(((inst.x0+inst.x1)/2, (inst.y0+inst.y1)/2), 70)
shape.finish(color=(0,1,0), fill=None, width=5)
# update the page with the drawing
shape.commit()
### OUTPUT
pdf_file.save("highlighted.pdf", garbage=4, deflate=True, clean=True)
pdf = fitz.open("OK31017-ML0100-DWG-ZA-B1.pdf")
highlight_text(pdf, "MECH PUMP ROOM (WATER FEATURE 12)")
I have tried a rectangle to surround the highlighted text. Now, I would like to change it to a circle, and increase the stroke thickness, so that it will be more visible.
I’m using Python 3
.
You can use Shape
object to draw circle:
def highlight_text(self, pdf_file, room_id):
for page in pdf_file:
### SEARCH
text = str(room_id)
print(text)
text_instances = page.search_for(text)
### HIGHLIGHT
for inst in text_instances:
highlight = page.add_highlight_annot(inst)
highlight.set_colors({"stroke":(0, 1, 0), "thickness": 2}) # <-- change thickness here
highlight.update()
print(highlight)
# create a shape object
shape = page.new_shape()
# draw a circle with width=5 (change it to whatever you want)
shape.draw_circle(((inst.x0+inst.x1)/2, (inst.y0+inst.y1)/2), 70)
shape.finish(color=(0,1,0), fill=None, width=5)
# update the page with the drawing
shape.commit()
### OUTPUT
pdf_file.save("highlighted.pdf", garbage=4, deflate=True, clean=True)
Above is the result of below code:
import fitz
...
pdf = fitz.open("table.pdf")
self.highlight_text(pdf, "12345678")
...
Found an answer,
circle_center = fitz.Point((inst.tl.x + inst.br.x)/2, (inst.tl.y + inst.br.y)/2)
circle_width = 300
circle_height = 150
circle_bbox = fitz.Rect(circle_center.x - circle_width/2, circle_center.y - circle_height/2,
circle_center.x + circle_width/2, circle_center.y + circle_height/2)
circle_annot = page.add_circle_annot(circle_bbox)
circle_annot.set_colors(stroke=(1,0,0), fill=(1, 1, 0))
circle_annot.set_border(width=10)
circle_annot.set_opacity(0.7)
circle_annot.border_width = 10
circle_annot.update() ```