def TextField(title, value, xmin, ymin, xmax, ymax, page, maxlen=1000000, font="Helvetica-Bold", fontsize=9, R=0, G=0, B=0.627, multiline=0): from reportlab.pdfbase.pdfdoc import PDFString, PDFName Flags = 0 if multiline: Flags = Flags | (1 << 12) # bit 13 is at position 12 :) fontname = FORMFONTNAMES[font] return PDFPattern(TextFieldPattern, value=PDFString(value), maxlen=maxlen, page=page, title=PDFString(title), xmin=xmin, ymin=ymin, xmax=xmax, ymax=ymax, fontname=PDFName(fontname), fontsize=fontsize, R=R, G=G, B=B, Flags=Flags)
def SelectField(title, value, options, xmin, ymin, xmax, ymax, page, font="Helvetica-Bold", fontsize=9, R=0, G=0, B=0.627): #print "ARGS", (title, value, options, xmin, ymin, xmax, ymax, page, font, fontsize, R, G, B) from reportlab.pdfbase.pdfdoc import PDFString, PDFName, PDFArray if value not in options: raise ValueError("value %s must be one of options %s" % (repr(value), repr(options))) fontname = FORMFONTNAMES[font] optionstrings = list(map(PDFString, options)) optionarray = PDFArray(optionstrings) return PDFPattern(SelectFieldPattern, Options=optionarray, Selected=PDFString(value), Page=page, Name=PDFString(title), xmin=xmin, ymin=ymin, xmax=xmax, ymax=ymax, fontname=PDFName(fontname), fontsize=fontsize, R=R, G=G, B=B)
def ButtonField(title, value, xmin, ymin, page, width=16.7704, height=14.907): if value not in ("Yes", "Off"): raise ValueError("button value must be 'Yes' or 'Off': " + repr(value)) fontSize = (11.3086 / 14.907) * height dx = (3.6017 / 16.7704) * width dy = (3.3881 / 14.907) * height return PDFPattern( ButtonFieldPattern, Name=PDFString(title), xmin=xmin, ymin=ymin, xmax=xmin + width, ymax=ymin + width, Hide=PDFPattern(['<< /S /Hide >>']), APDOff=ButtonStream('0.749 g 0 0 %(width)s %(height)s re f\r\n' % vars(), width=width, height=height), APDYes=ButtonStream( '0.749 g 0 0 %(width)s %(height)s re f q 1 1 %(width)s %(height)s re W n BT /ZaDb %(fontSize)s Tf 0 g 1 0 0 1 %(dx)s %(dy)s Tm (4) Tj ET\r\n' % vars(), width=width, height=height), APNYes=ButtonStream( 'q 1 1 %(width)s %(height)s re W n BT /ZaDb %(fontSize)s Tf 0 g 1 0 0 1 %(dx)s %(dy)s Tm (4) Tj ET Q\r\n' % vars(), width=width, height=height), Value=PDFName(value), Page=page)
def format(self,doc): kids = self.kids d = len(kids) if d<2: raise ValueError('RadioGroup:%s has %d < 2 RadioBoxes' % (self.T,d)) d = dict( Ff=self.Ff, Kids = PDFArray([k for k in self.kids]), FT = PDFName('Btn'), T = PDFString(self.T), #DA = PDFString('0 g'), ) if self.V: d['V'] = PDFName(self.V) if self.TU: d['TU'] =PDFString(self.TU) r = PDFDictionary(d).format(doc) return r
def format(self, doc): d = dict(Fields=PDFArray([self.getRef(f) for f in self.fields]), ) if self.fonts: F = [self.fontRef(f) for f in self.fonts] d['DA'] = PDFString('/%s 0 Tf 0 g' % list(self.fonts.keys())[0]) d['DR'] = PDFFromString( '<< /Encoding\n<<\n/RLAFencoding\n%s\n>>\n%s\n>>' % (self.encRefStr, '\n'.join(F))) r = PDFDictionary(d).format(doc) return r
def format(self, doc): d = dict(Fields=PDFArray([self.getRef(f) for f in self.fields]), ) if self.sigFlags: d['SigFlags'] = self.sigFlags if self.fonts: FK = list(sorted(self.fonts.keys())) F = [self.fontRef(f) for f in FK] d['DA'] = PDFString('/%s 0 Tf 0 g' % FK[0]) d['DR'] = PDFFromString( '<< /Encoding\n<<\n/RLAFencoding\n%s\n>>\n%s\n>>' % (self.encRefStr, '\n'.join(F))) d.update(self.extras) r = PDFDictionary(d).format(doc) return r
def ButtonField(title, value, xmin, ymin, page): if value not in ("Yes", "Off"): raise ValueError, "button value must be 'Yes' or 'Off': "+repr(value) (dx, dy) = (16.77036, 14.90698) return PDFPattern(ButtonFieldPattern, Name=PDFString(title), xmin=xmin, ymin=ymin, xmax=xmin+dx, ymax=ymin+dy, Hide=HIDE, APDOff=APDOFF, APDYes=APDYES, APNYes=APNYES, Value=PDFName(value), Page=page)
def format(self, document): d = {"Title": PDFString(self.title), "Author": PDFString(self.author), "CreationDate": PDFDate(invariant=self.invariant, dateFormatter=self._dateFormatter), "Producer": PDFString(self.producer)} if self.pdfxversion: d["GTS_PDFXVersion"] = PDFString(self.pdfxversion) d["Creator"] = PDFString(self.creator) d["Subject"] = PDFString(self.subject) d["Keywords"] = PDFString(self.keywords) pd = PDFDictionary(d) return pd.format(document)
def format(self, document): D = {} D["Title"] = PDFString(self.title) D["Author"] = PDFString(self.author) D["CreationDate"] = PDFDate(invariant=self.invariant, dateFormatter=self._dateFormatter) D["Producer"] = PDFString(self.producer) if self.pdfxversion: D["GTS_PDFXVersion"] = PDFString(self.pdfxversion) D["Creator"] = PDFString(self.creator) D["Subject"] = PDFString(self.subject) D["Keywords"] = PDFString(self.keywords) PD = PDFDictionary(D) return PD.format(document)
def _textfield( self, value='', fillColor=None, borderColor=None, textColor=None, borderWidth=1, borderStyle='solid', width=120, height=36, x=0, y=0, tooltip=None, name=None, annotationFlags='print', fieldFlags='', forceBorder=False, relative=False, maxlen=100, fontName=None, fontSize=None, wkind=None, options=None, dashLen=3, ): rFontName, iFontName = self.makeFont(fontName) if fontSize is None: fontSize = 12 textColor, borderColor, fillColor = self.stdColors( textColor, borderColor, fillColor) canv = self.canv if relative: x, y = self.canv.absolutePosition(x, y) doc = canv._doc rFontName = '<</%s %s>>' % (iFontName, rFontName) Ff = makeFlags(fieldFlags, fieldFlagValues) if wkind != 'textfield': #options must be a list of pairs (label value) #value must be a list of the values FT = 'Ch' if wkind == 'choice': Ff |= fieldFlagValues['combo'] #just in case V = [] Opt = [] AP = [] I = [] TF = [] if not isinstance(options, (list, tuple)): raise TypeError('%s options=%r is wrong type' % (wkind, options)) for v in options: if isStr(v): Opt.append(PDFString(v)) l = v elif isinstance(v, (list, tuple)): if len(v) == 1: v = l = v[0] else: l, v = v Opt.append(PDFArray([PDFString(v), PDFString(l)])) else: raise TypeError('%s option %r is wrong type' % (wkind, v)) AP.append(v) TF.append(l) Opt = PDFArray(Opt) if value: if not isinstance(value, (list, tuple)): value = [value] for v in value: if v not in AP: if v not in TF: raise ValueError( '%s value %r is not in option\nvalues %r\nor labels %r' % (wkind, v, AP, TF)) else: v = AP[TF.index(v)] I.append(AP.index(v)) V.append(PDFString(v)) I.sort() if not (Ff & fieldFlagValues['multiSelect']) or len(value) == 1: if wkind == 'choice': value = TF[I[0]] else: value = value[:1] V = V[:1] V = V[0] if len(V) == 1 else PDFArray(V) lbextras = dict(labels=TF, I=I, wkind=wkind) else: V = PDFString(value) else: I = Opt = [] lbextras = {} FT = 'Tx' if not isStr(value): raise TypeError('textfield value=%r is wrong type' % value) V = PDFString(value) AP = {} for key in 'N': tC, bC, fC = self.varyColors(key, textColor, borderColor, fillColor) ap = self.txAP(key, value, iFontName, rFontName, fontSize, fillColor=fC, borderColor=bC, textColor=tC, borderWidth=borderWidth, borderStyle=borderStyle, width=width, height=height, dashLen=dashLen, **lbextras) if ap._af_refstr in self._refMap: ref = self._refMap[ap._af_refstr] else: ref = self.getRef(ap) self._refMap[ap._af_refstr] = ref AP[key] = ref TF = dict( FT=PDFName(FT), P=doc.thisPageRef(), V=V, #AS = PDFName(value), DV=V, Rect=PDFArray((x, y, x + width, y + height)), AP=PDFDictionary(AP), Subtype=PDFName('Widget'), Type=PDFName('Annot'), F=makeFlags(annotationFlags, annotationFlagValues), Ff=Ff, #H=PDFName('N'), DA=PDFString( '/%s %d Tf %s' % (iFontName, fontSize, self.streamFillColor(textColor))), ) if Opt: TF['Opt'] = Opt if I: TF['I'] = PDFArray(I) if maxlen: TF['MaxLen'] = maxlen if tooltip: TF['TU'] = PDFString(tooltip) if not name: name = 'AFF%03d' % len(self.fields) TF['T'] = PDFString(name) MK = dict(BG=PDFArray(self.colorTuple(fillColor)), ) # Acrobat seems to draw a thin border when BS is defined, so only # include this if there actually is a border to draw if borderWidth: TF['BS'] = bsPDF(borderWidth, borderStyle, dashLen) MK['BC'] = PDFArray(self.colorTuple(borderColor)) TF['MK'] = PDFDictionary(MK) TF = PDFDictionary(TF) self.canv._addAnnotation(TF) self.fields.append(self.getRef(TF)) self.checkForceBorder(x, y, width, height, forceBorder, 'square', borderStyle, borderWidth, borderColor, fillColor)
def checkbox( self, checked=False, buttonStyle='check', shape='square', fillColor=None, borderColor=None, textColor=None, borderWidth=1, borderStyle='solid', size=20, x=0, y=0, tooltip=None, name=None, annotationFlags='print', fieldFlags='required', forceBorder=False, relative=False, dashLen=3, ): initialValue = 'Yes' if checked else 'Off' textColor, borderColor, fillColor = self.stdColors( textColor, borderColor, fillColor) canv = self.canv if relative: x, y = self.canv.absolutePosition(x, y) doc = canv._doc AP = {} for key in 'NDR': APV = {} tC, bC, fC = self.varyColors(key, textColor, borderColor, fillColor) for value in ('Yes', 'Off'): ap = self.checkboxAP( key, value, buttonStyle=buttonStyle, shape=shape, fillColor=fC, borderColor=bC, textColor=tC, borderWidth=borderWidth, borderStyle=borderStyle, size=size, dashLen=dashLen, ) if ap._af_refstr in self._refMap: ref = self._refMap[ap._af_refstr] else: ref = self.getRef(ap) self._refMap[ap._af_refstr] = ref APV[value] = ref AP[key] = PDFDictionary(APV) del APV CB = dict( FT=PDFName('Btn'), P=doc.thisPageRef(), V=PDFName(initialValue), AS=PDFName(initialValue), #DV = PDFName(initialValue), Rect=PDFArray((x, y, x + size, y + size)), AP=PDFDictionary(AP), Subtype=PDFName('Widget'), Type=PDFName('Annot'), F=makeFlags(annotationFlags, annotationFlagValues), Ff=makeFlags(fieldFlags, fieldFlagValues), H=PDFName('N'), ) if tooltip: CB['TU'] = PDFString(tooltip) if not name: name = 'AFF%03d' % len(self.fields) if borderWidth: CB['BS'] = bsPDF(borderWidth, borderStyle, dashLen) CB['T'] = PDFString(name) MK = dict( CA='(%s)' % ZDSyms[buttonStyle], BC=PDFArray(self.colorTuple(borderColor)), BG=PDFArray(self.colorTuple(fillColor)), ) CB['MK'] = PDFDictionary(MK) CB = PDFDictionary(CB) self.canv._addAnnotation(CB) self.fields.append(self.getRef(CB)) self.checkForceBorder(x, y, size, size, forceBorder, shape, borderStyle, borderWidth, borderColor, fillColor)
def format(self, document): from reportlab.pdfbase.pdfdoc import PDFArray proxy = PDFPattern(FormPattern, Resources=getattr(self, 'resources', None) or FormResources(), NeedAppearances=getattr(self, 'needAppearances', 'false'), fields=PDFArray(self.fields), SigFlags=getattr(self, 'sigFlags', 0)) return proxy.format(document) FormPattern = [ '<<\r\n', '/NeedAppearances ', ['NeedAppearances'], '\r\n' '/DA ', PDFString('/Helv 0 Tf 0 g '), '\r\n', '/DR ', ["Resources"], '\r\n', '/Fields ', ["fields"], '\r\n', PDFPatternIf('SigFlags', ['\r\n/SigFlags ', ['SigFlags']]), '>>' ] def FormFontsDictionary(): from reportlab.pdfbase.pdfdoc import PDFDictionary fontsdictionary = PDFDictionary() fontsdictionary.__RefOnly__ = 1 for fullname, shortname in FORMFONTNAMES.items(): fontsdictionary[shortname] = FormFont(fullname, shortname) fontsdictionary["ZaDb"] = PDFPattern(ZaDbPattern) return fontsdictionary
page = doc.thisPageRef() field = ButtonField(title, value, xmin, ymin, page) self.fields.append(field) canvas._addAnnotation(field) def format(self, document): from reportlab.pdfbase.pdfdoc import PDFArray proxy = PDFPattern(FormPattern, Resources=GLOBALRESOURCES, fields=PDFArray(self.fields)) return proxy.format(document) FormPattern = [ '<<', LINEEND, ' /NeedAppearances true ', LINEEND, ' /DA ', PDFString('/Helv 0 Tf 0 g '), LINEEND, ' /DR ', LINEEND, ["Resources"], ' /Fields ', LINEEND, ["fields"], '>>' ] def FormFontsDictionary(): from reportlab.pdfbase.pdfdoc import PDFDictionary fontsdictionary = PDFDictionary() fontsdictionary.__RefOnly__ = 1 for (fullname, shortname) in FORMFONTNAMES.items(): fontsdictionary[shortname] = FormFont(fullname, shortname) fontsdictionary["ZaDb"] = ZADB return fontsdictionary def FormResources():
def buttonField(self, canvas, title, value, xmin, ymin): # determine the page ref doc = canvas._doc page = doc.thisPageRef() field = ButtonField(title, value, xmin, ymin, page) self.fields.append(field) canvas._addAnnotation(field) def format(self, document): from reportlab.pdfbase.pdfdoc import PDFArray proxy = PDFPattern(FormPattern, Resources=GLOBALRESOURCES, fields=PDFArray(self.fields)) return proxy.format(document) FormPattern = [ '<<', LINEEND, ' /NeedAppearances true ', LINEEND, ' /DA ', PDFString('/Helv 0 Tf 0 g '), LINEEND, ' /DR ', LINEEND, ["Resources"], ' /Fields ', LINEEND, ["fields"], '>>' ] def FormFontsDictionary(): from reportlab.pdfbase.pdfdoc import PDFDictionary fontsdictionary = PDFDictionary() fontsdictionary.__RefOnly__ = 1 for (fullname, shortname) in FORMFONTNAMES.items(): fontsdictionary[shortname] = FormFont(fullname, shortname) fontsdictionary["ZaDb"] = ZADB return fontsdictionary
def set_defaults(self): self.dict.setdefault('Type', PDFString('LGIDict')) self.dict.setdefault('Version', PDFString('2.1')) self.dict.setdefault('Projection', Projection({'Datum': PDFString('WE')}))
def set_defaults(self): self.dict.setdefault('ProjectionType', PDFString('GEOGRAPHIC')) self.dict.setdefault('Type', PDFName('Projection'))