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
|
#include <QtCore/QDebug>
#include <QtCore/QFile>
#include <QtGui/QApplication>
#include <QtGui/QImage>
#include <QtGui/QLabel>
#include <QtGui/QMouseEvent>
#include <QtGui/QPainter>
#include <QtGui/QPaintEvent>
#include <QtGui/QToolTip>
#include <QtGui/QWidget>
#include <poppler-qt4.h>
class PDFDisplay : public QWidget // picture display widget
{
public:
PDFDisplay( Poppler::Document *d, bool arthur );
~PDFDisplay();
void setShowTextRects(bool show);
void display();
protected:
void paintEvent( QPaintEvent * );
void keyPressEvent( QKeyEvent * );
void mousePressEvent( QMouseEvent * );
private:
int m_currentPage;
QImage image;
Poppler::Document *doc;
QString backendString;
bool showTextRects;
QList<Poppler::TextBox*> textRects;
};
PDFDisplay::PDFDisplay( Poppler::Document *d, bool arthur )
{
showTextRects = false;
doc = d;
m_currentPage = 0;
if (arthur)
{
backendString = "Arthur";
doc->setRenderBackend(Poppler::Document::ArthurBackend);
}
else
{
backendString = "Splash";
doc->setRenderBackend(Poppler::Document::SplashBackend);
}
doc->setRenderHint(Poppler::Document::Antialiasing, true);
doc->setRenderHint(Poppler::Document::TextAntialiasing, true);
}
void PDFDisplay::setShowTextRects(bool show)
{
showTextRects = show;
}
void PDFDisplay::display()
{
if (doc) {
Poppler::Page *page = doc->page(m_currentPage);
if (page) {
qDebug() << "Displaying page using" << backendString << "backend: " << m_currentPage;
QTime t = QTime::currentTime();
image = page->renderToImage();
qDebug() << "Rendering took" << t.msecsTo(QTime::currentTime()) << "msecs";
qDeleteAll(textRects);
if (showTextRects)
{
QPainter painter(&image);
painter.setPen(Qt::red);
textRects = page->textList();
foreach(Poppler::TextBox *tb, textRects)
{
painter.drawRect(tb->boundingBox());
}
}
else textRects.clear();
update();
delete page;
}
} else {
qWarning() << "doc not loaded";
}
}
PDFDisplay::~PDFDisplay()
{
qDeleteAll(textRects);
delete doc;
}
void PDFDisplay::paintEvent( QPaintEvent *e )
{
QPainter paint( this ); // paint widget
if (!image.isNull()) {
paint.drawImage(0, 0, image);
} else {
qWarning() << "null image";
}
}
void PDFDisplay::keyPressEvent( QKeyEvent *e )
{
if (e->key() == Qt::Key_Down)
{
if (m_currentPage + 1 < doc->numPages())
{
m_currentPage++;
display();
}
}
else if (e->key() == Qt::Key_Up)
{
if (m_currentPage > 0)
{
m_currentPage--;
display();
}
}
else if (e->key() == Qt::Key_Q)
{
exit(0);
}
}
void PDFDisplay::mousePressEvent( QMouseEvent *e )
{
int i = 0;
foreach(Poppler::TextBox *tb, textRects)
{
if (tb->boundingBox().contains(e->pos()))
{
QString tt = QString("Text: \"%1\"\nIndex in text list: %2").arg(tb->text()).arg(i);
QToolTip::showText(e->globalPos(), tt, this);
break;
}
++i;
}
}
int main( int argc, char **argv )
{
QApplication a( argc, argv ); // QApplication required!
if ( argc < 2 ||
(argc == 3 && strcmp(argv[2], "-extract") != 0 && strcmp(argv[2], "-arthur") != 0 && strcmp(argv[2], "-textRects") != 0) ||
argc > 3)
{
// use argument as file name
qWarning() << "usage: test-poppler-qt filename [-extract|-arthur|-textRects]";
exit(1);
}
Poppler::Document *doc = Poppler::Document::load(QFile::decodeName(argv[1]));
if (!doc)
{
qWarning() << "doc not loaded";
exit(1);
}
if (doc->isLocked())
{
qWarning() << "document locked (needs password)";
exit(0);
}
// output some meta-data
qDebug() << " PDF Version: " << doc->pdfVersion();
qDebug() << " Title: " << doc->info("Title");
qDebug() << " Subject: " << doc->info("Subject");
qDebug() << " Author: " << doc->info("Author");
qDebug() << " Key words: " << doc->info("Keywords");
qDebug() << " Creator: " << doc->info("Creator");
qDebug() << " Producer: " << doc->info("Producer");
qDebug() << " Date created: " << doc->date("CreationDate").toString();
qDebug() << " Date modified: " << doc->date("ModDate").toString();
qDebug() << "Number of pages: " << doc->numPages();
qDebug() << " Linearised: " << doc->isLinearized();
qDebug() << " Encrypted: " << doc->isEncrypted();
qDebug() << " OK to print: " << doc->okToPrint();
qDebug() << " OK to copy: " << doc->okToCopy();
qDebug() << " OK to change: " << doc->okToChange();
qDebug() << "OK to add notes: " << doc->okToAddNotes();
qDebug() << " Page mode: " << doc->pageMode();
qDebug() << " Metadata: " << doc->metadata();
QStringList fontNameList;
foreach( const Poppler::FontInfo &font, doc->fonts() )
fontNameList += font.name();
qDebug() << " Fonts: " << fontNameList.join( ", " );
if ( doc->hasEmbeddedFiles() ) {
qDebug() << "Embedded files:";
foreach( Poppler::EmbeddedFile *file, doc->embeddedFiles() ) {
qDebug() << " " << file->name();
}
qDebug();
} else {
qDebug() << "No embedded files";
}
if (doc->numPages() <= 0)
{
delete doc;
qDebug() << "Doc has no pages";
return 0;
}
Poppler::Page *page = doc->page(0);
qDebug() << "Page 1 size: " << page->pageSize().width()/72 << "inches x " << page->pageSize().height()/72 << "inches";
delete page;
if (argc == 2 || (argc == 3 && strcmp(argv[2], "-arthur") == 0) || (argc == 3 && strcmp(argv[2], "-textRects") == 0))
{
bool useArthur = (argc == 3 && strcmp(argv[2], "-arthur") == 0);
PDFDisplay test( doc, useArthur ); // create picture display
test.setWindowTitle("Poppler-Qt4 Test");
test.setShowTextRects(argc == 3 && strcmp(argv[2], "-textRects") == 0);
test.display();
test.show(); // show it
return a.exec(); // start event loop
}
else
{
Poppler::Page *page = doc->page(0);
QLabel *l = new QLabel(page->text(QRectF()), 0);
l->show();
delete page;
delete doc;
return a.exec();
}
}
|