@@ -480,6 +480,9 @@ def __init__(self, filename):
480480
481481 self .paths = []
482482
483+ self .pageAnnotations = [] # A list of annotations for the
484+ # current page
485+
483486 # The PDF spec recommends to include every procset
484487 procsets = [Name (x )
485488 for x in "PDF Text ImageB ImageC ImageI" .split ()]
@@ -507,7 +510,8 @@ def newPage(self, width, height):
507510 'Contents' : contentObject ,
508511 'Group' : {'Type' : Name ('Group' ),
509512 'S' : Name ('Transparency' ),
510- 'CS' : Name ('DeviceRGB' )}
513+ 'CS' : Name ('DeviceRGB' )},
514+ 'Annots' : self .pageAnnotations ,
511515 }
512516 pageObject = self .reserveObject ('page' )
513517 self .writeObject (pageObject , thePage )
@@ -519,6 +523,20 @@ def newPage(self, width, height):
519523 # graphics context: currently only the join style needs to be set
520524 self .output (GraphicsContextPdf .joinstyles ['round' ], Op .setlinejoin )
521525
526+ # Clear the list of annotations for the next page
527+ self .pageAnnotations = []
528+
529+ def newTextnote (self , text , positionRect = [- 100 , - 100 , 0 , 0 ]):
530+ # Create a new annotation of type text
531+ theNote = {'Type' : Name ('Annot' ),
532+ 'Subtype' : Name ('Text' ),
533+ 'Contents' : text ,
534+ 'Rect' : positionRect ,
535+ }
536+ annotObject = self .reserveObject ('annotation' )
537+ self .writeObject (annotObject , theNote )
538+ self .pageAnnotations .append (annotObject )
539+
522540 def close (self ):
523541 self .endStream ()
524542 # Write out the various deferred objects
@@ -2447,6 +2465,15 @@ def get_pagecount(self):
24472465 """
24482466 return len (self ._file .pageList )
24492467
2468+ def attach_note (self , text , positionRect = [- 100 , - 100 , 0 , 0 ]):
2469+ """
2470+ Add a new text note to the page to be saved next. The optional
2471+ positionRect specifies the position of the new note on the
2472+ page. It is outside the page per default to make sure it is
2473+ invisible on printouts.
2474+ """
2475+ self ._file .newTextnote (text , positionRect )
2476+
24502477
24512478class FigureCanvasPdf (FigureCanvasBase ):
24522479 """
0 commit comments