-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfprocessor.cpp
371 lines (308 loc) · 12.1 KB
/
pdfprocessor.cpp
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
*
* Copyright: 2012-2017 Boomaga team https://github.com/Boomaga
* Authors:
* Alexander Sokoloff <[email protected]>
*
* This program or library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */
#include "assert.h"
#include "pdfprocessor.h"
#include <QFile>
#include "pdfobject.h"
#include "pdfvalue.h"
#include <pdfreader.h>
#include <pdfwriter.h>
//#define SAVE_DEBUG_INFO
/************************************************
*
************************************************/
PdfProcessor::PdfProcessor(const QString &fileName, qint64 startPos, qint64 endPos):
QObject(),
mFileName(fileName),
mStartPos(startPos),
mEndPos(endPos),
mObjNumOffset(0),
mWriter(nullptr)
{
}
/************************************************
*
************************************************/
PdfProcessor::~PdfProcessor()
{
}
/************************************************
*
************************************************/
void PdfProcessor::open()
{
mReader.open(mFileName, mStartPos, mEndPos);
}
/************************************************
*
************************************************/
quint32 PdfProcessor::pageCount()
{
return mReader.pageCount();
}
/************************************************
*
************************************************/
void PdfProcessor::run(PDF::Writer *writer, quint32 objNumOffset)
{
mWriter = writer;
mObjNumOffset = objNumOffset;
PDF::Object catalog = mReader.getObject(mReader.trailerDict().value("Root").asLink());
PDF::Object pages = mReader.getObject(catalog.dict().value("Pages").asLink());
mPageInfo.reserve(pages.dict().value("Count").asNumber().value());
PDF::Dict dict;
walkPageTree(0, pages, dict);
writer = nullptr;
}
/************************************************
*
************************************************/
void fillPageInfo(PdfPageInfo *pageInfo, const PDF::Dict &pageDict, const PDF::Dict &inherited)
{
const PDF::Array &mediaBox = pageDict.value("MediaBox", inherited.value("MediaBox")).asArray();
if (mediaBox.count() != 4)
throw QString("Incorrect MediaBox rectangle");
pageInfo->mediaBox = QRectF(mediaBox.at(0).asNumber().value(),
mediaBox.at(1).asNumber().value(),
mediaBox.at(2).asNumber().value() - mediaBox.at(0).asNumber().value(),
mediaBox.at(3).asNumber().value() - mediaBox.at(1).asNumber().value());
const PDF::Array &cropBox = pageDict.value("CropBox", inherited.value("CropBox")).asArray();
if (cropBox.isValid())
{
if (cropBox.count() != 4)
throw QString("Incorrect CropBox rectangle");
pageInfo->cropBox = QRectF(cropBox.at(0).asNumber().value(),
cropBox.at(1).asNumber().value(),
cropBox.at(2).asNumber().value() - cropBox.at(0).asNumber().value(),
cropBox.at(3).asNumber().value() - cropBox.at(1).asNumber().value());
}
else
{
pageInfo->cropBox = pageInfo->mediaBox;
}
pageInfo->rotate = pageDict.value("Rotate", inherited.value("Rotate")).asNumber().value();
}
/************************************************
*
************************************************/
int PdfProcessor::walkPageTree(int pageNum, const PDF::Object &page, const PDF::Dict &inherited)
{
assert(page.type() == "Pages" || page.type() == "Page");
// Type = Pages .......................................
if (page.type() == "Pages")
{
const PDF::Dict &pageDict = page.dict();
PDF::Dict dict = inherited;
if (pageDict.contains("Resources"))
dict.insert("Resources", pageDict.value("Resources"));
if (pageDict.contains("MediaBox"))
dict.insert("MediaBox", pageDict.value("MediaBox"));
if (pageDict.contains("CropBox"))
dict.insert("CropBox", pageDict.value("CropBox"));
if (pageDict.contains("Rotate"))
dict.insert("Rotate", pageDict.value("Rotate"));
const PDF::Array kids = pageDict.value("Kids").asArray();
for (int i=0; i<kids.count(); ++i)
{
pageNum = walkPageTree(pageNum, mReader.getObject(kids.at(i).asLink()), dict);
}
return pageNum;
}
// Type = Page ........................................
if (page.type() == "Page")
{
PdfPageInfo pageInfo;
try
{
fillPageInfo(&pageInfo, page.dict(), inherited);
}
catch (const QString &err)
{
throw QString("Error on page %1 %2: %3").arg(page.objNum()).arg(page.genNum()).arg(err);
}
pageInfo.xObjNums << writePageAsXObject(page, inherited);
mPageInfo << pageInfo;
emit pageReady();
return pageNum + 1;
}
return pageNum;
}
/************************************************
XObject Page Const
----------------------------------------
Type - XObject
Subtype - Form
FormType - 1
BBox CropBox
Matrix -
Resources Resources
Group -
Ref -
Metadata Metadata
PieceInfo PieceInfo
LastModified LastModified
StructParent -
StructParents StructParents
OPI -
OC -
Name -
Page Boundaries
================
MediaBox (required, inheritable)
The media box defines the boundaries of the physical medium on which the
page is to be printed. It may include any extended area surrounding the
finished page for bleed, printing marks, or other such purposes. It may also
include areas close to the edges of the medium that cannot be marked because
of physical limitations of the output device. Content falling outside this
boundary can safely be discarded without affecting the meaning of the PDF file.
CropBox (optional, inheritable, default=MediaBox)
The crop box defines the region to which the contents of the page are to be
clipped (cropped) when displayed or printed. Unlike the other boxes, the crop
box has no defined meaning in terms of physical page geometry or intended
use; it merely imposes clipping on the page contents. However, in the absence
of additional information (such as imposition instructions specified in a JDF or
PJTF job ticket), the crop box determines how the page’s contents are to be po-
sitioned on the output medium. The default value is the page’s media box.
BleedBox (optional, default=CropBox)
The bleed box defines the region to which the contents of the page
should be clipped when output in a production environment. This may include
any extra bleed area needed to accommodate the physical limitations of cut-
ting, folding, and trimming equipment. The actual printed page may include
printing marks that fall outside the bleed box. The default value is the page’s
crop box.
TrimBox (optional, defaul=CropBox)
The trim box (PDF 1.3) defines the intended dimensions of the finished page
after trimming. It may be smaller than the media box to allow for production-
related content, such as printing instructions, cut marks, or color bars.
The default value is the page’s crop box.
ArtBox (optional, default=CropBox)
The art box defines the extent of the page’s meaningful content
(including potential white space) as intended by the page’s creator. The default
value is the page’s crop box.
************************************************/
PDF::ObjNum PdfProcessor::writePageAsXObject(const PDF::Object &page, const PDF::Dict &inherited)
{
const PDF::Dict &pageDict = page.dict();
PDF::Object xObj;
xObj.setObjNum(page.objNum());
xObj.setGenNum(page.genNum());
PDF::Dict &dict = xObj.dict();
dict.insert("Type", PDF::Name("XObject"));
dict.insert("Subtype", PDF::Name("Form"));
dict.insert("FormType", PDF::Number(1));
dict.insert("Resources", pageDict.value("Resources", inherited.value("Resources")));
dict.insert("BBox", pageDict.value("CropBox", inherited.value("CropBox",
pageDict.value("MediaBox", inherited.value("MediaBox")))));
if (pageDict.contains("Metadata")) dict.insert("Metadata", pageDict.value("Metadata"));
if (pageDict.contains("PieceInfo")) dict.insert("PieceInfo", pageDict.value("PieceInfo"));
if (pageDict.contains("LastModified")) dict.insert("LastModified", pageDict.value("LastModified"));
if (pageDict.contains("StructParents")) dict.insert("StructParents", pageDict.value("StructParents"));
PDF::Value v = pageDict.value("Contents");
PDF::Object content;
bool ok;
// Page content is Link .....................
while (v.isLink())
{
const PDF::Link &link = v.asLink(&ok);
if (!ok)
throw QString("Page %1 %2 has incorrect content type.").arg(page.objNum()).arg(page.genNum());
content = mReader.getObject(link);
v = content.value();
}
// Page content is Dict (stream) ............
if (v.isDict())
{
xObj.setStream(content.stream());
if (content.dict().contains("Filter"))
dict.insert("Filter", content.dict().value("Filter"));
else
dict.remove("Filter");
dict.insert("Length", xObj.stream().length());
addOffset(xObj);
return xObj.objNum();
}
// Page content is array ....................
const PDF::Array &arr = v.asArray(&ok);
if (ok)
{
QByteArray stream;
for (int i=0; i<arr.count(); ++i)
{
PDF::Object content = mReader.getObject(arr.at(i).asLink());
stream.append(content.decodedStream());
}
xObj.setStream(stream);
xObj.dict().remove("Filter");
xObj.dict().insert("Length", xObj.stream().length());
addOffset(xObj);
return xObj.objNum();
}
throw QString("Page %1 %2 has incorrect content type.").arg(page.objNum()).arg(page.genNum());
}
/************************************************
*
************************************************/
PDF::Object &PdfProcessor::addOffset(PDF::Object &obj)
{
#ifdef SAVE_DEBUG_INFO
obj.dict().insert("BoomagaFrom", PDF::String(QString("%1 %2 obj").arg(obj.objNum()).arg(obj.genNum())));
#endif
obj.setObjNum(obj.objNum() + mObjNumOffset);
offsetValue(obj.value());
mWriter->writeObject(obj);
return obj;
}
/************************************************
*
************************************************/
void PdfProcessor::offsetValue(PDF::Value &value)
{
if (value.isLink())
{
PDF::Link &link = value.asLink();
if (!mProcessedObjects.contains(link.objNum()))
{
mProcessedObjects << link.objNum();
PDF::Object obj = mReader.getObject(link);
addOffset(obj);
}
link.setObjNum(link.objNum() + mObjNumOffset);
}
if (value.isArray())
{
PDF::Array &arr = value.asArray();
for (int i=0; i<arr.count(); ++i)
{
offsetValue(arr[i]);
}
}
if (value.isDict())
{
PDF::Dict &dict = value.asDict();
foreach (auto &key, dict.keys())
{
offsetValue(dict[key]);
}
}
}