Skip to content

Instantly share code, notes, and snippets.

@here0009
Forked from agentcooper/PyPDF2Highlight.py
Created March 17, 2017 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save here0009/90fb77c7e7956e7adc5c6d9cfc30fd0f to your computer and use it in GitHub Desktop.
Save here0009/90fb77c7e7956e7adc5c6d9cfc30fd0f to your computer and use it in GitHub Desktop.
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)
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