Sample JavaScript code for using Apryse SDK to copy pages from one document to another, delete and rearrange pages, and use ImportPages() method for very efficient copy and merge operations.
This sample is utilizing the option to run code programmatically, without the Viewer.
Learn more about our Web SDK and PDF Editing & Manipulation Library.
Step 1: Follow get started in your preferred web stack for WebViewer
Step 2: Implement Full API code without viewer
Step 3: Add the sample code provided in this guide
This full sample is included in the manual download of WebViewer.
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5(exports => {
6 exports.runPDFPageTest = () => {
7 const PDFNet = exports.Core.PDFNet;
8
9 const main = async () => {
10 console.log('Beginning Test');
11 const inputPath = '../TestFiles/';
12 let docStoreArray = null;
13
14 // split a pdf into multiple separate pdf pages
15 try {
16 const inDoc = await PDFNet.PDFDoc.createFromURL(inputPath + 'newsletter.pdf');
17 inDoc.initSecurityHandler();
18 inDoc.lock();
19
20 console.log('PDF document initialized and locked');
21
22 const pageCount = await inDoc.getPageCount();
23 const pagesToSplit = Math.min(4, pageCount);
24
25 // docStoreArray is used to leep track of the documents we have split up for later use.
26 docStoreArray = [];
27 for (let i = 1; i <= pagesToSplit; ++i) {
28 const newDoc = await PDFNet.PDFDoc.create();
29 const filename = 'newsletter_split_page_' + i + '.pdf';
30 newDoc.insertPages(0, inDoc, i, i, PDFNet.PDFDoc.InsertFlag.e_none);
31 docStoreArray[i - 1] = newDoc;
32 const docbuf = await newDoc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
33 saveBufferAsPDFDoc(docbuf, filename);
34 console.log('Result saved as ' + filename);
35 }
36 } catch (err) {
37 console.log(err.stack);
38 }
39
40 try {
41 // start stack-based deallocation with startDeallocateStack. Later on when endDeallocateStack is called,
42 // all objects in memory that were initialized since the most recent startDeallocateStack call will be
43 // cleaned up. Doing this makes sure that memory growth does not get too high.
44 await PDFNet.startDeallocateStack();
45 const newDoc = await PDFNet.PDFDoc.create();
46 newDoc.initSecurityHandler();
47 newDoc.lock();
48
49 console.log('Sample 2, merge several PDF documents into one:');
50
51 for (let i = 1; i <= docStoreArray.length; ++i) {
52 const currDoc = docStoreArray[i - 1];
53 const currDocPageCount = await currDoc.getPageCount();
54 newDoc.insertPages(i, currDoc, 1, currDocPageCount, PDFNet.PDFDoc.InsertFlag.e_none);
55 }
56 const docbuf = await newDoc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
57 saveBufferAsPDFDoc(docbuf, 'newsletter_merged.pdf');
58 await PDFNet.endDeallocateStack();
59 } catch (err) {
60 console.log(err.stack);
61 }
62
63 try {
64 await PDFNet.startDeallocateStack();
65 console.log('Sample 3, delete every second page');
66 const inDoc = await PDFNet.PDFDoc.createFromURL(inputPath + 'newsletter.pdf');
67
68 inDoc.initSecurityHandler();
69 inDoc.lock();
70
71 let pageNum = await inDoc.getPageCount();
72
73 while (pageNum >= 1) {
74 const itr = await inDoc.getPageIterator(pageNum);
75 inDoc.pageRemove(itr);
76 pageNum -= 2;
77 }
78
79 const docbuf = await inDoc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
80 saveBufferAsPDFDoc(docbuf, 'newsletter_page_removed.pdf');
81 await PDFNet.endDeallocateStack();
82 } catch (err) {
83 console.log(err);
84 }
85
86 try {
87 await PDFNet.startDeallocateStack();
88 console.log('Sample 4, Insert a page at different locations');
89 const in1Doc = await PDFNet.PDFDoc.createFromURL(inputPath + 'newsletter.pdf');
90 in1Doc.initSecurityHandler();
91 in1Doc.lock();
92
93 const in2Doc = await PDFNet.PDFDoc.createFromURL(inputPath + 'fish.pdf');
94 in2Doc.initSecurityHandler();
95 in2Doc.lock();
96
97 const srcPage = await in2Doc.getPageIterator(1);
98 const dstPage = await in1Doc.getPageIterator(1);
99 let pageNum = 1;
100 while (await dstPage.hasNext()) {
101 if (pageNum++ % 3 === 0) {
102 in1Doc.pageInsert(dstPage, await srcPage.current());
103 }
104 dstPage.next();
105 }
106
107 const docbuf = await in1Doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
108 saveBufferAsPDFDoc(docbuf, 'newsletter_page_insert.pdf');
109 console.log('done');
110 await PDFNet.endDeallocateStack();
111 } catch (err) {
112 console.log(err.stack);
113 }
114
115 try {
116 await PDFNet.startDeallocateStack();
117 console.log('Sample 5, replicate pages within a single document');
118 const doc = await PDFNet.PDFDoc.createFromURL(inputPath + 'newsletter.pdf');
119 doc.initSecurityHandler();
120
121 // Replicate the cover page three times (copy page #1 and place it before the
122 // seventh page in the document page sequence)
123 const cover = await doc.getPage(1);
124 const p7 = await doc.getPageIterator(7);
125 doc.pageInsert(p7, cover);
126 doc.pageInsert(p7, cover);
127 doc.pageInsert(p7, cover);
128 // replicate cover page two more times by placing it before and after existing pages
129 doc.pagePushFront(cover);
130 doc.pagePushBack(cover);
131
132 const docbuf = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
133 saveBufferAsPDFDoc(docbuf, 'newsletter_page_clone.pdf');
134 console.log('done saving newsletter_page_clone.pdf');
135 await PDFNet.endDeallocateStack();
136 } catch (err) {
137 console.log(err.stack);
138 }
139 };
140
141 // add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY')
142 PDFNet.runWithCleanup(main);
143 };
144})(window);
145// eslint-disable-next-line spaced-comment
146//# sourceURL=PDFPageTest.js
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales