forked from dscho/fiji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf-reverse-pages.bsh
executable file
·52 lines (38 loc) · 1.26 KB
/
pdf-reverse-pages.bsh
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
#!/bin/sh
// 2>&1 | :; exec "$(dirname "$0")"/ImageJ.sh "$0" "$@" # exec with fiji
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.SimpleBookmark;
import java.io.FileOutputStream;
import java.util.ArrayList;
if (bsh.args.length != 2) {
System.err.println("Usage: " + this.interpreter.getSourceFileInfo()
+ " <input> <output>");
System.exit(1);
}
copy = null;
all_bookmarks = new ArrayList();
page_offset = 0;
reader = new PdfReader(bsh.args[0]);
reader.consolidateNamedDestinations();
bookmarks = SimpleBookmark.getBookmark(reader);
if (bookmarks != null) {
if (page_offset != 0)
SimpleBookmark.shiftPageNumbers(bookmarks, page_offset, null);
all_bookmarks.add(bookmarks);
}
page_count = reader.getNumberOfPages();
page_offset += page_offset;
document = new Document(reader.getPageSizeWithRotation(1));
output = new FileOutputStream(bsh.args[1]);
copy = new PdfCopy(document, output);
document.open();
print("Reversing " + page_count + " pages");
for (k = page_count; k > 0; k--)
copy.addPage(copy.getImportedPage(reader, k));
if (reader.getAcroForm() != null)
copy.copyAcroForm(reader);
if (!all_bookmarks.isEmpty())
copy.setOutlines(all_bookmarks);
document.close();