Repository: Hopding/pdf-lib
Pull request #414 (2020-04-18 16:17:01)

Add PDFObject accessors
Addresses https://github.com/Hopding/pdf-lib/issues/238


Patch:
diff --git a/README.md b/README.md
index 815b225..7f10bc8 100644
--- a/README.md
+++ b/README.md
@@ -814,7 +814,7 @@ We welcome contributions from the open source community! If you are interested i
 
 - [Möbius Printing helper](https://shreevatsa.net/mobius-print/) - a tool created by @shreevatsa
 - [Extract PDF pages](https://shreevatsa.net/pdf-pages/) - a tool created by @shreevatsa
-- [COVID-19 travel certificate generator](https://github.com/LAB-MI/deplacement-covid-19) - a tool that creates travel certificates for French citizens under quarantine due to COVID-19
+- [Travel certificate generator](https://github.com/LAB-MI/deplacement-covid-19) - a tool that creates travel certificates for French citizens under quarantine due to COVID-19
 - [How to use pdf-lib in AWS Lambdas](https://medium.com/swlh/create-pdf-using-pdf-lib-on-serverless-aws-lambda-e9506246dc88) - a tutorial written by Crespo Wang
 - [Working With PDFs in Node.js Using pdf-lib](http://thecodebarbarian.com/working-with-pdfs-in-node-js.html) - a tutorial by Valeri Karpov
 
diff --git a/src/api/PDFPage.ts b/src/api/PDFPage.ts
index 156c3c5..3d9bcc5 100644
--- a/src/api/PDFPage.ts
+++ b/src/api/PDFPage.ts
@@ -142,7 +142,7 @@ export default class PDFPage {
    */
   getRotation(): Rotation {
     const Rotate = this.node.Rotate();
-    return degrees(Rotate ? Rotate.value() : 0);
+    return degrees(Rotate ? Rotate.asNumber() : 0);
   }
 
   /**
diff --git a/src/api/objects.ts b/src/api/objects.ts
index dc41b5e..a6d4bbb 100644
--- a/src/api/objects.ts
+++ b/src/api/objects.ts
@@ -7,4 +7,4 @@ export const asPDFNumber = (num: number | PDFNumber) =>
   num instanceof PDFNumber ? num : PDFNumber.of(num);
 
 export const asNumber = (num: number | PDFNumber) =>
-  num instanceof PDFNumber ? num.value() : num;
+  num instanceof PDFNumber ? num.asNumber() : num;
diff --git a/src/core/embedders/PDFPageEmbedder.ts b/src/core/embedders/PDFPageEmbedder.ts
index d1d12ef..a5efac4 100644
--- a/src/core/embedders/PDFPageEmbedder.ts
+++ b/src/core/embedders/PDFPageEmbedder.ts
@@ -41,12 +41,12 @@ const fullPageBoundingBox = (page: PDFPageLeaf) => {
   const mediaBox = page.MediaBox();
 
   const width =
-    mediaBox.lookup(2, PDFNumber).value() -
-    mediaBox.lookup(0, PDFNumber).value();
+    mediaBox.lookup(2, PDFNumber).asNumber() -
+    mediaBox.lookup(0, PDFNumber).asNumber();
 
   const height =
-    mediaBox.lookup(3, PDFNumber).value() -
-    mediaBox.lookup(1, PDFNumber).value();
+    mediaBox.lookup(3, PDFNumber).asNumber() -
+    mediaBox.lookup(1, PDFNumber).asNumber();
 
   return { left: 0, bottom: 0, right: width, top: height };
 };
diff --git a/src/core/objects/PDFArray.ts b/src/core/objects/PDFArray.ts
index 9129e57..f4a871e 100644
--- a/src/core/objects/PDFArray.ts
+++ b/src/core/objects/PDFArray.ts
@@ -85,10 +85,10 @@ class PDFArray extends PDFObject {
   asRectangle(): { x: number; y: number; width: number; height: number } {
     if (this.size() !== 4) throw new PDFArrayIsNotRectangleError(this.size());
 
-    const lowerLeftX = this.lookup(0, PDFNumber).value();
-    const lowerLeftY = this.lookup(1, PDFNumber).value();
-    const upperRightX = this.lookup(2, PDFNumber).value();
-    const upperRightY = this.lookup(3, PDFNumber).value();
+    const lowerLeftX = this.lookup(0, PDFNumber).asNumber();
+    const lowerLeftY = this.lookup(1, PDFNumber).asNumber();
+    const upperRightX = this.lookup(2, PDFNumber).asNumber();
+    const upperRightY = this.lookup(3, PDFNumber).asNumber();
 
     const x = lowerLeftX;
     const y = lowerLeftY;
@@ -98,6 +98,10 @@ class PDFArray extends PDFObject {
     return { x, y, width, height };
   }
 
+  asArray(): PDFObject[] {
+    return this.array.slice();
+  }
+
   clone(context?: PDFContext): PDFArray {
     const clone = PDFArray.withContext(context || this.context);
     for (let idx = 0, len = this.size(); idx < len; idx++) {
diff --git a/src/core/objects/PDFBool.ts b/src/core/objects/PDFBool.ts
index 87b7521..fc8264a 100644
--- a/src/core/objects/PDFBool.ts
+++ b/src/core/objects/PDFBool.ts
@@ -16,6 +16,10 @@ class PDFBool extends PDFObject {
     this.value = value;
   }
 
+  asBoolean(): boolean {
+    return this.value;
+  }
+
   clone(): PDFBool {
     return this;
   }
diff --git a/src/core/objects/PDFDict.ts b/src/core/objects/PDFDict.ts
index 48519a0..a71a5cb 100644
--- a/src/core/objects/PDFDict.ts
+++ b/src/core/objects/PDFDict.ts
@@ -23,7 +23,7 @@ class PDFDict extends PDFObject {
 
   private readonly dict: DictMap;
 
-  protected constructor(map: Map<PDFName, PDFObject>, context: PDFContext) {
+  protected constructor(map: DictMap, context: PDFContext) {
     super();
     this.dict = map;
     this.context = context;
@@ -83,6 +83,10 @@ class PDFDict extends PDFObject {
     return this.dict.delete(key);
   }
 
+  asMap(): Map<PDFName, PDFObject> {
+    return new Map(this.dict);
+  }
+
   clone(context?: PDFContext): PDFDict {
     const clone = PDFDict.withContext(context || this.context);
     const entries = this.entries();
diff --git a/src/core/objects/PDFHexString.ts b/src/core/objects/PDFHexString.ts
index 9b60461..b4a63ab 100644
--- a/src/core/objects/PDFHexString.ts
+++ b/src/core/objects/PDFHexString.ts
@@ -27,6 +27,10 @@ class PDFHexString extends PDFObject {
     this.value = value;
   }
 
+  asString(): string {
+    return this.value;
+  }
+
   clone(): PDFHexString {
     return PDFHexString.of(this.value);
   }
diff --git a/src/core/objects/PDFName.ts b/src/core/objects/PDFName.ts
index 7da3523..3600886 100644
--- a/src/core/objects/PDFName.ts
+++ b/src/core/objects/PDFName.ts
@@ -68,6 +68,11 @@ class PDFName extends PDFObject {
     this.encodedName = encodedName;
   }
 
+  asString(): string {
+    return this.encodedName;
+  }
+
+  /** @deprecated in favor of [[PDFName.asString]] */
   value(): string {
     return this.encodedName;
   }
diff --git a/src/core/objects/PDFNull.ts b/src/core/objects/PDFNull.ts
index 7b11748..36f3f29 100644
--- a/src/core/objects/PDFNull.ts
+++ b/src/core/objects/PDFNull.ts
@@ -2,6 +2,10 @@ import PDFObject from 'src/core/objects/PDFObject';
 import CharCodes from 'src/core/syntax/CharCodes';
 
 class PDFNull extends PDFObject {
+  asNull(): null {
+    return null;
+  }
+
   clone(): PDFNull {
     return this;
   }
diff --git a/src/core/objects/PDFNumber.ts b/src/core/objects/PDFNumber.ts
index 02f68bb..33db2e6 100644
--- a/src/core/objects/PDFNumber.ts
+++ b/src/core/objects/PDFNumber.ts
@@ -14,6 +14,11 @@ class PDFNumber extends PDFObject {
     this.stringValue = numberToString(value);
   }
 
+  asNumber(): number {
+    return this.numberValue;
+  }
+
+  /** @deprecated in favor of [[PDFNumber.asNumber]] */
   value(): number {
     return this.numberValue;
   }
diff --git a/src/core/objects/PDFRawStream.ts b/src/core/objects/PDFRawStream.ts
index 1315d0c..e867eba 100644
--- a/src/core/objects/PDFRawStream.ts
+++ b/src/core/objects/PDFRawStream.ts
@@ -14,6 +14,10 @@ class PDFRawStream extends PDFStream {
     this.contents = contents;
   }
 
+  asUint8Array(): Uint8Array {
+    return this.contents.slice();
+  }
+
   clone(context?: PDFContext): PDFRawStream {
     return PDFRawStream.of(this.dict.clone(context), this.contents.slice());
   }
diff --git a/src/core/objects/PDFRef.ts b/src/core/objects/PDFRef.ts
index dfde3f0..513f873 100644
--- a/src/core/objects/PDFRef.ts
+++ b/src/core/objects/PDFRef.ts
@@ -34,7 +34,7 @@ class PDFRef extends PDFObject {
     this.tag = `${objectNumber} ${generationNumber} R`;
   }
 
-  clone() {
+  clone(): PDFRef {
     return this;
   }
 
diff --git a/src/core/objects/PDFString.ts b/src/core/objects/PDFString.ts
index ffc5191..93bdf82 100644
--- a/src/core/objects/PDFString.ts
+++ b/src/core/objects/PDFString.ts
@@ -25,6 +25,10 @@ class PDFString extends PDFObject {
     this.value = value;
   }
 
+  asString(): string {
+    return this.value;
+  }
+
   clone(): PDFString {
     return PDFString.of(this.value);
   }
diff --git a/src/core/parser/PDFObjectParser.ts b/src/core/parser/PDFObjectParser.ts
index 04d3263..20e1ba4 100644
--- a/src/core/parser/PDFObjectParser.ts
+++ b/src/core/parser/PDFObjectParser.ts
@@ -226,7 +226,7 @@ class PDFObjectParser extends BaseParser {
 
     const Length = dict.get(PDFName.of('Length'));
     if (Length instanceof PDFNumber) {
-      end = start + Length.value();
+      end = start + Length.asNumber();
       this.bytes.moveTo(end);
       this.skipWhitespaceAndComments();
       if (!this.matchKeyword(Keywords.endstream)) {
diff --git a/src/core/parser/PDFObjectStreamParser.ts b/src/core/parser/PDFObjectStreamParser.ts
index ba4a51b..47dd043 100644
--- a/src/core/parser/PDFObjectStreamParser.ts
+++ b/src/core/parser/PDFObjectStreamParser.ts
@@ -25,8 +25,8 @@ class PDFObjectStreamParser extends PDFObjectParser {
 
     this.alreadyParsed = false;
     this.shouldWaitForTick = shouldWaitForTick || (() => false);
-    this.firstOffset = dict.lookup(PDFName.of('First'), PDFNumber).value();
-    this.objectCount = dict.lookup(PDFName.of('N'), PDFNumber).value();
+    this.firstOffset = dict.lookup(PDFName.of('First'), PDFNumber).asNumber();
+    this.objectCount = dict.lookup(PDFName.of('N'), PDFNumber).asNumber();
   }
 
   async parseIntoContext(): Promise<void> {
diff --git a/src/core/parser/PDFXRefStreamParser.ts b/src/core/parser/PDFXRefStreamParser.ts
index 4aa2e57..e452d11 100644
--- a/src/core/parser/PDFXRefStreamParser.ts
+++ b/src/core/parser/PDFXRefStreamParser.ts
@@ -43,18 +43,18 @@ class PDFXRefStreamParser {
     if (Index instanceof PDFArray) {
       this.subsections = [];
       for (let idx = 0, len = Index.size(); idx < len; idx += 2) {
-        const firstObjectNumber = Index.lookup(idx + 0, PDFNumber).value();
-        const length = Index.lookup(idx + 1, PDFNumber).value();
+        const firstObjectNumber = Index.lookup(idx + 0, PDFNumber).asNumber();
+        const length = Index.lookup(idx + 1, PDFNumber).asNumber();
         this.subsections.push({ firstObjectNumber, length });
       }
     } else {
-      this.subsections = [{ firstObjectNumber: 0, length: Size.value() }];
+      this.subsections = [{ firstObjectNumber: 0, length: Size.asNumber() }];
     }
 
     const W = this.dict.lookup(PDFName.of('W'), PDFArray);
     this.byteWidths = [-1, -1, -1];
     for (let idx = 0, len = W.size(); idx < len; idx++) {
-      this.byteWidths[idx] = W.lookup(idx, PDFNumber).value();
+      this.byteWidths[idx] = W.lookup(idx, PDFNumber).asNumber();
     }
   }
 
diff --git a/src/core/streams/decode.ts b/src/core/streams/decode.ts
index 2f29869..c9bd689 100644
--- a/src/core/streams/decode.ts
+++ b/src/core/streams/decode.ts
@@ -28,7 +28,7 @@ const decodeStream = (
     if (params instanceof PDFDict) {
       const EarlyChange = params.lookup(PDFName.of('EarlyChange'));
       if (EarlyChange instanceof PDFNumber) {
-        earlyChange = EarlyChange.value();
+        earlyChange = EarlyChange.asNumber();
       }
     }
     return new LZWStream(stream, undefined, earlyChange as 0 | 1);
@@ -42,7 +42,7 @@ const decodeStream = (
   if (encoding === PDFName.of('RunLengthDecode')) {
     return new RunLengthStream(stream);
   }
-  throw new UnsupportedEncodingError(encoding.value());
+  throw new UnsupportedEncodingError(encoding.asString());
 };
 
 export const decodePDFRawStream = ({ dict, contents }: PDFRawStream) => {
@@ -52,13 +52,17 @@ export const decodePDFRawStream = ({ dict, contents }: PDFRawStream) => {
   const DecodeParms = dict.lookup(PDFName.of('DecodeParms'));
 
   if (Filter instanceof PDFName) {
-    stream = decodeStream(stream, Filter, DecodeParms);
+    stream = decodeStream(
+      stream,
+      Filter,
+      DecodeParms as PDFDict | typeof PDFNull | undefined,
+    );
   } else if (Filter instanceof PDFArray) {
     for (let idx = 0, len = Filter.size(); idx < len; idx++) {
       stream = decodeStream(
         stream,
         Filter.lookup(idx, PDFName),
-        DecodeParms && (DecodeParms as PDFArray).lookup(idx),
+        DecodeParms && (DecodeParms as PDFArray).lookupMaybe(idx, PDFDict),
       );
     }
   } else if (!!Filter) {
diff --git a/src/core/structures/PDFPageTree.ts b/src/core/structures/PDFPageTree.ts
index 7589841..601275f 100644
--- a/src/core/structures/PDFPageTree.ts
+++ b/src/core/structures/PDFPageTree.ts
@@ -43,7 +43,7 @@ class PDFPageTree extends PDFDict {
     Kids.push(leafRef);
     this.ascend((node) => {
       const Count = node.Count();
-      node.set(PDFName.of('Count'), PDFNumber.of(Count.value() + 1));
+      node.set(PDFName.of('Count'), PDFNumber.of(Count.asNumber() + 1));
     });
   }
 
@@ -71,7 +71,7 @@ class PDFPageTree extends PDFDict {
       const kid = this.context.lookup(kidRef) as TreeNode;
 
       if (kid instanceof PDFPageTree) {
-        const kidCount = kid.Count().value();
+        const kidCount = kid.Count().asNumber();
         if (pageIdx + kidCount > targetIndex) {
           return kid.insertLeafNode(leafRef, targetIndex - pageIdx) || kidRef;
         } else {
@@ -85,7 +85,7 @@ class PDFPageTree extends PDFDict {
     Kids.insert(kidIdx, leafRef);
     this.ascend((node) => {
       const Count = node.Count();
-      node.set(PDFName.of('Count'), PDFNumber.of(Count.value() + 1));
+      node.set(PDFName.of('Count'), PDFNumber.of(Count.asNumber() + 1));
     });
 
     return undefined;
@@ -111,7 +111,7 @@ class PDFPageTree extends PDFDict {
       const kid = this.context.lookup(kidRef) as TreeNode;
 
       if (kid instanceof PDFPageTree) {
-        const kidCount = kid.Count().value();
+        const kidCount = kid.Count().asNumber();
         if (pageIdx + kidCount > targetIndex) {
           kid.removeLeafNode(targetIndex - pageIdx);
           return;
@@ -134,7 +134,7 @@ class PDFPageTree extends PDFDict {
       Kids.remove(kidIdx);
       this.ascend((node) => {
         const Count = node.Count();
-        node.set(PDFName.of('Count'), PDFNumber.of(Count.value() - 1));
+        node.set(PDFName.of('Count'), PDFNumber.of(Count.asNumber() - 1));
       });
     }
   }


Test patch:
diff --git a/tests/core/objects/PDFArray.spec.ts b/tests/core/objects/PDFArray.spec.ts
index dd196df..59e8f75 100644
--- a/tests/core/objects/PDFArray.spec.ts
+++ b/tests/core/objects/PDFArray.spec.ts
@@ -92,6 +92,19 @@ describe(`PDFArray`, () => {
     expect(array.size()).toBe(2);
   });
 
+  it(`can be converted to an Array`, () => {
+    expect(pdfArray.asArray()).toEqual([
+      pdfBool,
+      pdfHexString,
+      pdfName,
+      pdfNull,
+      pdfNumber,
+      pdfString,
+      pdfSubArray,
+      pdfRef,
+    ]);
+  });
+
   it(`can be cloned`, () => {
     const original = pdfArray;
     const clone = original.clone();
diff --git a/tests/core/objects/PDFBool.spec.ts b/tests/core/objects/PDFBool.spec.ts
index 50a30cf..7bab13e 100644
--- a/tests/core/objects/PDFBool.spec.ts
+++ b/tests/core/objects/PDFBool.spec.ts
@@ -8,6 +8,11 @@ describe(`PDFBool`, () => {
     );
   });
 
+  it(`can be converted to a boolean`, () => {
+    expect(PDFBool.True.asBoolean()).toBe(true);
+    expect(PDFBool.False.asBoolean()).toBe(false);
+  });
+
   it(`can be cloned`, () => {
     expect(PDFBool.True.clone()).toBe(PDFBool.True);
     expect(PDFBool.False.clone()).toBe(PDFBool.False);
diff --git a/tests/core/objects/PDFDict.spec.ts b/tests/core/objects/PDFDict.spec.ts
index 9697ebb..b8ecb30 100644
--- a/tests/core/objects/PDFDict.spec.ts
+++ b/tests/core/objects/PDFDict.spec.ts
@@ -9,6 +9,7 @@ import {
   PDFNumber,
   PDFRef,
   PDFString,
+  PDFObject,
 } from 'src/core';
 import { toCharCode, typedArrayFor } from 'src/utils';
 
@@ -60,6 +61,21 @@ describe(`PDFDict`, () => {
     expect(pdfSubDict.get(PDFName.of('Array'))).toBe(pdfArray);
   });
 
+  it(`can be converted to a Map`, () => {
+    expect(pdfDict.asMap()).toEqual(
+      new Map<PDFName, PDFObject>([
+        [PDFName.of('Boolean'), pdfBool],
+        [PDFName.of('HexString'), pdfHexString],
+        [PDFName.of('Name'), pdfName],
+        [PDFName.of('Null'), pdfNull],
+        [PDFName.of('Number'), pdfNumber],
+        [PDFName.of('String'), pdfString],
+        [PDFName.of('Ref'), pdfRef],
+        [PDFName.of('Dictionary'), pdfSubDict],
+      ]),
+    );
+  });
+
   it(`can be cloned`, () => {
     const original = pdfDict;
     const clone = original.clone();
diff --git a/tests/core/objects/PDFHexString.spec.ts b/tests/core/objects/PDFHexString.spec.ts
index 26609b4..5c47453 100644
--- a/tests/core/objects/PDFHexString.spec.ts
+++ b/tests/core/objects/PDFHexString.spec.ts
@@ -18,6 +18,13 @@ describe(`PDFHexString`, () => {
     );
   });
 
+  it(`can be converted to a string`, () => {
+    expect(PDFHexString.of('901FA').asString()).toBe('901FA');
+    expect(PDFHexString.fromText('stuff 💩 and 🎂things').asString()).toBe(
+      'FEFF007300740075006600660020D83DDCA900200061006E00640020D83CDF82007400680069006E00670073',
+    );
+  });
+
   it(`can be cloned`, () => {
     const original = PDFHexString.of('901FA');
     const clone = original.clone();
diff --git a/tests/core/objects/PDFNull.spec.ts b/tests/core/objects/PDFNull.spec.ts
index 86eeb5d..1ebfc3f 100644
--- a/tests/core/objects/PDFNull.spec.ts
+++ b/tests/core/objects/PDFNull.spec.ts
@@ -6,6 +6,10 @@ describe(`PDFNull`, () => {
     expect(() => new (PDFNull as any)()).toThrow();
   });
 
+  it(`can be converted to null`, () => {
+    expect(PDFNull.asNull()).toBe(null);
+  });
+
   it(`can be cloned`, () => {
     expect(PDFNull.clone()).toBe(PDFNull);
   });
diff --git a/tests/core/objects/PDFRawStream.spec.ts b/tests/core/objects/PDFRawStream.spec.ts
index 680d94a..dd078fc 100644
--- a/tests/core/objects/PDFRawStream.spec.ts
+++ b/tests/core/objects/PDFRawStream.spec.ts
@@ -10,6 +10,10 @@ describe(`PDFRawStream`, () => {
     expect(PDFRawStream.of(dict, data)).toBeInstanceOf(PDFRawStream);
   });
 
+  it(`can be converted to a Uint8Array`, () => {
+    expect(PDFRawStream.of(dict, data).asUint8Array()).toEqual(data);
+  });
+
   it(`can be cloned`, () => {
     const original = PDFRawStream.of(dict, data);
     const clone = original.clone();
diff --git a/tests/core/objects/PDFString.spec.ts b/tests/core/objects/PDFString.spec.ts
index 4a62d78..1fe3ad9 100644
--- a/tests/core/objects/PDFString.spec.ts
+++ b/tests/core/objects/PDFString.spec.ts
@@ -16,6 +16,13 @@ describe(`PDFString`, () => {
     expect(String(PDFString.fromDate(date2))).toBe('(D:20191221070011Z)');
   });
 
+  it(`can be converted to a string`, () => {
+    expect(PDFString.of('foobar').asString()).toBe('foobar');
+
+    const date = new Date('2018-06-24T01:58:37.228Z');
+    expect(PDFString.fromDate(date).asString()).toBe('D:20180624015837Z');
+  });
+
   it(`can be cloned`, () => {
     const original = PDFString.of(')b\\a/z(');
     const clone = original.clone();
diff --git a/tests/core/structures/PDFCatalog.spec.ts b/tests/core/structures/PDFCatalog.spec.ts
index 44caf94..7a966ce 100644
--- a/tests/core/structures/PDFCatalog.spec.ts
+++ b/tests/core/structures/PDFCatalog.spec.ts
@@ -63,7 +63,7 @@ describe(`PDFCatalog`, () => {
     pageTree1.pushLeafNode(leafRef1);
     pageTree1.pushLeafNode(leafRef2);
 
-    expect(pageTree1.Count().value()).toBe(2);
+    expect(pageTree1.Count().asNumber()).toBe(2);
     expect(pageTree1.Kids().get(1)).toBe(leafRef2);
     expect(pageTree1.Kids().get(2)).toBe(undefined);
 
@@ -71,7 +71,7 @@ describe(`PDFCatalog`, () => {
     const newLeafRef = context.register(newLeaf);
     const insertionRef = catalog.insertLeafNode(newLeafRef, 2);
 
-    expect(pageTree1.Count().value()).toBe(3);
+    expect(pageTree1.Count().asNumber()).toBe(3);
 
     expect(insertionRef).toBe(pageTreeRef1);
     expect(pageTree1.Kids().get(1)).toBe(leafRef2);
@@ -95,13 +95,13 @@ describe(`PDFCatalog`, () => {
     pageTree1.pushLeafNode(leafRef1);
     pageTree1.pushLeafNode(leafRef2);
 
-    expect(pageTree1.Count().value()).toBe(2);
+    expect(pageTree1.Count().asNumber()).toBe(2);
     expect(pageTree1.Kids().get(0)).toBe(leafRef1);
     expect(pageTree1.Kids().get(1)).toBe(leafRef2);
 
     catalog.removeLeafNode(1);
 
-    expect(pageTree1.Count().value()).toBe(1);
+    expect(pageTree1.Count().asNumber()).toBe(1);
     expect(pageTree1.Kids().get(0)).toBe(leafRef1);
     expect(pageTree1.Kids().get(1)).toBe(undefined);
   });
diff --git a/tests/core/structures/PDFPageTree.spec.ts b/tests/core/structures/PDFPageTree.spec.ts
index e180fdb..7ce9e1f 100644
--- a/tests/core/structures/PDFPageTree.spec.ts
+++ b/tests/core/structures/PDFPageTree.spec.ts
@@ -57,7 +57,7 @@ describe(`PDFPageTree`, () => {
     expect(pageTree.get(PDFName.of('Kids'))).toBeInstanceOf(PDFArray);
     expect(pageTree.lookup(PDFName.of('Kids'), PDFArray).size()).toBe(0);
     expect(pageTree.get(PDFName.of('Count'))).toBeInstanceOf(PDFNumber);
-    expect(pageTree.lookup(PDFName.of('Count'), PDFNumber).value()).toBe(0);
+    expect(pageTree.lookup(PDFName.of('Count'), PDFNumber).asNumber()).toBe(0);
     expect(pageTree.get(PDFName.of('Parent'))).toBeUndefined();
   });
 
@@ -175,20 +175,20 @@ describe(`PDFPageTree`, () => {
 
       pushLeafNodes(lvl4Tree1, leafRef3);
 
-      expect(lvl1Tree1.Count().value()).toBe(4);
-      expect(lvl2Tree1.Count().value()).toBe(2);
-      expect(lvl3Tree1.Count().value()).toBe(1);
-      expect(lvl4Tree1.Count().value()).toBe(1);
+      expect(lvl1Tree1.Count().asNumber()).toBe(4);
+      expect(lvl2Tree1.Count().asNumber()).toBe(2);
+      expect(lvl3Tree1.Count().asNumber()).toBe(1);
+      expect(lvl4Tree1.Count().asNumber()).toBe(1);
       expect(lvl2Tree1.Kids().get(1)).toBe(lvl3Tree1Ref);
 
       const newLeaf = PDFPageLeaf.withContextAndParent(context, lvl2Tree1Ref);
       const newLeafRef = context.register(newLeaf);
       const insertionRef = lvl1Tree1.insertLeafNode(newLeafRef, 2);
 
-      expect(lvl1Tree1.Count().value()).toBe(5);
-      expect(lvl2Tree1.Count().value()).toBe(3);
-      expect(lvl3Tree1.Count().value()).toBe(1);
-      expect(lvl4Tree1.Count().value()).toBe(1);
+      expect(lvl1Tree1.Count().asNumber()).toBe(5);
+      expect(lvl2Tree1.Count().asNumber()).toBe(3);
+      expect(lvl3Tree1.Count().asNumber()).toBe(1);
+      expect(lvl4Tree1.Count().asNumber()).toBe(1);
 
       expect(insertionRef).toBe(lvl2Tree1Ref);
       expect(lvl2Tree1.Kids().get(1)).toBe(newLeafRef);
@@ -219,18 +219,18 @@ describe(`PDFPageTree`, () => {
 
       pushLeafNodes(lvl3Tree1, leafRef3, leafRef4);
 
-      expect(lvl1Tree1.Count().value()).toBe(6);
-      expect(lvl2Tree1.Count().value()).toBe(4);
-      expect(lvl3Tree1.Count().value()).toBe(2);
+      expect(lvl1Tree1.Count().asNumber()).toBe(6);
+      expect(lvl2Tree1.Count().asNumber()).toBe(4);
+      expect(lvl3Tree1.Count().asNumber()).toBe(2);
       expect(lvl2Tree1.Kids().get(2)).toBe(leafRef5);
 
       const newLeaf = PDFPageLeaf.withContextAndParent(context, lvl2Tree1Ref);
       const newLeafRef = context.register(newLeaf);
       const insertionRef = lvl1Tree1.insertLeafNode(newLeafRef, 4);
 
-      expect(lvl1Tree1.Count().value()).toBe(7);
-      expect(lvl2Tree1.Count().value()).toBe(5);
-      expect(lvl3Tree1.Count().value()).toBe(2);
+      expect(lvl1Tree1.Count().asNumber()).toBe(7);
+      expect(lvl2Tree1.Count().asNumber()).toBe(5);
+      expect(lvl3Tree1.Count().asNumber()).toBe(2);
 
       expect(insertionRef).toBe(lvl2Tree1Ref);
       expect(lvl2Tree1.Kids().get(2)).toBe(newLeafRef);
@@ -248,7 +248,7 @@ describe(`PDFPageTree`, () => {
 
       pushLeafNodes(lvl1Tree1, leafRef1, leafRef2, leafRef3);
 
-      expect(lvl1Tree1.Count().value()).toBe(3);
+      expect(lvl1Tree1.Count().asNumber()).toBe(3);
       expect(lvl1Tree1.Kids().get(2)).toBe(leafRef3);
       expect(lvl1Tree1.Kids().get(3)).toBe(undefined);
 
@@ -256,7 +256,7 @@ describe(`PDFPageTree`, () => {
       const newLeafRef = context.register(newLeaf);
       const insertionRef = lvl1Tree1.insertLeafNode(newLeafRef, 3);
 
-      expect(lvl1Tree1.Count().value()).toBe(4);
+      expect(lvl1Tree1.Count().asNumber()).toBe(4);
 
       expect(insertionRef).toBe(undefined);
       expect(lvl1Tree1.Kids().get(2)).toBe(leafRef3);
@@ -277,7 +277,7 @@ describe(`PDFPageTree`, () => {
       pushTreeNodes(lvl2Tree1, lvl3Tree1Ref);
       pushLeafNodes(lvl3Tree1, leafRef1, leafRef2);
 
-      expect(lvl1Tree1.Count().value()).toBe(2);
+      expect(lvl1Tree1.Count().asNumber()).toBe(2);
       expect(lvl3Tree1.Kids().get(0)).toBe(leafRef1);
       expect(lvl3Tree1.Kids().get(1)).toBe(leafRef2);
 
@@ -285,7 +285,7 @@ describe(`PDFPageTree`, () => {
       const newLeafRef = context.register(newLeaf);
       const insertionRef = lvl1Tree1.insertLeafNode(newLeafRef, 1);
 
-      expect(lvl1Tree1.Count().value()).toBe(3);
+      expect(lvl1Tree1.Count().asNumber()).toBe(3);
 
       expect(insertionRef).toBe(lvl3Tree1Ref);
       expect(lvl3Tree1.Kids().get(0)).toBe(leafRef1);
@@ -335,17 +335,17 @@ describe(`PDFPageTree`, () => {
 
       pushLeafNodes(lvl3Tree1, leafRef3, leafRef4);
 
-      expect(lvl1Tree1.Count().value()).toBe(6);
-      expect(lvl2Tree1.Count().value()).toBe(4);
-      expect(lvl3Tree1.Count().value()).toBe(2);
+      expect(lvl1Tree1.Count().asNumber()).toBe(6);
+      expect(lvl2Tree1.Count().asNumber()).toBe(4);
+      expect(lvl3Tree1.Count().asNumber()).toBe(2);
       expect(lvl2Tree1.Kids().get(1)).toBe(lvl3Tree1Ref);
       expect(lvl2Tree1.Kids().get(2)).toBe(leafRef5);
 
       lvl1Tree1.removeLeafNode(4);
 
-      expect(lvl1Tree1.Count().value()).toBe(5);
-      expect(lvl2Tree1.Count().value()).toBe(3);
-      expect(lvl3Tree1.Count().value()).toBe(2);
+      expect(lvl1Tree1.Count().asNumber()).toBe(5);
+      expect(lvl2Tree1.Count().asNumber()).toBe(3);
+      expect(lvl3Tree1.Count().asNumber()).toBe(2);
 
       expect(lvl2Tree1.Kids().get(1)).toBe(lvl3Tree1Ref);
       expect(lvl2Tree1.Kids().get(2)).toBe(undefined);
@@ -375,19 +375,19 @@ describe(`PDFPageTree`, () => {
 
       pushLeafNodes(lvl4Tree1, leafRef3);
 
-      expect(lvl1Tree1.Count().value()).toBe(4);
-      expect(lvl2Tree1.Count().value()).toBe(2);
-      expect(lvl3Tree1.Count().value()).toBe(1);
-      expect(lvl4Tree1.Count().value()).toBe(1);
+      expect(lvl1Tree1.Count().asNumber()).toBe(4);
+      expect(lvl2Tree1.Count().asNumber()).toBe(2);
+      expect(lvl3Tree1.Count().asNumber()).toBe(1);
+      expect(lvl4Tree1.Count().asNumber()).toBe(1);
       expect(lvl2Tree1.Kids().get(1)).toBe(lvl3Tree1Ref);
       expect(lvl4Tree1.Kids().get(0)).toBe(leafRef3);
 
       lvl1Tree1.removeLeafNode(2);
 
-      expect(lvl1Tree1.Count().value()).toBe(3);
-      expect(lvl2Tree1.Count().value()).toBe(1);
-      expect(lvl3Tree1.Count().value()).toBe(0);
-      expect(lvl4Tree1.Count().value()).toBe(0);
+      expect(lvl1Tree1.Count().asNumber()).toBe(3);
+      expect(lvl2Tree1.Count().asNumber()).toBe(1);
+      expect(lvl3Tree1.Count().asNumber()).toBe(0);
+      expect(lvl4Tree1.Count().asNumber()).toBe(0);
 
       expect(lvl2Tree1.Kids().get(1)).toBe(lvl3Tree1Ref);
       expect(lvl4Tree1.Kids().get(0)).toBe(undefined);
@@ -403,13 +403,13 @@ describe(`PDFPageTree`, () => {
       const [leafRef3] = pageLeaf(lvl1Tree1Ref);
       pushLeafNodes(lvl1Tree1, leafRef1, leafRef2, leafRef3);
 
-      expect(lvl1Tree1.Count().value()).toBe(3);
+      expect(lvl1Tree1.Count().asNumber()).toBe(3);
       expect(lvl1Tree1.Kids().get(1)).toBe(leafRef2);
       expect(lvl1Tree1.Kids().get(2)).toBe(leafRef3);
 
       lvl1Tree1.removeLeafNode(2);
 
-      expect(lvl1Tree1.Count().value()).toBe(2);
+      expect(lvl1Tree1.Count().asNumber()).toBe(2);
       expect(lvl1Tree1.Kids().get(1)).toBe(leafRef2);
       expect(lvl1Tree1.Kids().get(2)).toBe(undefined);
     });