How can I detect with PDFNet whether a PDF object is free or not, given the object number?

Q: How can I detect with PDFNet whether an object is free or not, given the object number?

So far, I’ve seen three behaviors for free objects:

  • SDFDoc.GetObj() throws an exception

  • SDFDoc.GetObj() succeeds and GetObjNum() for the object returns 0

Some PDF documents (erroneously) use “0000000000 00000 n” in the xref table for free objects (and they created by an Adobe product!). We would like to detect this type of errors and to add some special handling.

A:

To use a random obj from the file you need to make sure that obj is not NULL and that it is not free (i.e. obj && !obj.IsFree()).

For a concrete example, please take a look at a second code snippet in ImageExtract sample project:

SDFDoc& cos_doc=doc.GetSDFDoc();

int num_objs = cos_doc.XRefSize();

for(int i=1; i<num_objs; ++i) {

Obj obj = cos_doc.GetObj(i);

if(obj && !obj.IsFree() && … ) {

}

}