From b41a2f4d5a7e6c1acdac49c92869a12be18dd45c Mon Sep 17 00:00:00 2001 From: Tim van der Meij Date: Sat, 22 Aug 2020 22:47:15 +0200 Subject: [PATCH] Move the collection logic from the document to the catalog The `Collection` entry is part of the catalog, not of the document, so its logic should be placed there instead. The document should look in the catalog to fetch it, and not have knowledge of `catDict`, which is a member internal to the catalog. Moreover, remove the collection member from the document instance. It's only used internally and was also never intended to be public. For users it's exposed by the `getMetadata` API endpoint as `IsCollectionPresent`. Moving this out of the `parse` function makes sure that the getter is only executed if the document information is actually requested (potentially making initial parsing a tiny bit faster). --- src/core/document.js | 15 +-------------- src/core/obj.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/core/document.js b/src/core/document.js index 3076490dc..4513ae71d 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -600,19 +600,6 @@ class PDFDocument { info("Cannot fetch AcroForm entry; assuming no AcroForms are present"); this.acroForm = null; } - - // Check if a Collection dictionary is present in the document. - try { - const collection = this.catalog.catDict.get("Collection"); - if (isDict(collection) && collection.size > 0) { - this.collection = collection; - } - } catch (ex) { - if (ex instanceof MissingDataException) { - throw ex; - } - info("Cannot fetch Collection dictionary."); - } } get linearization() { @@ -745,7 +732,7 @@ class PDFDocument { IsLinearized: !!this.linearization, IsAcroFormPresent: !!this.acroForm, IsXFAPresent: !!this.xfa, - IsCollectionPresent: !!this.collection, + IsCollectionPresent: !!this.catalog.collection, }; let infoDict; diff --git a/src/core/obj.js b/src/core/obj.js index 47dcd902b..eaf885a7f 100644 --- a/src/core/obj.js +++ b/src/core/obj.js @@ -84,6 +84,22 @@ class Catalog { return shadow(this, "version", version.name); } + get collection() { + let collection = null; + try { + const obj = this.catDict.get("Collection"); + if (isDict(obj) && obj.size > 0) { + collection = obj; + } + } catch (ex) { + if (ex instanceof MissingDataException) { + throw ex; + } + info("Cannot fetch Collection entry; assuming no collection is present."); + } + return shadow(this, "collection", collection); + } + get metadata() { const streamRef = this.catDict.getRaw("Metadata"); if (!isRef(streamRef)) {