-
Notifications
You must be signed in to change notification settings - Fork 4
/
mergeOddEvenPdf.js
78 lines (64 loc) · 1.92 KB
/
mergeOddEvenPdf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Created by Nathaniel Young (nyoungstudios on GitHub)
// this script creates a new pdf by merging the even and odd pages
// in other words, creates new pdf by inserting alternating pages from each source
// must open the odd pages pdf before the even pages pdf
// anonymous wrapper function
(function() {
// gets the active documents with the first one being the odd and second one being the even document
var activeDocs = app.activeDocs;
if (activeDocs.length < 2) {
app.alert('Need to open two PDFs.');
return;
}
var oddDoc = activeDocs[0];
var evenDoc = activeDocs[1];
// gets the page length of the documents
var lenOddDoc = oddDoc.numPages;
var lenEvenDoc = evenDoc.numPages;
// Create new PDF document
var newDoc = app.newDoc();
// index counters
index = 0;
oddIndex = 0;
evenIndex = 0;
// inserts the pages alternating from each pdf source
while (oddIndex != lenOddDoc && evenIndex != lenEvenDoc) {
if (index % 2 == 0) {
newDoc.insertPages({
nPage: index,
cPath: oddDoc.path,
nStart: oddIndex,
nEnd: oddIndex
});
oddIndex++;
} else {
newDoc.insertPages({
nPage: index,
cPath: evenDoc.path,
nStart: evenIndex,
nEnd: evenIndex
});
evenIndex++;
}
// console.println('index: ' + index + ' odd: ' + oddIndex + ' even: ' + evenIndex);
index++;
}
// appends the leftover pages if pdf lengths are not equal
if (oddIndex != lenOddDoc) {
newDoc.insertPages({
nPage: index,
cPath: oddDoc.path,
nStart: oddIndex,
nEnd: lenOddDoc - 1
});
} else if (evenIndex != lenEvenDoc) {
newDoc.insertPages({
nPage: index,
cPath: evenDoc.path,
nStart: evenIndex,
nEnd: lenEvenDoc - 1
});
}
// deletes initial blank page
newDoc.deletePages();
})();