Added highlight annotations
authorbenechols-wf <ben.echols@webfilings.com>
Tue, 09 Jul 2013 11:38:52 -0600
changeset 3711 49cde747af53
parent 3710 b082fcc3a2ac
child 3712 960245b5737e
Added highlight annotations
src/reportlab/pdfbase/pdfdoc.py
src/reportlab/pdfgen/canvas.py
--- a/src/reportlab/pdfbase/pdfdoc.py	Thu Jul 04 15:45:38 2013 +0100
+++ b/src/reportlab/pdfbase/pdfdoc.py	Tue Jul 09 11:38:52 2013 -0600
@@ -1721,6 +1721,51 @@
         d["Dest"] = self.Destination
         return self.AnnotationDict(**d)
 
+class HighlightAnnotation(Annotation):
+    """
+    HighlightAnnotation adds a highlighted
+    """
+    permitted = Annotation.permitted + ("QuadPoints", )
+
+    def __init__(self, Rect, Contents, QuadPoints, Color=[0.83, 0.89, 0.95], **kw):
+        self.Rect = Rect
+        self.Contents = Contents
+        self.otherkw = kw
+        self.QuadPoints = QuadPoints
+        self.Color = Color
+
+    def cvtdict(self, d, escape=1):
+        """transform dict args from python form to pdf string rep as needed"""
+        Rect = d["Rect"]
+        Quad = d["QuadPoints"]
+        Color = d["C"]
+        if type(Rect) is not types.StringType:
+            d["Rect"] = PDFArray(Rect)
+        if type(Quad) is not types.StringType:
+            d["QuadPoints"] = PDFArray(Quad)
+        if type(Color) is not types.StringType:
+            d["C"] = PDFArray(Color)
+        d["Contents"] = PDFString(d["Contents"],escape)
+        return d
+
+    def Dict(self):
+        d = {}
+        d.update(self.otherkw)
+        d["Rect"] = self.Rect
+        d["Contents"] = self.Contents
+        d["Subtype"] = "/Highlight"
+        d["QuadPoints"] = self.QuadPoints
+        d["C"] = self.Color
+        return self.AnnotationDict(**d)
+
+
+def rect_to_quad(Rect):
+    """
+    Utility method to convert a Rect to a QuadPoint
+    """
+    return [Rect[0], Rect[1], Rect[2], Rect[1],
+            Rect[0], Rect[3], Rect[2], Rect[3]]
+
 # skipping names tree
 # skipping actions
 # skipping names trees
--- a/src/reportlab/pdfgen/canvas.py	Thu Jul 04 15:45:38 2013 +0100
+++ b/src/reportlab/pdfgen/canvas.py	Tue Jul 09 11:38:52 2013 -0600
@@ -1091,6 +1091,13 @@
         self._addAnnotation(pdfdoc.TextAnnotation(Rect, contents, **kw), name, addtopage)
     textAnnotation0 = textAnnotation    #deprecated
 
+    def highlightAnnotation(self, contents, Rect, QuadPoints=None, Color=[0.83, 0.89, 0.95], addtopage=1,
+                            name=None, relative=0, **kw):
+        Rect = self._absRect(Rect, relative)
+        if not QuadPoints:
+            QuadPoints = pdfdoc.rect_to_quad(Rect)
+        self._addAnnotation(pdfdoc.HighlightAnnotation(Rect, contents, QuadPoints, Color, **kw), name, addtopage)
+
     def inkAnnotation(self, contents, InkList=None, Rect=None, addtopage=1, name=None, relative=0, **kw):
         raise NotImplementedError
         "Experimental"