@pdfme/pdf-lib
Version: 
Create and modify PDF files with JavaScript
112 lines (90 loc) • 3.06 kB
text/typescript
import PDFDict from '../objects/PDFDict';
import PDFName from '../objects/PDFName';
import PDFRef from '../objects/PDFRef';
import PDFString from '../objects/PDFString';
import PDFHexString from '../objects/PDFHexString';
import PDFContext from '../PDFContext';
import BorderStyle from './BorderStyle';
import PDFAnnotation from './PDFAnnotation';
import AppearanceCharacteristics from './AppearanceCharacteristics';
class PDFWidgetAnnotation extends PDFAnnotation {
  static fromDict = (dict: PDFDict): PDFWidgetAnnotation => new PDFWidgetAnnotation(dict);
  static create = (context: PDFContext, parent: PDFRef) => {
    const dict = context.obj({
      Type: 'Annot',
      Subtype: 'Widget',
      Rect: [0, 0, 0, 0],
      Parent: parent,
    });
    return new PDFWidgetAnnotation(dict);
  };
  MK(): PDFDict | undefined {
    const MK = this.dict.lookup(PDFName.of('MK'));
    if (MK instanceof PDFDict) return MK;
    return undefined;
  }
  BS(): PDFDict | undefined {
    const BS = this.dict.lookup(PDFName.of('BS'));
    if (BS instanceof PDFDict) return BS;
    return undefined;
  }
  DA(): PDFString | PDFHexString | undefined {
    const da = this.dict.lookup(PDFName.of('DA'));
    if (da instanceof PDFString || da instanceof PDFHexString) return da;
    return undefined;
  }
  P(): PDFRef | undefined {
    const P = this.dict.get(PDFName.of('P'));
    if (P instanceof PDFRef) return P;
    return undefined;
  }
  setP(page: PDFRef) {
    this.dict.set(PDFName.of('P'), page);
  }
  setDefaultAppearance(appearance: string) {
    this.dict.set(PDFName.of('DA'), PDFString.of(appearance));
  }
  getDefaultAppearance(): string | undefined {
    const DA = this.DA();
    if (DA instanceof PDFHexString) {
      return DA.decodeText();
    }
    return DA?.asString();
  }
  getAppearanceCharacteristics(): AppearanceCharacteristics | undefined {
    const MK = this.MK();
    if (MK) return AppearanceCharacteristics.fromDict(MK);
    return undefined;
  }
  getOrCreateAppearanceCharacteristics(): AppearanceCharacteristics {
    const MK = this.MK();
    if (MK) return AppearanceCharacteristics.fromDict(MK);
    const ac = AppearanceCharacteristics.fromDict(this.dict.context.obj({}));
    this.dict.set(PDFName.of('MK'), ac.dict);
    return ac;
  }
  getBorderStyle(): BorderStyle | undefined {
    const BS = this.BS();
    if (BS) return BorderStyle.fromDict(BS);
    return undefined;
  }
  getOrCreateBorderStyle(): BorderStyle {
    const BS = this.BS();
    if (BS) return BorderStyle.fromDict(BS);
    const bs = BorderStyle.fromDict(this.dict.context.obj({}));
    this.dict.set(PDFName.of('BS'), bs.dict);
    return bs;
  }
  getOnValue(): PDFName | undefined {
    const normal = this.getAppearances()?.normal;
    if (normal instanceof PDFDict) {
      const keys = normal.keys();
      for (let idx = 0, len = keys.length; idx < len; idx++) {
        const key = keys[idx];
        if (key !== PDFName.of('Off')) return key;
      }
    }
    return undefined;
  }
}
export default PDFWidgetAnnotation;