prepare method

  1. @override
void prepare()

Prepare the object to be written to the stream

Implementation

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

  // These are for kids only
  if (parent != null) {
    params['/Title'] = PdfString.fromString(title!);

    if (color != null) {
      params['/C'] = PdfArray.fromColor(color!);
    }

    if (style != PdfOutlineStyle.normal) {
      params['/F'] = PdfNum(style.index);
    }

    if (anchor != null) {
      params['/Dest'] = PdfString.fromString(anchor!);
    } else {
      final dests = PdfArray();
      dests.add(dest!.ref());

      if (destMode == PdfOutlineMode.fitPage) {
        dests.add(const PdfName('/Fit'));
      } else {
        dests.add(const PdfName('/FitR'));
        dests.add(PdfNum(rect!.left));
        dests.add(PdfNum(rect!.bottom));
        dests.add(PdfNum(rect!.right));
        dests.add(PdfNum(rect!.top));
      }
      params['/Dest'] = dests;
    }
    params['/Parent'] = parent!.ref();

    // were a descendent, so by default we are closed. Find out how many
    // entries are below us
    final c = descendants();
    if (c > 0) {
      params['/Count'] = PdfNum(-c);
    }

    final index = parent!.getIndex(this);
    if (index > 0) {
      // Now if were not the first, then we have a /Prev node
      params['/Prev'] = parent!.getNode(index - 1).ref();
    }

    if (index < parent!.getLast()) {
      // We have a /Next node
      params['/Next'] = parent!.getNode(index + 1).ref();
    }
  } else {
    // the number of outlines in this document
    // were the top level node, so all are open by default
    params['/Count'] = PdfNum(outlines.length);
  }

  // These only valid if we have children
  if (outlines.isNotEmpty) {
    // the number of the first outline in list
    params['/First'] = outlines[0].ref();

    // the number of the last outline in list
    params['/Last'] = outlines[outlines.length - 1].ref();
  }
}