Class: Uniword::Watermark::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/watermark/manager.rb

Overview

Manages watermarks in a document.

Watermarks are implemented as shape elements in the default header. This manager adds/removes watermark shapes from the document’s header section.

Examples:

Add a watermark

manager = Manager.new(doc)
manager.add("CONFIDENTIAL", color: "#FF0000", font_size: 72)

Remove watermarks

manager.remove

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Manager

Initialize with a document.

Parameters:



23
24
25
# File 'lib/uniword/watermark/manager.rb', line 23

def initialize(document)
  @document = document
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



18
19
20
# File 'lib/uniword/watermark/manager.rb', line 18

def document
  @document
end

Instance Method Details

#add(text, color: "#808080", font_size: 72, font: "Segoe UI", opacity: ".5") ⇒ void

This method returns an undefined value.

Add a text watermark to the document.

Creates a shape element in the default header with diagonal text at 50% opacity.

Parameters:

  • text (String)

    Watermark text

  • color (String) (defaults to: "#808080")

    Hex color (e.g., “#808080”)

  • font_size (Integer) (defaults to: 72)

    Font size in points

  • font (String) (defaults to: "Segoe UI")

    Font family name

  • opacity (String) (defaults to: ".5")

    Opacity value (e.g., “.5”)



38
39
40
41
42
43
# File 'lib/uniword/watermark/manager.rb', line 38

def add(text, color: "#808080", font_size: 72,
        font: "Segoe UI", opacity: ".5")
  header = find_or_create_default_header
  mark = build_watermark(text, color, font_size, font, opacity)
  header.paragraphs << mark
end

#listArray<String>

List all watermarks in the document.

Returns:

  • (Array<String>)

    Watermark texts



72
73
74
75
76
77
78
79
80
# File 'lib/uniword/watermark/manager.rb', line 72

def list
  marks = []
  (document.headers || []).each do |header|
    header.paragraphs.each do |p|
      marks << extract_watermark_text(p) if watermark?(p)
    end
  end
  marks
end

#present?Boolean

Check if the document has any watermarks.

Returns:

  • (Boolean)


63
64
65
66
67
# File 'lib/uniword/watermark/manager.rb', line 63

def present?
  (document.headers || []).any? do |header|
    header.paragraphs.any? { |p| watermark?(p) }
  end
end

#removeInteger

Remove all watermarks from the document.

Returns:

  • (Integer)

    Number of watermarks removed



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/uniword/watermark/manager.rb', line 48

def remove
  count = 0
  (document.headers || []).each do |header|
    before = header.paragraphs.size
    header.paragraphs.reject! do |p|
      watermark?(p)
    end
    count += before - header.paragraphs.size
  end
  count
end