Created
November 17, 2016 22:50
-
-
Save agentcooper/4c55133f5d95866acdee5017cd318558 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PyPDF2 import PdfFileWriter, PdfFileReader | |
from PyPDF2Highlight import createHighlight, addHighlightToPage | |
pdfInput = PdfFileReader(open("input.pdf", "rb")) | |
pdfOutput = PdfFileWriter() | |
page1 = pdfInput.getPage(0) | |
highlight = createHighlight(100, 400, 400, 500, { | |
"author": "", | |
"contents": "Bla-bla-bla" | |
}) | |
addHighlightToPage(highlight, page1, pdfOutput) | |
pdfOutput.addPage(page1) | |
outputStream = open("output.pdf", "wb") | |
pdfOutput.write(outputStream) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PyPDF2.generic import ( | |
DictionaryObject, | |
NumberObject, | |
FloatObject, | |
NameObject, | |
TextStringObject, | |
ArrayObject | |
) | |
# x1, y1 starts in bottom left corner | |
def createHighlight(x1, y1, x2, y2, meta, color = [1, 0, 0]): | |
newHighlight = DictionaryObject() | |
newHighlight.update({ | |
NameObject("/F"): NumberObject(4), | |
NameObject("/Type"): NameObject("/Annot"), | |
NameObject("/Subtype"): NameObject("/Highlight"), | |
NameObject("/T"): TextStringObject(meta["author"]), | |
NameObject("/Contents"): TextStringObject(meta["contents"]), | |
NameObject("/C"): ArrayObject([FloatObject(c) for c in color]), | |
NameObject("/Rect"): ArrayObject([ | |
FloatObject(x1), | |
FloatObject(y1), | |
FloatObject(x2), | |
FloatObject(y2) | |
]), | |
NameObject("/QuadPoints"): ArrayObject([ | |
FloatObject(x1), | |
FloatObject(y2), | |
FloatObject(x2), | |
FloatObject(y2), | |
FloatObject(x1), | |
FloatObject(y1), | |
FloatObject(x2), | |
FloatObject(y1) | |
]), | |
}) | |
return newHighlight | |
def addHighlightToPage(highlight, page, output): | |
highlight_ref = output._addObject(highlight); | |
if "/Annots" in page: | |
page[NameObject("/Annots")].append(highlight_ref) | |
else: | |
page[NameObject("/Annots")] = ArrayObject([highlight_ref]) |
Updated for new versions of PyPDF2
that don't support PDFFileReader
and PDFFileWriter
:
from PyPDF2 import PdfReader, PdfWriter
from PyPDF2.generic import (
DictionaryObject,
NumberObject,
FloatObject,
NameObject,
TextStringObject,
ArrayObject
)
def createHighlight(x1, y1, x2, y2, meta, color=[1, 0, 0]):
newHighlight = DictionaryObject()
newHighlight.update({
NameObject("/F"): NumberObject(4),
NameObject("/Type"): NameObject("/Annot"),
NameObject("/Subtype"): NameObject("/Highlight"),
NameObject("/T"): TextStringObject(meta["author"]),
NameObject("/Contents"): TextStringObject(meta["contents"]),
NameObject("/C"): ArrayObject([FloatObject(c) for c in color]),
NameObject("/Rect"): ArrayObject([
FloatObject(x1),
FloatObject(y1),
FloatObject(x2),
FloatObject(y2)
]),
NameObject("/QuadPoints"): ArrayObject([
FloatObject(x1),
FloatObject(y2),
FloatObject(x2),
FloatObject(y2),
FloatObject(x1),
FloatObject(y1),
FloatObject(x2),
FloatObject(y1)
]),
})
return newHighlight
def addHighlightToPage(highlight, page, writer):
# Add the highlight annotation to the specified page
if "/Annots" in page:
page["/Annots"].append(highlight)
else:
page[NameObject("/Annots")] = ArrayObject([highlight])
@rbehal You should no longer use PyPDF2 as it's deprecated. Use pypdf. It supports highlights out of the box: https://pypdf.readthedocs.io/en/latest/user/adding-pdf-annotations.html#highlighting
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is very helpful! I took it bit further and use pdfminer to find the text that you're trying to highlight, optionally constraining that search space to a bounding box. PDF makes this incredibly complicated - you have to find the coordinates of every letter on the page, cluster that into bounding boxes line-by-line, and then highlight that polygon. Hope this code helps someone the way @agentcooper's helped me.
You can then take the result of
highlight_annotation
and pass that on towriter.add_annotation
as @MartinThoma suggested