Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9f77885

Browse files
author
Ulrich Dobramysl
committed
Add per-page pdf notes in PdfFile and PdfPages.
1 parent 53c2708 commit 9f77885

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

lib/matplotlib/backends/backend_pdf.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
@@ -2445,6 +2463,11 @@ def get_pagecount(self):
24452463
"""
24462464
return len(self._file.pageList)
24472465

2466+
def attach_note(self, text):
2467+
"""
2468+
Add a new text note to the page to be saved next.
2469+
"""
2470+
self._file.newTextnote(text)
24482471

24492472
class FigureCanvasPdf(FigureCanvasBase):
24502473
"""

0 commit comments

Comments
 (0)