class HexaPDF::Document::Images
Such cases are all handled by this class automatically.
as a mask for another image, not all image objects found in a PDF are really used as images.
Images themselves are represented by the HexaPDF::Type::Image class.Since an image can be used
through the HexaPDF::Document#images method.
This class provides methods for managing the images embedded in a PDF file. It is available
def add(file_or_io)
If the image has been added to the PDF before (i.e. if there is an image object with the
Adds the image from the given file or IO to the PDF document and returns the image object.
images.add(io) -> image
images.add(file) -> image
:call-seq:
def add(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 = 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
def each(&block)
Note that only real images are yielded which means, for example, that images used as soft
Iterates over all images in the PDF document.
images.each -> Enumerator
images.each {|image| block } -> images
:call-seq:
def each(&block) images = @document.each.select do |obj| next unless obj.kind_of?(HexaPDF::Dictionary) 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
def image_loader_for(file_or_io)
Returns the image loader (see HexaPDF::ImageLoader) for the given file or IO stream or
def image_loader_for(file_or_io) @document.config['image_loader'].each_index do |index| loader = @document.config.constantize('image_loader', index) do raise HexaPDF::Error, "Couldn't retrieve image loader from configuration" end return loader if loader.handles?(file_or_io) end raise HexaPDF::Error, "Couldn't find suitable image loader" end
def initialize(document)
def initialize(document) @document = document end