-
-
Save ntddk/e22ce959082222a8dfab84456b906a2b to your computer and use it in GitHub Desktop.
This file contains 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 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]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment