-
Notifications
You must be signed in to change notification settings - Fork 0
/
addbar.py
81 lines (65 loc) · 2.46 KB
/
addbar.py
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
79
80
81
#######################################################################
# Copyright 2013 Junghoon Kim
# jfkimberly@skku.edu
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#######################################################################
from pyPdf import PdfFileWriter, PdfFileReader
# run from the command line
name = raw_input("Enter name of file (excluding '.pdf')\n")
namepdf = name + ".pdf"
# number of output channels
while True:
try:
channels = int(
raw_input("How many output channels (usually 1 or 2)?\n"))
except ValueError:
print("Try 1 or 2")
else:
if 1 <= channels <= 2:
break
else:
print("Try 1 or 2")
# output filename
outputfile = raw_input(
"Output filename without '.pdf' (default: '" + name + "-addbar.pdf')?\n")
infoot = PdfFileReader(file(namepdf, "rb"))
outfoot = PdfFileWriter()
numPages = infoot.getNumPages()
print "document has %s pages." % numPages
if channels == 1:
watermark = PdfFileReader(file("bar.pdf", "rb"))
if channels == 2:
watermark = PdfFileReader(file("bar2.pdf", "rb"))
for pagenum in range(numPages):
page = infoot.getPage(pagenum)
page.mergePage(watermark.getPage(0))
# print page.mediaBox.getLowerRight_x(), page.mediaBox.getLowerRight_y()
if channels == 1:
page.trimBox.upperLeft = (62, 38)
page.trimBox.lowerRight = (458, 434)
page.cropBox.upperLeft = (62, 38)
page.cropBox.lowerRight = (458, 434)
elif channels == 2:
page.trimBox.upperLeft = (43.5, 0)
page.trimBox.lowerRight = (440, 793.5)
page.cropBox.upperLeft = (43.5, 0)
page.cropBox.lowerRight = (440, 793.5)
outfoot.addPage(page)
if outputfile == '':
outfootStream = file(name + "-addbar.pdf", "wb")
else:
outfootStream = file(outputfile + ".pdf", "wb")
outfoot.write(outfootStream)
outfootStream.close()