Class: Pdfrb::Content::HiddenTextDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/content/hidden_text_detector.rb

Overview

Detects hidden / transparent text in content streams. Hidden text is text rendered with Tr=3 (invisible) or with alpha=0 (fully transparent). PDF/A and PDF/UA require such text to be tagged as /Artifact or wrapped in a structure element.

Inspired by mn2pdf's PDFHiddenTextStripper. Walks content streams via the Content::Parser, tracking graphics state changes.

Defined Under Namespace

Classes: Collector, HiddenItem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ HiddenTextDetector

Returns a new instance of HiddenTextDetector.



23
24
25
# File 'lib/pdfrb/content/hidden_text_detector.rb', line 23

def initialize(document)
  @document = document
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



21
22
23
# File 'lib/pdfrb/content/hidden_text_detector.rb', line 21

def document
  @document
end

Instance Method Details

#detectObject

Walk all content streams. Returns HiddenItem objects for each text-showing operation. Items with a nil reason are visible.



29
30
31
32
33
34
35
36
37
38
# File 'lib/pdfrb/content/hidden_text_detector.rb', line 29

def detect
  items = []
  @document.each_indirect_object do |obj|
    next unless content_stream?(obj)

    page = find_page(obj)
    detect_in_stream(obj, page) { |item| items << item }
  end
  items
end

#hidden_onlyObject

Returns only the hidden text items (rendering_mode_3 or alpha=0).



41
42
43
# File 'lib/pdfrb/content/hidden_text_detector.rb', line 41

def hidden_only
  detect.select(&:hidden?)
end