Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/pdf/reader/page_text_receiver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def page=(page)
@page = page
@content = []
@characters = []
@actual_text = nil
@actual_text_consumed = false
end

def runs(opts = {})
Expand Down Expand Up @@ -121,6 +123,21 @@ def set_spacing_next_line_show_text(aw, ac, string) # "
move_to_next_line_and_show_text(string)
end

#####################################################
# Marked Content
#####################################################
def begin_marked_content_with_pl(tag, properties)
if properties.is_a?(Hash) && properties[:ActualText]
@actual_text = properties[:ActualText]
@actual_text_consumed = false
end
end

def end_marked_content
@actual_text = nil
@actual_text_consumed = false
end

#####################################################
# XObjects
#####################################################
Expand Down Expand Up @@ -149,6 +166,18 @@ def internal_show_text(string)

utf8_chars = @state.current_font.to_utf8(glyph_code)

# Use ActualText from marked content if available (PDF 32000-1 §14.9.4).
# ActualText replaces all text within a BDC/EMC span.
if @actual_text
if !@actual_text_consumed
text = @actual_text
utf8_chars = PDF::Reader::EncodingUtils.string_to_utf8(text)
@actual_text_consumed = true
else
utf8_chars = ""
end
Comment on lines +176 to +178
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you describe the purpose of the else block? What happens if we remove it and let the control flow continue as normal, just with the value of utf8_chars no replaced by the marked content ActualText?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The else block handles subsequent glyphs within the same BDC/EMC span after ActualText has already been emitted. Since ActualText replaces all glyph text in the span, these glyphs should contribute only their displacement (positioning), not text.

You're right that removing the else block and letting control flow continue works — setting utf8_chars to an empty string achieves the same result more simply. Updated.

end

# apply to glyph displacment for the current glyph so the next
# glyph will appear in the correct position
glyph_width = @state.current_font.glyph_width_in_text_space(glyph_code)
Expand Down
Binary file added spec/data/actual_text_marked_content.pdf
Binary file not shown.
11 changes: 11 additions & 0 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,17 @@
end
end

context "PDF with ActualText in marked content" do
let(:filename) { pdf_spec_file("actual_text_marked_content") }

it "extracts text correctly using ActualText" do
PDF::Reader.open(filename) do |reader|
page = reader.page(1)
expect(page.text).to include("21.09.2023")
end
end
end

context "PDF that uses a standatd font and a ligature" do
let(:filename) { pdf_spec_file("standard_font_with_a_difference") }

Expand Down
3 changes: 3 additions & 0 deletions spec/integrity.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ data/encrypted_version5_revision6_256bit_aes_user_pass_apples_unenc_metadata.pdf
data/extended_eof.pdf:
:bytes: 61721
:md5: 02bd4cfbc79b4a295754fda2705b6181
data/actual_text_marked_content.pdf:
:bytes: 6651
:md5: 3fa1e5422adb93486eaad8a75bd3323c
data/font_sizes.pdf:
:bytes: 1062
:md5: c8d4bd87fa2d9b16c9c41501f37905c3
Expand Down