import { useEffect, useRef, useState } from 'react'; import { Viewer, DocumentGate, usePageList } from '@embedpdf/react/runtime'; import type { OpenInput, PageRef } from '@embedpdf/react/runtime'; import { Stage, stagePlugin, useStage, useStageState } from '@embedpdf/react/stage'; import { RenderLayer, renderPlugin } from '@embedpdf/react/render'; import { pageEditPlugin, usePageEdit } from '@embedpdf/react/page-edit'; import { localEngine } from '@embedpdf/engine'; import './insert.css'; const engine = localEngine(); const plugins = [stagePlugin(), renderPlugin(), pageEditPlugin()]; const welcome = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/welcome.pdf'); return { kind: 'bytes', id: 'welcome', bytes: new Uint8Array(await response.arrayBuffer()) }; }; /** Another PDF's bytes, to insert pages from. */ const otherPdf = async () => (await fetch('https://www.embedpdf.com/pdfs/welcome.pdf')).arrayBuffer(); // Inserts next to the page you're on, then goes to the first new page. function InsertToolbar() { const pageEdit = usePageEdit(); const stage = useStage(); const pages = usePageList(); const currentPageIndex = useStageState((state) => state.currentPageIndex); const page = pages[currentPageIndex]; const [added, setAdded] = useState([]); // Every insert resolves { pages }: the new pages, to go to or select. const show = (result: { pages: readonly PageRef[] }) => { setAdded(result.pages); stage.goToPage(result.pages[0]); }; // A blank page after the cover, once, on load. const inserted = useRef(false); useEffect(() => { if (inserted.current || !pages[0]) return; inserted.current = true; void pageEdit.insertBlank({ position: { after: pages[0].ref } }).then(show); }); if (!page) return null; const canEdit = pageEdit.canEdit(); const positions = added .map((ref) => pages.findIndex((each) => each.ref.objectNumber === ref.objectNumber) + 1) .filter((position) => position > 0); return (
{positions.length > 0 ? `New: page ${positions.join(' and ')} of ${pages.length}` : `${pages.length} pages`}
); } export default function App() { return ( Loading…

}> {() => }
); }