• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

adnsistemas / pdf-lib / #18

24 Mar 2026 08:15PM UTC coverage: 74.286% (+0.3%) from 74.001%
#18

push

David N. Abdala
Documentation change

2569 of 3981 branches covered (64.53%)

Branch coverage included in aggregate %.

7372 of 9401 relevant lines covered (78.42%)

297170.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.67
/src/core/annotation/PDFWidgetAnnotation.ts
1
import PDFDict from '../objects/PDFDict';
2
import PDFName from '../objects/PDFName';
54✔
3
import PDFRef from '../objects/PDFRef';
4
import PDFString from '../objects/PDFString';
54✔
5
import PDFHexString from '../objects/PDFHexString';
6
import PDFContext from '../PDFContext';
7
import BorderStyle from './BorderStyle';
54✔
8
import PDFAnnotation from './PDFAnnotation';
54✔
9
import AppearanceCharacteristics from './AppearanceCharacteristics';
54✔
10
import { isPDFInstance, PDFClasses } from '../../api/objects';
54✔
11

12
class PDFWidgetAnnotation extends PDFAnnotation {
13
  static fromDict = (dict: PDFDict): PDFWidgetAnnotation =>
54✔
14
    new PDFWidgetAnnotation(dict);
289✔
15

16
  static create = (context: PDFContext, parent: PDFRef) => {
54✔
17
    const dict = context.obj({
29✔
18
      Type: 'Annot',
19
      Subtype: 'Widget',
20
      Rect: [0, 0, 0, 0],
21
      Parent: parent,
22
    });
23
    return new PDFWidgetAnnotation(dict);
29✔
24
  };
25

26
  MK(): PDFDict | undefined {
27
    const MK = this.dict.lookup(PDFName.of('MK'));
77✔
28
    if (isPDFInstance(MK, PDFClasses.PDFDict)) return MK as PDFDict;
77✔
29
    return undefined;
26✔
30
  }
31

32
  BS(): PDFDict | undefined {
33
    const BS = this.dict.lookup(PDFName.of('BS'));
77✔
34
    if (isPDFInstance(BS, PDFClasses.PDFDict)) return BS as PDFDict;
77✔
35
    return undefined;
35✔
36
  }
37

38
  DA(): PDFString | PDFHexString | undefined {
39
    const da = this.dict.lookup(PDFName.of('DA'));
86✔
40
    if (
86✔
41
      isPDFInstance(da, PDFClasses.PDFString) ||
139✔
42
      isPDFInstance(da, PDFClasses.PDFHexString)
43
    )
44
      return da as PDFString | PDFHexString;
34✔
45
    return undefined;
52✔
46
  }
47

48
  P(): PDFRef | undefined {
49
    const P = this.dict.get(PDFName.of('P'));
12✔
50
    if (isPDFInstance(P, PDFClasses.PDFRef)) return P as PDFRef;
12✔
51
    return undefined;
×
52
  }
53

54
  setP(page: PDFRef) {
55
    this.dict.set(PDFName.of('P'), page);
26✔
56
  }
57

58
  setDefaultAppearance(appearance: string) {
59
    this.dict.set(PDFName.of('DA'), PDFString.of(appearance));
18✔
60
  }
61

62
  getDefaultAppearance(): string | undefined {
63
    const DA = this.DA();
86✔
64

65
    if (isPDFInstance(DA, PDFClasses.PDFHexString)) {
86✔
66
      return (DA as PDFHexString).decodeText();
1✔
67
    }
68

69
    return DA?.asString();
85✔
70
  }
71

72
  getAppearanceCharacteristics(): AppearanceCharacteristics | undefined {
73
    const MK = this.MK();
51✔
74
    if (MK) return AppearanceCharacteristics.fromDict(MK);
51✔
75
    return undefined;
×
76
  }
77

78
  getOrCreateAppearanceCharacteristics(): AppearanceCharacteristics {
79
    const MK = this.MK();
26✔
80
    if (MK) return AppearanceCharacteristics.fromDict(MK);
26!
81

82
    const ac = AppearanceCharacteristics.fromDict(this.dict.context.obj({}));
26✔
83
    this.dict.set(PDFName.of('MK'), ac.dict);
26✔
84
    return ac;
26✔
85
  }
86

87
  getBorderStyle(): BorderStyle | undefined {
88
    const BS = this.BS();
51✔
89
    if (BS) return BorderStyle.fromDict(BS);
51✔
90
    return undefined;
9✔
91
  }
92

93
  getOrCreateBorderStyle(): BorderStyle {
94
    const BS = this.BS();
26✔
95
    if (BS) return BorderStyle.fromDict(BS);
26!
96

97
    const bs = BorderStyle.fromDict(this.dict.context.obj({}));
26✔
98
    this.dict.set(PDFName.of('BS'), bs.dict);
26✔
99
    return bs;
26✔
100
  }
101

102
  getOnValue(): PDFName | undefined {
103
    const normal = this.getAppearances()?.normal;
167✔
104

105
    if (isPDFInstance(normal, PDFClasses.PDFDict)) {
167✔
106
      const keys = (normal as PDFDict).keys();
158✔
107
      for (let idx = 0, len = keys.length; idx < len; idx++) {
158✔
108
        const key = keys[idx];
192✔
109
        if (key !== PDFName.of('Off')) return key;
192✔
110
      }
111
    }
112

113
    return undefined;
9✔
114
  }
115
}
116

117
export default PDFWidgetAnnotation;
54✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc