-
Notifications
You must be signed in to change notification settings - Fork 0
/
duplicatepage.groovy
53 lines (41 loc) · 1.37 KB
/
duplicatepage.groovy
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
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfReader;
public class Concatenate {
/** The resulting PDF file. */
/**
* Main method.
* @param args no arguments needed
* @throws DocumentException
* @throws IOException
*/
public static void main(String[] args)
throws IOException, DocumentException {
if (args.length < 2) {
println("USAGE duplicatepages outputfile inputfile1 [inputfile2 ...]")
System.exit 1
}
String outputfilepath = args[0]
List<String> files = args[1..-1]
Document document = new Document();
PdfCopy newpdf = new PdfCopy(document, new FileOutputStream(outputfilepath));
document.open();
PdfReader reader;
int n;
files.each{ file ->
reader = new PdfReader(file);
n = reader.getNumberOfPages();
for (int page = 0; page < n; ) {
page++
newpdf.addPage(newpdf.getImportedPage(reader, page));
newpdf.addPage(newpdf.getImportedPage(reader, page));
}
newpdf.freeReader(reader);
reader.close();
}
document.close();
}
}