Module: HexaPDF::DocumentUtils::Images
- Included in:
- HexaPDF::DocumentUtils
- Defined in:
- lib/hexapdf/document_utils.rb
Overview
This module provides methods for managing the images embedded in a PDF file; images themselves are represented by the HexaPDF::Type::Image class.
Since an image can be used as a mask for another image, not all image objects found in a PDF are really used as images. Such cases are all handled by this class automatically.
Instance Method Summary collapse
-
#add_image(file_or_io) ⇒ Object
:call-seq: images.add_image(file) -> image images.add_image(io) -> image.
-
#each_image(&block) ⇒ Object
:call-seq: images.each_image {|image| block } -> images images.each_image -> Enumerator.
Instance Method Details
#add_image(file_or_io) ⇒ Object
:call-seq:
images.add_image(file) -> image
images.add_image(io) -> image
Adds the image from the given file or IO to the PDF and returns the image object.
If the image has been added to the PDF before (i.e. if there is an image object with the same path name), the already existing image object is returned.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/hexapdf/document_utils.rb', line 61 def add_image(file_or_io) name = if file_or_io.kind_of?(String) file_or_io elsif file_or_io.respond_to?(:to_path) file_or_io.to_path end if name name = File.absolute_path(name) image = each_image.find {|im| im.source_path == name} end unless image image = image_loader_for(file_or_io).load(@document, file_or_io) image.source_path = name end image end |
#each_image(&block) ⇒ Object
:call-seq:
images.each_image {|image| block } -> images
images.each_image -> Enumerator
Iterates over all images in the PDF.
Note that only real images are yielded which means, for example, that images used as soft mask are not.
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/hexapdf/document_utils.rb', line 86 def each_image(&block) images = @document.each(current: false).select do |obj| obj[:Subtype] == :Image && !obj[:ImageMask] end masks = images.each_with_object([]) do |image, temp| temp << image[:Mask] if image[:Mask].kind_of?(Stream) temp << image[:SMask] if image[:SMask].kind_of?(Stream) end (images - masks).each(&block) end |