* Insert an existing page at the given index. * * @param index - Position to insert (0 = first, negative = append) * @param page - The page dict or ref to insert * @returns The page reference * @throws {Error} If page reference does not point to a dictionary * @throws {RangeError}
(index: number, page: PdfDict | PdfRef)
| 1655 | * @throws {RangeError} If index is out of bounds |
| 1656 | */ |
| 1657 | insertPage(index: number, page: PdfDict | PdfRef): PdfRef { |
| 1658 | let pageRef: PdfRef; |
| 1659 | let pageDict: PdfDict; |
| 1660 | |
| 1661 | if (page instanceof PdfRef) { |
| 1662 | pageRef = page; |
| 1663 | // Get the dict from registry or throw |
| 1664 | const obj = this.ctx.registry.getObject(page); |
| 1665 | |
| 1666 | if (!(obj instanceof PdfDict)) { |
| 1667 | throw new Error("Page reference does not point to a dictionary"); |
| 1668 | } |
| 1669 | |
| 1670 | pageDict = obj; |
| 1671 | } else { |
| 1672 | // Register the dict and get a ref |
| 1673 | pageRef = this.register(page); |
| 1674 | pageDict = page; |
| 1675 | } |
| 1676 | |
| 1677 | this.ctx.pages.insertPage(index, pageRef, pageDict); |
| 1678 | |
| 1679 | return pageRef; |
| 1680 | } |
| 1681 | |
| 1682 | /** |
| 1683 | * Remove the page at the given index. |
no test coverage detected