Method: RBPDF#Button

Defined in:
lib/rbpdf.rb

#Button(name, w, h, caption, action, prop = {}, opt = {}, x = '', y = '', js = false) ⇒ Object Also known as: button

Creates a button field

@param string :name

field name

@param int :w

width

@param int :h

height

@param string :caption

caption.

@param mixed :action

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 array :prop

javascript field properties. Possible values are described on official Javascript for Acrobat API reference.

@param array :opt

annotation parameters. Possible values are described on official PDF32000_2008 reference.

@param float :x

Abscissa of the upper-left corner of the rectangle

@param float :y

Ordinate of the upper-left corner of the rectangle

@param boolean :js

if true put the field using JavaScript (requires Acrobat Writer to be rendered).

@access public
@author Nicola Asuni
@since 4.8.000 (2009-09-07)


11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
# File 'lib/rbpdf.rb', line 11240

def Button(name, w, h, caption, action, prop = {}, opt = {}, x = '', y = '', js = false)
  x = @x if x == ''
  y = @y if y == ''

  if js
    addfield('button', name, x, y, w, h, prop)
    @javascript << "f#{name}.buttonSetCaption('#{caption}');\n"
    @javascript << "f#{name}.setAction('MouseUp','#{action}');\n"
    @javascript << "f#{name}.highlight='push';\n"
    @javascript << "f#{name}.print=false;\n"
    return
  end
  # get default style
  prop = getFormDefaultProp.merge prop
  prop['Pushbutton'] = 'true'
  prop['highlight'] = 'push'
  prop['display'] = 'display.noPrint'
  # get annotation data
  popt = getAnnotOptFromJSProp(prop)
  # set additional default options
  popt['mk'] ||= {}
  popt['mk']['ca'] = textstring(caption)
  popt['mk']['rc'] = textstring(caption)
  popt['mk']['ac'] = textstring(caption)
  font = @font_family
  fontkey = @fontkeys.index font
  unless @annotation_fonts.include? fontkey
    @annotation_fonts[font] = fontkey
  end
  fontstyle = sprintf('/F%d %.2f Tf %s', fontkey + 1, @font_size_pt, @text_color)
  popt['da'] = fontstyle
  popt['ap'] = {}
  # set Appearances
  popt['ap']['n'] = +"/Tx BMC q #{fontstyle} 0.800 g\n"

  gvars = getGraphicVars()
  @h = h
  @w = w
  @c_margin *= 1.6

  @tmp_buffer = +''
  SetLineStyle({'width' => 1.0, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => [231]})
  SetFillColor(204)
  multi_cell(w, h, caption, 1, 'C', 1, 0, 0, 0, true)
  popt['ap']['n'] << @tmp_buffer
  popt['ap']['n'] << 'Q EMC'
  @tmp_buffer = nil

  # restore previous values
  setGraphicVars(gvars, true)

  # merge options
  opt = popt.merge opt
  # set remaining annotation data
  opt['Subtype'] = 'Widget'
  opt['ft'] = 'Btn'
  opt['t'] = caption
  opt['v'] = name
  unless action.empty?
    if action.is_a?(Hash)
      # form action options as on section 12.7.5 of PDF32000_2008.
      opt['aa'] = +'/D <<'
      bmode = ['SubmitForm', 'ResetForm', 'ImportData']
      action.each {|key, val|
        if (key == 'S') && bmode.include?(val)
          opt['aa'] << " /S /#{val}"
        elsif (key == 'F') && !val.empty?
          opt['aa'] << " /F #{datastring(val)}"
        elsif (key == 'Fields') && val.is_a?(Array) && !val.empty?
          opt['aa'] << ' /Fields ['
          val.each {|field|
            opt['aa'] << " #{textstring(field)}"
          }
          opt['aa'] << ']'
        elsif key == 'Flags'
          ff = 0
          if val.is_a?(Array)
            val.each {|flag|
              case flag
              when 'Include/Exclude';      ff |= 1 << 0
              when 'IncludeNoValueFields'; ff |= 1 << 1
              when 'ExportFormat';         ff |= 1 << 2
              when 'GetMethod';            ff |= 1 << 3
              when 'SubmitCoordinates';    ff |= 1 << 4
              when 'XFDF';                 ff |= 1 << 5
              when 'IncludeAppendSaves';   ff |= 1 << 6
              when 'IncludeAnnotations';   ff |= 1 << 7
              when 'SubmitPDF';            ff |= 1 << 8
              when 'CanonicalFormat';      ff |= 1 << 9
              when 'ExclNonUserAnnots';    ff |= 1 << 10
              when 'ExclFKey';             ff |= 1 << 11
              when 'EmbedForm';            ff |= 1 << 13
              end
            }
          else
            ff = val.to_i
          end
          opt['aa'] << " /Flags #{ff}"
        end
      }
      opt['aa'] << ' >>'
    else
      # Javascript action or raw action command
      js_obj_id = addJavascriptObject(action)
      opt['aa'] = "/D #{js_obj_id} 0 R"
    end
  end
  Annotation(x, y, w, h, name, opt, 0)
  if @rtl
    @x -= w
  else
    @x += w
  end
end