-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwwc_paginatepdf.py
151 lines (126 loc) · 4.92 KB
/
wwc_paginatepdf.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import fitz
from wwc_TOC import isTOCPage
from utilities import annot_name
suffix="pages"
def get_colour(choice):
# returns colour code for selection
red = (1, 0, 0)
orange = (1, 0.65, 0)
yellow = (1, 1, 0.2)
green = (0, 1, 0)
blue = (0, 0, 1)
indigo = (0.29, 0, 0.51)
violet = (0.54, 0.17, 0.89)
black = (0, 0, 0)
colour = red # default
if choice == 'Red':
colour = red
if choice == 'Orange':
colour = orange
if choice == 'Yellow':
colour = yellow
if choice == 'Green':
colour = green
if choice == 'Blue':
colour = blue
if choice == 'Indigo':
colour = indigo
if choice == 'Violet':
colour = violet
if choice == 'Black':
colour = black
return colour
def paginate(doc, options, display=None, addHistory=True):
fitz.TOOLS.set_annot_stem(annot_name+suffix)
remove_pagination(doc, options, display, addHistory=False)
if display:
display.updatestatusBar("Adding pagination...")
if options['pgRange']:
pgRange=options['pgRange']
else:
pgRange=doc[0].get_label() + "-" + doc[doc.page_count-1].get_label()
fontn = 'helv'
if options['fontName'] == "Helvetica": fontn = 'helv'
if options['fontName'] == 'Times Roman': font = 'TiRo'
if options['fontName'] == "Courier": font = 'Cour'
count = 0
a=doc.parse_page_string(pgRange)
if a:
total = len(a)
for pgNo in a:
page=doc[pgNo]
r = page.rect
txt = page.get_label()
if not isTOCPage(page): # skip TOC pages
width = fitz.get_text_length(txt, fontname="helv", fontsize=options['Size'])
height = options['Size']
top = r[1] + options['vMargin']*72
bottom = r[3] - options['vMargin']*72
left = r[0] + options['hMargin']*72
right = r[2] - options['hMargin']*72
hmiddle = left + (right - left) / 2
vmiddle = top + (bottom-top)/2
if options['VPOS'] == 'B': # bottom
r[1] = bottom - height
r[3] = bottom
if options['VPOS'] == 'M': # middle
r[1] = vmiddle - height/2
r[3] = vmiddle + height/2
if options['VPOS'] == 'T': # top
r[1] = top
r[3] = top + height
if options['HPOS'] == 'L': # left
r[0] = left
r[2] = left + width
if options['HPOS'] == 'C': # centre
r[0] = hmiddle - (width / 2)
r[2] = hmiddle + (width / 2)
if options['HPOS'] == 'R': # right
r[0] = right - width
r[2] = right
annot = page.add_freetext_annot(rect=r * page.derotation_matrix, text=txt, fontname=fontn, fontsize=options['Size'],
fill_color=None)
annot.update(text_color=get_colour(options['Colour']), fill_color=None, rotate=page.rotation - 360)
if display: display.dlist_tab[pgNo]=None
count += 1
percentComplete = int((float(count) / float(total)) * 100)
if percentComplete % 10 == 0:
if display: display.updateprogressBar(percentComplete)
if display:
display.updatestatusBar('Added pagination.')
if addHistory: display.adddocHistory({'code':"DOC_paginated", 'options': options})
def isPaginated(doc):
for page in doc:
for annot in page.annots():
info = annot.info
id = info['id']
if id.find(annot_name + suffix) > -1:
return True
return False
def remove_pagination(doc, options, display=None, addHistory=True):
# delete annotations with names starting annot+suffix
if not isPaginated(doc): return
if display: display.updatestatusBar('Removing pagination...')
if options['pgRange']:
pgRange = options['pgRange']
else:
pgRange = doc[0].get_label() + "-" + doc[doc.page_count - 1].get_label()
a = doc.parse_page_string(pgRange)
count = 0
total=len(a)
if a:
for pgNo in a:
page=doc[pgNo]
for annot in page.annots():
info = annot.info
id = info['id']
if id.find(annot_name + suffix) > -1:
page.delete_annot(annot)
if display: display.dlist_tab[pgNo]=None
count += 1
percentComplete = int((float(count) / float(total)) * 100)
if percentComplete % 10 == 0:
if display: display.updateprogressBar(percentComplete)
if display:
display.updatestatusBar('Removed pagination.')
if addHistory: display.adddocHistory({'code':"DOC_pagination_removed", 'options': options})