prepare method

  1. @override
void prepare()

Prepare the object to be written to the stream

Implementation

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

  // the /Parent pages object
  params['/Parent'] = pdfDocument.pdfPageList.ref();

  if (rotate != PdfPageRotation.none) {
    params['/Rotate'] = PdfNum(rotate.index * 90);
  }

  // the /MediaBox for the page size
  params['/MediaBox'] =
      PdfArray.fromNum(<double>[0, 0, pageFormat.width, pageFormat.height]);

  for (final content in contents) {
    if (!_contentGraphics[content]!.altered) {
      content.inUse = false;
    }
  }

  // The graphic operations to draw the page
  final contentList =
      PdfArray.fromObjects(contents.where((e) => e.inUse).toList());

  if (params.containsKey('/Contents')) {
    final prevContent = params['/Contents']!;
    if (prevContent is PdfArray) {
      contentList.values
          .insertAll(0, prevContent.values.whereType<PdfIndirect>());
    } else if (prevContent is PdfIndirect) {
      contentList.values.insert(0, prevContent);
    }
  }

  contentList.uniq();

  if (contentList.values.length == 1) {
    params['/Contents'] = contentList.values.first;
  } else if (contentList.isNotEmpty) {
    params['/Contents'] = contentList;
  }

  // The /Annots object
  if (annotations.isNotEmpty) {
    if (params.containsKey('/Annots')) {
      final annotationList = params['/Annots'];
      if (annotationList is PdfArray) {
        annotationList.values
            .addAll(PdfArray.fromObjects(annotations).values);
      }
    } else {
      params['/Annots'] = PdfArray.fromObjects(annotations);
    }
  }
}