prepare method

  1. @override
void prepare()
override

Prepare the object to be written to the stream

Implementation

@override
void prepare() {
  super.prepare();

  /// the PDF specification version, overrides the header version starting from 1.4
  params['/Version'] = PdfName('/${pdfDocument.versionString}');

  params['/Pages'] = pdfPageList.ref();

  // the Outlines object
  if (outlines != null && outlines!.outlines.isNotEmpty) {
    params['/Outlines'] = outlines!.ref();
  }

  if (metadata != null) {
    params['/Metadata'] = metadata!.ref();
  }

  // the Names object
  if (names != null) {
    params['/Names'] = names!.ref();
  }

  // the PageLabels object
  if (pageLabels != null && pageLabels!.labels.isNotEmpty) {
    params['/PageLabels'] = pageLabels!.ref();
  }

  // the /PageMode setting
  if (pageMode != null) {
    params['/PageMode'] = PdfName(_pdfPageModes[pageMode!.index]);
  }

  if (pdfDocument.sign != null) {
    if (pdfDocument.sign!.value.hasMDP) {
      params['/Perms'] = PdfDict.values({
        '/DocMDP': pdfDocument.sign!.ref(),
      });
    }

    final dss = PdfDict();
    if (pdfDocument.sign!.crl.isNotEmpty) {
      dss['/CRLs'] = PdfArray.fromObjects(pdfDocument.sign!.crl);
    }
    if (pdfDocument.sign!.cert.isNotEmpty) {
      dss['/Certs'] = PdfArray.fromObjects(pdfDocument.sign!.cert);
    }
    if (pdfDocument.sign!.ocsp.isNotEmpty) {
      dss['/OCSPs'] = PdfArray.fromObjects(pdfDocument.sign!.ocsp);
    }

    if (dss.values.isNotEmpty) {
      params['/DSS'] = dss;
    }
  }

  final widgets = <PdfAnnot>[];
  for (final page in pdfDocument.pdfPageList.pages) {
    for (final annot in page.annotations) {
      if (annot.annot.subtype == '/Widget') {
        widgets.add(annot);
      }
    }
  }

  if (widgets.isNotEmpty) {
    final acroForm = (params['/AcroForm'] ??= PdfDict()) as PdfDict;
    acroForm['/SigFlags'] = PdfNum(pdfDocument.sign?.flagsValue ?? 0) |
        (acroForm['/SigFlags'] as PdfNum? ?? const PdfNum(0));
    final fields = (acroForm['/Fields'] ??= PdfArray()) as PdfArray;
    final fontRefs = PdfDict();
    for (final w in widgets) {
      if (w.annot is PdfTextField) {
        // collect textfield font references
        final tf = w.annot as PdfTextField;
        fontRefs.addAll(PdfDict.values({tf.font.name: tf.font.ref()}));
      }
      final ref = w.ref();
      if (!fields.values.contains(ref)) {
        fields.add(ref);
      }
    }
    if (fontRefs.isNotEmpty) {
      acroForm['/DR'] = PdfDict.values(// "Document Resources"
          {'/Font': fontRefs});
    }
  }
}