insertImported method

PdfPage insertImported(
  1. int index,
  2. PdfDocumentParser source,
  3. int sourcePageIndex, {
  4. PdfMergeOptions? options,
})

Imports page sourcePageIndex of source and inserts it at index.

Reuses the deep import of PdfDocumentMerger through PdfDocument.importPage: content, resources, fonts and annotations of the source are materialized in the target with new object numbers. The target has to be a new document — a loaded document would produce an incremental update containing objects from another file, and a PdfMergeException is thrown.

Implementation

PdfPage insertImported(
  int index,
  PdfDocumentParser source,
  int sourcePageIndex, {
  PdfMergeOptions? options,
}) {
  _checkInsertIndex(index);
  final before = List<PdfPage>.of(_pages);

  // The importer appends the page at the end; bringing it to the requested
  // place is a move within the same list.
  final page = document.importPage(source, sourcePageIndex, options: options);
  final current = indexOf(page);
  if (current >= 0 && current != index) {
    _pages.removeAt(current);
    _pages.insert(index > current ? index - 1 : index, page);
  }

  _afterMutation(before);
  return page;
}