Repository: mozilla/pdf.js
File: src/core/editor/pdf_editor.js (JavaScript)

Code before:
      return;
    }

    const promises = [];
    let newAnnotations = [];
    let newIndex = 0;

    // TODO: remove only links to deleted pages.
    for (const annotationRef of annotations) {
      const newAnnotationIndex = newIndex++;
      promises.push(
        xref.fetchIfRefAsync(annotationRef).then(async annotationDict => {
          if (!isName(annotationDict.get("Subtype"), "Link")) {
            newAnnotations[newAnnotationIndex] = annotationRef;
          }
        })
      );
    }
    await Promise.all(promises);
    newAnnotations = newAnnotations.filter(annot => !!annot);
    pageData.annotations = newAnnotations.length > 0 ? newAnnotations : null;
  }

  async #collectPageLabels() {
    // We can only preserve page labels when editing a single PDF file.
    // Acrobat does the same and I'm not sure that it makes sense otherwise.
    if (!this.hasSingleFile) {
      return;
    }
    const {
      page: {
        pdfManager: { pdfDocument },
      },
    } = this.oldPages[0];
    const { pageLabels } = this.documentData.values().next().value;
    if (!pageLabels) {
      return;
    }
    const numPages = pdfDocument.numPages;
    const oldPageLabels = [];
    const oldPageIndices = new Set(
      this.oldPages.map(({ page: { pageIndex } }) => pageIndex)
    );
    let currentLabel = null;
    for (let i = 0; i < numPages; i++) {
      currentLabel = pageLabels.get(i) || currentLabel;
      if (!oldPageIndices.has(i)) {
        continue;
      }
      oldPageLabels.push(currentLabel);
    }

Review comment (suggestion):
I also don't see how this is expected to work for multiple PDF documents. If Acrobat does the same, I would rephrase this line to `This is consistent with behavior in Adobe Acrobat.`

Code after:
      currentIndex = i;
      currentLabel = label;
      newPageLabels.push([currentIndex, currentLabel]);
    }
  }

  /**
   * Create a copy of a page.
   * @param {number} pageIndex
   * @returns {Promise<Ref>} the page reference in the new PDF document.
   */
  async #makePageCopy(pageIndex) {
    const { page, documentData, annotations } = this.oldPages[pageIndex];
    this.currentDocument = documentData;
    const { oldRefMapping } = documentData;
    const { xref, rotate, mediaBox, resources, ref: oldPageRef } = page;
    const pageRef = this.newRef;
    const pageDict = (this.xref[pageRef.num] = page.pageDict.clone());
    oldRefMapping.put(oldPageRef, pageRef);

    // No need to keep these entries as we'll set them again later.
    for (const key of [
      "Rotate",
      "MediaBox",
      "CropBox",
      "BleedBox",
      "TrimBox",
      "ArtBox",
      "Resources",
      "Annots",
      "Parent",
      "UserUnit",
    ]) {
      pageDict.delete(key);
    }

    const lastRef = this.newRefCount;
    await this.#collectDependencies(pageDict, false, xref);

    pageDict.set("Rotate", rotate);
    pageDict.set("MediaBox", mediaBox);
    for (const boxName of ["CropBox", "BleedBox", "TrimBox", "ArtBox"]) {
      const box = page.getBoundingBox(boxName);
      if (box?.some((value, index) => value !== mediaBox[index])) {
        // These boxes are optional and their default value is the MediaBox.
        pageDict.set(boxName, box);
      }
    }
    const userUnit = page.userUnit;
    if (userUnit !== 1) {
      pageDict.set("UserUnit", userUnit);