Class: HexaPDF::Type::AcroForm::VariableTextField

Inherits:
Field show all
Defined in:
lib/hexapdf/type/acro_form/variable_text_field.rb

Overview

An AcroForm variable text field defines how text that it is not known at generation time should be rendered. For example, AcroForm text fields (normally) don’t have an initial value; the value is entered by the user and needs to be rendered correctly by the PDF reader.

See: PDF1.7 s12.7.3.3

Direct Known Subclasses

ChoiceField, TextField

Constant Summary collapse

INHERITABLE_FIELDS =

All inheritable dictionary fields for text fields.

(superclass::INHERITABLE_FIELDS + [:DA, :Q]).freeze
UNSET_ARG =

:nodoc:

::Object.new

Constants included from DictionaryFields

DictionaryFields::Boolean, DictionaryFields::PDFByteString, DictionaryFields::PDFDate

Instance Attribute Summary

Attributes inherited from Object

#data, #document, #must_be_indirect

Instance Method Summary collapse

Methods inherited from Field

#[], #alternate_field_name, #alternate_field_name=, #concrete_field_type, #create_widget, #delete_widget, #each_widget, #embedded_widget?, #field_name, #field_type, #flags, #full_field_name, inherited_value, #must_be_indirect?, #terminal_field?

Methods included from Utils::BitField

#bit_field

Methods inherited from Dictionary

#[], #[]=, define_field, define_type, #delete, #each, each_field, #empty?, field, #key?, #to_h, type, #type

Methods inherited from Object

#<=>, #==, #cache, #cached?, #clear_cache, deep_copy, #deep_copy, #document?, #eql?, #gen, #gen=, #hash, #indirect?, #initialize, #inspect, #must_be_indirect?, #null?, #oid, #oid=, #type, #validate, #value, #value=

Constructor Details

This class inherits a constructor from HexaPDF::Object

Instance Method Details

#parse_default_appearance_stringObject

Parses the default appearance string and returns an array containing [font_name, font_size].

The default appearance string is taken from the field or, if not set, the default appearance string of the form.

Raises:



107
108
109
110
111
112
113
114
115
116
# File 'lib/hexapdf/type/acro_form/variable_text_field.rb', line 107

def parse_default_appearance_string
  da = self[:DA] || (document.acro_form && document.acro_form[:DA])
  raise HexaPDF::Error, "No default appearance string set" unless da

  font_params = nil
  HexaPDF::Content::Parser.parse(da) do |obj, params|
    font_params = params.dup if obj == :Tf
  end
  font_params
end

#set_default_appearance_string(font: 'Helvetica', font_size: 0) ⇒ Object

Sets the default appearance string using the provided values.

The default argument values are a sane default. If font_size is set to 0, the font size is calculated using the height/width of the field.



96
97
98
99
100
# File 'lib/hexapdf/type/acro_form/variable_text_field.rb', line 96

def set_default_appearance_string(font: 'Helvetica', font_size: 0)
  name = document.acro_form(create: true).default_resources.
    add_font(document.fonts.add(font).pdf_object)
  self[:DA] = "0 g /#{name} #{font_size} Tf"
end

#text_alignment(alignment = UNSET_ARG) ⇒ Object

:call-seq:

field.text_alignment                -> alignment
field.text_alignment(alignment)     -> field

Sets or returns the text alignment that should be used when displaying text.

With no argument, the current text alignment is returned. When a value is provided, the text alignment is set accordingly.

The alignment value is one of :left, :center or :right.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/hexapdf/type/acro_form/variable_text_field.rb', line 74

def text_alignment(alignment = UNSET_ARG)
  if alignment == UNSET_ARG
    case self[:Q]
    when 0 then :left
    when 1 then :center
    when 2 then :right
    end
  else
    self[:Q] = case alignment
               when :left then 0
               when :center then 1
               when :right then 2
               else
                 raise ArgumentError, "Invalid variable text field alignment #{alignment}"
               end
  end
end