Skip to content

Commit

Permalink
Add support for text annotations
Browse files Browse the repository at this point in the history
Support PDF annotations via the TextAnnotation raw directive in this
format:

    .. raw:: pdf

        TextAnnotation "My content here"

This is useful for speaker notes in Présentation.
  • Loading branch information
akrabat committed Dec 7, 2017
1 parent 281683c commit fa48c7d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
16 changes: 16 additions & 0 deletions rst2pdf/flowables.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,22 @@ def draw(self):
def wrap(self, aW, aH):
return 0,0

class TextAnnotation(Flowable):
"""Add text annotation flowable"""

def __init__(self, *args):
if len(args) < 1:
args = [None, 1] # No transition
self.annotationText = args[0].lstrip('"').rstrip('"')

def wrap(self, w, h):
return 0, 0

def draw(self):
# Format of Reportlab's textAnnotation():
# textAnnotation("Your content", Rect=[x_begin, y_begin, x_end, y_end], relative=1)
self.canv.textAnnotation(self.annotationText, [-1, -1, -1, -1], 1)

class Transition(Flowable):
"""Wrap canvas.setPageTransition.
Expand Down
5 changes: 5 additions & 0 deletions rst2pdf/tests/input/textannotation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The `TextAnnotation` directive add an annotation to the PDF

.. raw:: pdf

TextAnnotation "This is an annotation"
9 changes: 9 additions & 0 deletions rst2pdf/tests/md5/textannotation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
bad_md5 = [
'sentinel'
]

good_md5 = [
'15759227f97f019679a39c0d893e3c68',
'sentinel'
]

Binary file added rst2pdf/tests/reference/textannotation.pdf
Binary file not shown.
9 changes: 8 additions & 1 deletion rst2pdf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ def parseRaw(data, node):
Supported (can add others on request):
* PageBreak
* EvenPageBreak
* OddPageBreak
* FrameBreak
* Spacer width, height
* Transition
* SetPageCounter
* TextAnnotation "text of annotation"
"""
elements = []
Expand Down Expand Up @@ -57,6 +62,8 @@ def parseRaw(data, node):
elements.append(Transition(*tokens[1:]))
elif command == 'SetPageCounter':
elements.append(flowables.PageCounter(*tokens[1:]))
elif command == 'TextAnnotation':
elements.append(TextAnnotation(*tokens[1:]))
else:
log.error('Unknown command %s in raw pdf directive [%s]'%(command,nodeid(node)))
return elements
Expand Down

0 comments on commit fa48c7d

Please sign in to comment.