PDFPage

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. Learn more about our Web SDK and PDF Editing & Manipulation Library.

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

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales