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

Skip to content

Commit d4289db

Browse files
committed
Merge branch 'pdf-annotations'
This is ulido:pdf-annotations with a small documentation addition to advertise it a little bit better. Closes matplotlib#3994
2 parents aa45e26 + 6f99eea commit d4289db

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Per-page pdf notes in multi-page pdfs (PdfPages)
2+
------------------------------------------------
3+
4+
Add a new method attach_note to the PdfPages class, allowing the
5+
attachment of simple text notes to pages in a multi-page pdf of
6+
figures. The new note is visible in the list of pdf annotations in a
7+
viewer that has this facility (Adobe Reader, OSX Preview, Skim,
8+
etc.). Per default the note itself is kept off-page to prevent it to
9+
appear in print-outs.
10+
11+
PdfPages.attach_note needs to be called before savefig in order to be
12+
added to the correct figure.

examples/pylab_examples/multipage_pdf.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
# This is a demo of creating a pdf file with several pages.
1+
"""
2+
This is a demo of creating a pdf file with several pages,
3+
as well as adding metadata and annotations to pdf files.
4+
"""
25

36
import datetime
47
import numpy as np
@@ -20,6 +23,8 @@
2023
x = np.arange(0, 5, 0.1)
2124
plt.plot(x, np.sin(x), 'b-')
2225
plt.title('Page Two')
26+
pdf.attach_note("plot of sin(x)") # you can add a pdf note to
27+
# attach metadata to a page
2328
pdf.savefig()
2429
plt.close()
2530

lib/matplotlib/backends/backend_pdf.py

Lines changed: 28 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
@@ -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

24512478
class FigureCanvasPdf(FigureCanvasBase):
24522479
"""

0 commit comments

Comments
 (0)