[Question] How to keep highlight when converting pdf to image #1413
-
Configuration
Hello, I've realised that once I've done the highlight annotation in the pdf I cannot keep it if I want to save it as an image. It automatically disappears. Is there a way of doing so? The code:
There is no error. It just doesn't keep the highlighted sentence. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Page pixmap creation by default is with annotations included. So you should see them. BTW: If you save a pixmap, you cannot directly use JPEG as output format: MuPDF doen't support this. But you could use But the following does work as desired: >>> doc=fitz.open("search.pdf")
>>> page=doc[0]
>>> annot = page.add_highlight_annot(page.search_for("beteigeuze"))
>>> annot.set_colors(stroke=(0,1,0))
>>> annot.update()
>>> pix =page.get_pixmap()
>>> pix.save("beteigeuze.png")
>>> doc.save("beteigeuze.pdf")
>>> The PDF looks the same. |
Beta Was this translation helpful? Give feedback.
Page pixmap creation by default is with annotations included. So you should see them.
The only possible hickup is that right after an
annot.update()
MuPDF won't always have become aware of the changes PyMuPDF has inserted in the annotation.So it is best to reload the page before taking the pixmap:
page = doc.reload_page(page)
.But after going to a different page, or after opening the PDF after such changes will do the same job.
If you save the document, all outstanding changes should be saved as well.
BTW: If you save a pixmap, you cannot directly use JPEG as output format: MuPDF doen't support this. But you could use
pil_save()
instead.But the following does work as desired: