diff --git a/poppler/Catalog.cc b/poppler/Catalog.cc index 039f63e..d92800c 100644 --- a/poppler/Catalog.cc +++ b/poppler/Catalog.cc @@ -807,7 +807,6 @@ int Catalog::getNumPages() return 0; } catDict.dictLookup("Pages", &pagesDict); - catDict.free(); // This should really be isDict("Pages"), but I've seen at least one // PDF file where the /Type entry is missing. @@ -815,19 +814,47 @@ int Catalog::getNumPages() error(errSyntaxError, -1, "Top-level pages object is wrong type ({0:s})", pagesDict.getTypeName()); pagesDict.free(); + catDict.free(); return 0; } pagesDict.dictLookup("Count", &obj); // some PDF files actually use real numbers here ("/Count 9.0") if (!obj.isNum()) { - error(errSyntaxError, -1, "Page count in top-level pages object is wrong type ({0:s})", - obj.getTypeName()); - numPages = 0; + if (pagesDict.dictIs("Page")) { + Object pageRootRef; + catDict.dictLookupNF("Pages", &pageRootRef); + + error(errSyntaxError, -1, "Pages top-level is a single Page. The document is mal-formet, trying to recover..."); + + Dict *pageDict = pagesDict.getDict(); + const Ref pageRef = pageRootRef.getRef(); + Page *p = new Page(doc, 1, pageDict, pageRef, new PageAttrs(NULL, pageDict), form); + if (p->isOk()) { + pages = (Page **)gmallocn(1, sizeof(Page *)); + pageRefs = (Ref *)gmallocn(1, sizeof(Ref)); + + pages[0] = p; + pageRefs[0].num = pageRef.num; + pageRefs[0].gen = pageRef.gen; + + numPages = 1; + lastCachedPage = 1; + pagesSize = 1; + } else { + delete p; + numPages = 0; + } + } else { + error(errSyntaxError, -1, "Page count in top-level pages object is wrong type ({0:s})", + obj.getTypeName()); + numPages = 0; + } } else { numPages = (int)obj.getNum(); } + catDict.free(); obj.free(); pagesDict.free(); }