Class: HexaPDF::Document::Annotations

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hexapdf/document/annotations.rb

Overview

This class provides methods for creating and managing the annotations of a PDF file.

An annotation is an object that can be added to a certain location on a page, provides a visual appearance and allows for interaction with the user via keyboard and mouse.

Usage

To create an annotation either call the general #create method or a specific creation method for an annotation type. After the annotation has been created customize it using the convenience methods on the annotation object. The last step should be the call to regenerate_appearance so that the appearance is generated.

See: PDF2.0 s12.5

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Annotations

Creates a new Annotations object for the given PDF document.



61
62
63
# File 'lib/hexapdf/document/annotations.rb', line 61

def initialize(document)
  @document = document
end

Instance Method Details

#create(type, page, **options) ⇒ Object

:call-seq:

annotations.create(type, page, **options)      -> annotation

Creates a new annotation object with the given type and page by calling the respective create_type method.

The options are passed on the specific annotation creation method.



72
73
74
75
76
77
78
# File 'lib/hexapdf/document/annotations.rb', line 72

def create(type, page, **options)
  method_name = "create_#{type}"
  unless respond_to?(method_name)
    raise ArgumentError, "Invalid type specified"
  end
  send("create_#{type}", page, **options)
end

#create_line(page, start_point:, end_point:) ⇒ Object

:call-seq:

annotations.create_line(page, start_point:, end_point:)  -> annotation

Creates a line annotation from start_point to end_point on the given page and returns it.

The line uses a black color and a width of 1pt. It can be further styled using the convenience methods on the returned annotation object.

Example:

doc.annotations.create_line(doc.pages[0], start_point: [100, 100], end_point: [130, 180]).
  border_style(color: "blue", width: 2).
  leader_line_length(10).
  regenerate_appearance

See: Type::Annotations::Line



97
98
99
100
101
# File 'lib/hexapdf/document/annotations.rb', line 97

def create_line(page, start_point:, end_point:)
  create_and_add_to_page(:Line, page).
    line(*start_point, *end_point).
    border_style(color: 0, width: 1)
end