-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
172 lines (139 loc) · 5.5 KB
/
main.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
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
*
* Copyright: 2012-2013 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 <QTextStream>
#include <QDebug>
#include <QCoreApplication>
#include <iostream>
#include "pdfwriter.h"
#include "pdfprocessor.h"
#include "pdfobject.h"
void writeCatalog(PDF::Writer *writer, const QVector<PdfPageInfo> &pages)
{
// Catalog object ...........................
PDF::Object catalog;
catalog.setObjNum(1);
catalog.dict().insert("Type", PDF::Name("Catalog"));
catalog.dict().insert("Pages", PDF::Link(catalog.objNum() + 1));
writer->writeObject(catalog);
// ..........................................
// Pages object .............................
//if (!std::getenv("BOOMAGAMERGER_DEBUGPAGES"))
//{
// PDF::Object pagesObj(2);
// pagesObj.dict().insert("Type", PDF::Name("Pages"));
// pagesObj.dict().insert("Count", PDF::Number(0));
// pagesObj.dict().insert("Kids", PDF::Array());
// writer->writeObject(pagesObj);
// }
// else
{
PDF::Object pagesObj(2);
pagesObj.dict().insert("Type", PDF::Name("Pages"));
PDF::ObjNum pageNum = writer->xRefTable().maxObjNum() + 1;
PDF::Array kids;
for (int i=0; i< pages.count(); ++i)
{
PdfPageInfo pi = pages.at(i);
PDF::Object page( pageNum + i * 3 + 1);
PDF::Object xobj( pageNum + i * 3 + 2);
PDF::Object content(pageNum + i * 3 + 3);
kids.append(PDF::Link(page.objNum()));
{
PDF::Dict dict;
dict.insert("Type", PDF::Name("Page"));
dict.insert("Parent", PDF::Link(pagesObj.objNum(), 0));
dict.insert("Resources", PDF::Link(xobj.objNum()));
PDF::Array mediaBox;
mediaBox.append(PDF::Number(pi.mediaBox.left()));
mediaBox.append(PDF::Number(pi.mediaBox.top()));
mediaBox.append(PDF::Number(pi.mediaBox.width()));
mediaBox.append(PDF::Number(pi.mediaBox.height()));
dict.insert("MediaBox", mediaBox);
PDF::Array cropBox;
cropBox.append(PDF::Number(pi.mediaBox.left()));
cropBox.append(PDF::Number(pi.mediaBox.top()));
cropBox.append(PDF::Number(pi.mediaBox.width()));
cropBox.append(PDF::Number(pi.mediaBox.height()));
dict.insert("CropBox", cropBox);
dict.insert("Rotate", PDF::Number(pi.rotate));
dict.insert("Contents", PDF::Link(content.objNum()));
page.setValue(dict);
writer->writeObject(page);
}
{
xobj.dict().insert("ProcSet", PDF::Array() << PDF::Name("PDF"));
xobj.dict().insert("XObject", PDF::Dict());
PDF::Dict dict;
for (int c=0; c<pi.xObjNums.count(); ++c)
{
dict.insert(QString("Im0_%1").arg(c),
PDF::Link(pi.xObjNums.at(c)));
}
xobj.dict().insert("XObject", dict);
writer->writeObject(xobj);
}
{
QString stream;
for (int c=0; c<pi.xObjNums.count(); ++c)
stream += QString("/Im0_%1 Do ").arg(c);
content.setStream(stream.toLatin1());
content.dict().insert("Length", PDF::Number(content.stream().length()));
writer->writeObject(content);
}
}
pagesObj.dict().insert("Count", PDF::Number(kids.count()));
pagesObj.dict().insert("Kids", kids);
writer->writeObject(pagesObj);
}
// ..........................................
writer->writeXrefTable();
writer->writeTrailer(PDF::Link(catalog.objNum()));
}
/************************************************
************************************************/
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cerr << "Usage:\n\t pdfparser INPUT_PDF OUTPUT_PDF" << std::endl;
return 1;
}
QFile file(argv[2]);
if (! file.open(QFile::WriteOnly | QFile::Truncate))
{
std::cerr << "I can't write to file " << argv[2] << file.errorString().toLocal8Bit().data() << std::endl;
return 2;
}
PDF::Writer writer(&file);
writer.writePDFHeader(1,7);
QVector<PdfPageInfo> pages;
PdfProcessor *proc = new PdfProcessor(argv[1]);
proc->open();
proc->run(&writer, writer.xRefTable().maxObjNum() + 3);
pages << proc->pageInfo();
delete proc;
writeCatalog(&writer, pages);
file.close();
return 0;
}