Example #1
0
 /**
  * Creates a button field
  * @param $name (string) field name
  * @param $w (int) width
  * @param $h (int) height
  * @param $caption (string) caption.
  * @param $action (mixed) action triggered by pressing the button. Use a string to specify a javascript action. Use an array to specify a form action options as on section 12.7.5 of PDF32000_2008.
  * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  * @param $x (float) Abscissa of the upper-left corner of the rectangle
  * @param $y (float) Ordinate of the upper-left corner of the rectangle
  * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  * @public
  * @author Nicola Asuni
  * @since 4.8.000 (2009-09-07)
  */
 public function Button($name, $w, $h, $caption, $action, $prop = array(), $opt = array(), $x = '', $y = '', $js = false)
 {
     if ($x === '') {
         $x = $this->x;
     }
     if ($y === '') {
         $y = $this->y;
     }
     // check page for no-write regions and adapt page margins if necessary
     list($x, $y) = $this->checkPageRegions($h, $x, $y);
     if ($js) {
         $this->_addfield('button', $name, $this->x, $this->y, $w, $h, $prop);
         $this->javascript .= 'f' . $name . ".buttonSetCaption('" . addslashes($caption) . "');\n";
         $this->javascript .= 'f' . $name . ".setAction('MouseUp','" . addslashes($action) . "');\n";
         $this->javascript .= 'f' . $name . ".highlight='push';\n";
         $this->javascript .= 'f' . $name . ".print=false;\n";
         return;
     }
     // get default style
     $prop = array_merge($this->getFormDefaultProp(), $prop);
     $prop['Pushbutton'] = 'true';
     $prop['highlight'] = 'push';
     $prop['display'] = 'display.noPrint';
     // get annotation data
     $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
     $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
     $fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
     $popt['da'] = $fontstyle;
     // build appearance stream
     $popt['ap'] = array();
     $popt['ap']['n'] = '/Tx BMC q ' . $fontstyle . ' ';
     $tmpid = $this->startTemplate($w, $h, false);
     $bw = 2 / $this->k;
     // border width
     $border = array('L' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(231)), 'R' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(51)), 'T' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(231)), 'B' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(51)));
     $this->SetFillColor(204);
     $this->Cell($w, $h, $caption, $border, 0, 'C', true, '', 1, false, 'T', 'M');
     $this->endTemplate();
     --$this->n;
     $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
     unset($this->xobjects[$tmpid]);
     $popt['ap']['n'] .= 'Q EMC';
     // set additional default options
     if (!isset($popt['mk'])) {
         $popt['mk'] = array();
     }
     $ann_obj_id = $this->n + 1;
     if (!empty($action) and !is_array($action)) {
         $ann_obj_id = $this->n + 2;
     }
     $popt['mk']['ca'] = $this->_textstring($caption, $ann_obj_id);
     $popt['mk']['rc'] = $this->_textstring($caption, $ann_obj_id);
     $popt['mk']['ac'] = $this->_textstring($caption, $ann_obj_id);
     // merge options
     $opt = array_merge($popt, $opt);
     // set remaining annotation data
     $opt['Subtype'] = 'Widget';
     $opt['ft'] = 'Btn';
     $opt['t'] = $caption;
     $opt['v'] = $name;
     if (!empty($action)) {
         if (is_array($action)) {
             // form action options as on section 12.7.5 of PDF32000_2008.
             $opt['aa'] = '/D <<';
             $bmode = array('SubmitForm', 'ResetForm', 'ImportData');
             foreach ($action as $key => $val) {
                 if ($key == 'S' and in_array($val, $bmode)) {
                     $opt['aa'] .= ' /S /' . $val;
                 } elseif ($key == 'F' and !empty($val)) {
                     $opt['aa'] .= ' /F ' . $this->_datastring($val, $ann_obj_id);
                 } elseif ($key == 'Fields' and is_array($val) and !empty($val)) {
                     $opt['aa'] .= ' /Fields [';
                     foreach ($val as $field) {
                         $opt['aa'] .= ' ' . $this->_textstring($field, $ann_obj_id);
                     }
                     $opt['aa'] .= ']';
                 } elseif ($key == 'Flags') {
                     $ff = 0;
                     if (is_array($val)) {
                         foreach ($val as $flag) {
                             switch ($flag) {
                                 case 'Include/Exclude':
                                     $ff += 1 << 0;
                                     break;
                                 case 'IncludeNoValueFields':
                                     $ff += 1 << 1;
                                     break;
                                 case 'ExportFormat':
                                     $ff += 1 << 2;
                                     break;
                                 case 'GetMethod':
                                     $ff += 1 << 3;
                                     break;
                                 case 'SubmitCoordinates':
                                     $ff += 1 << 4;
                                     break;
                                 case 'XFDF':
                                     $ff += 1 << 5;
                                     break;
                                 case 'IncludeAppendSaves':
                                     $ff += 1 << 6;
                                     break;
                                 case 'IncludeAnnotations':
                                     $ff += 1 << 7;
                                     break;
                                 case 'SubmitPDF':
                                     $ff += 1 << 8;
                                     break;
                                 case 'CanonicalFormat':
                                     $ff += 1 << 9;
                                     break;
                                 case 'ExclNonUserAnnots':
                                     $ff += 1 << 10;
                                     break;
                                 case 'ExclFKey':
                                     $ff += 1 << 11;
                                     break;
                                 case 'EmbedForm':
                                     $ff += 1 << 13;
                                     break;
                             }
                         }
                     } else {
                         $ff = intval($val);
                     }
                     $opt['aa'] .= ' /Flags ' . $ff;
                 }
             }
             $opt['aa'] .= ' >>';
         } else {
             // Javascript action or raw action command
             $js_obj_id = $this->addJavascriptObject($action);
             $opt['aa'] = '/D ' . $js_obj_id . ' 0 R';
         }
     }
     $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
     if ($this->rtl) {
         $this->x -= $w;
     } else {
         $this->x += $w;
     }
 }