How do I access an Element that no longer exist?

Q: I’ve downloaded the new 4.8.0.0 library for Java and when I call

ColorSpace colorSpace = element.getImageColorSpace();
if (colorSpace.getType() == ColorSpace.e_device_rgb) {
}

the I get an exception. At this point of my code I’ve already
traversed all elements of the page and the element is supposed to
point to the last image element.


A: Based on the provided sample code there is nothing wrong with the
provided code snippet and it runs fine (e.g. in ElementReaderAdv
sample project - http://www.pdftron.com/pdfnet/samplecode.html#ElementReaderAdv).

Please keep in mind that the current Element only exists till the next
call of ElementReader.Next(). So every call to ElementReader.Next()
destroys the current Element (this is done for efficiency reasons and
to keep memeory consumption low). Therefore, an Element becomes
invalid after subsequent ElementReader.Next() operation. You are free
to copy any information into your own data structures and use it later
on.

Another important bit is that color space is not required on PDF image
masks. So when processing images you can use the following line to
detect image masks:

if (element.IsImageMask() == false) {
ColorSpace colorSpace = element.getImageColorSpace();
if (colorSpace.getType() == ColorSpace.e_device_rgb) {
}
}
// else color space is ‘Gray’