1 | #include <QtCore/QDebug> |
2 | #include <QtWidgets/QApplication> |
3 | #include <QtGui/QImage> |
4 | #include <QtGui/QPainter> |
5 | #include <QtGui/QPaintEvent> |
6 | #include <QtWidgets/QWidget> |
7 | |
8 | #include <poppler-qt6.h> |
9 | |
10 | class PDFDisplay : public QWidget // picture display widget |
11 | { |
12 | Q_OBJECT |
13 | public: |
14 | explicit PDFDisplay(std::unique_ptr<Poppler::Document> &&d, QWidget *parent = nullptr); |
15 | ~PDFDisplay() override; |
16 | |
17 | protected: |
18 | void paintEvent(QPaintEvent *) override; |
19 | void keyPressEvent(QKeyEvent *) override; |
20 | |
21 | private: |
22 | void display(); |
23 | int m_currentPage; |
24 | QImage image; |
25 | std::unique_ptr<Poppler::Document> doc; |
26 | }; |
27 | |
28 | PDFDisplay::PDFDisplay(std::unique_ptr<Poppler::Document> &&d, QWidget *parent) : QWidget(parent) |
29 | { |
30 | doc = std::move(d); |
31 | m_currentPage = 0; |
32 | display(); |
33 | } |
34 | |
35 | void PDFDisplay::display() |
36 | { |
37 | if (doc) { |
38 | std::unique_ptr<Poppler::Page> page = doc->page(index: m_currentPage); |
39 | if (page) { |
40 | qDebug() << "Displaying page: " << m_currentPage; |
41 | image = page->renderToImage(); |
42 | update(); |
43 | } |
44 | } else { |
45 | qWarning() << "doc not loaded" ; |
46 | } |
47 | } |
48 | |
49 | PDFDisplay::~PDFDisplay() { } |
50 | |
51 | void PDFDisplay::paintEvent(QPaintEvent *e) |
52 | { |
53 | QPainter paint(this); // paint widget |
54 | if (!image.isNull()) { |
55 | paint.drawImage(x: 0, y: 0, image); |
56 | } else { |
57 | qWarning() << "null image" ; |
58 | } |
59 | } |
60 | |
61 | void PDFDisplay::keyPressEvent(QKeyEvent *e) |
62 | { |
63 | if (e->key() == Qt::Key_Down) { |
64 | if (m_currentPage + 1 < doc->numPages()) { |
65 | m_currentPage++; |
66 | display(); |
67 | } |
68 | } else if (e->key() == Qt::Key_Up) { |
69 | if (m_currentPage > 0) { |
70 | m_currentPage--; |
71 | display(); |
72 | } |
73 | } else if (e->key() == Qt::Key_Q) { |
74 | exit(status: 0); |
75 | } |
76 | } |
77 | |
78 | int main(int argc, char **argv) |
79 | { |
80 | QApplication a(argc, argv); // QApplication required! |
81 | |
82 | if (argc != 3) { |
83 | qWarning() << "usage: test-password-qt6 owner-password filename" ; |
84 | exit(status: 1); |
85 | } |
86 | |
87 | std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(filePath: argv[2], ownerPassword: argv[1]); |
88 | if (!doc) { |
89 | qWarning() << "doc not loaded" ; |
90 | exit(status: 1); |
91 | } |
92 | |
93 | // output some meta-data |
94 | auto pdfVersion = doc->getPdfVersion(); |
95 | qDebug() << " PDF Version: " << qPrintable(QStringLiteral("%1.%2" ).arg(pdfVersion.major).arg(pdfVersion.minor)); |
96 | qDebug() << " Title: " << doc->info(QStringLiteral("Title" )); |
97 | qDebug() << " Subject: " << doc->info(QStringLiteral("Subject" )); |
98 | qDebug() << " Author: " << doc->info(QStringLiteral("Author" )); |
99 | qDebug() << " Key words: " << doc->info(QStringLiteral("Keywords" )); |
100 | qDebug() << " Creator: " << doc->info(QStringLiteral("Creator" )); |
101 | qDebug() << " Producer: " << doc->info(QStringLiteral("Producer" )); |
102 | qDebug() << " Date created: " << doc->date(QStringLiteral("CreationDate" )).toString(); |
103 | qDebug() << " Date modified: " << doc->date(QStringLiteral("ModDate" )).toString(); |
104 | qDebug() << "Number of pages: " << doc->numPages(); |
105 | qDebug() << " Linearised: " << doc->isLinearized(); |
106 | qDebug() << " Encrypted: " << doc->isEncrypted(); |
107 | qDebug() << " OK to print: " << doc->okToPrint(); |
108 | qDebug() << " OK to copy: " << doc->okToCopy(); |
109 | qDebug() << " OK to change: " << doc->okToChange(); |
110 | qDebug() << "OK to add notes: " << doc->okToAddNotes(); |
111 | qDebug() << " Page mode: " << doc->pageMode(); |
112 | QStringList fontNameList; |
113 | foreach (const Poppler::FontInfo &font, doc->fonts()) |
114 | fontNameList += font.name(); |
115 | qDebug() << " Fonts: " << fontNameList.join(QStringLiteral(", " )); |
116 | |
117 | std::unique_ptr<Poppler::Page> page = doc->page(index: 0); |
118 | qDebug() << " Page 1 size: " << page->pageSize().width() / 72 << "inches x " << page->pageSize().height() / 72 << "inches" ; |
119 | |
120 | PDFDisplay test(std::move(doc)); // create picture display |
121 | test.setWindowTitle(QStringLiteral("Poppler-Qt6 Test" )); |
122 | test.show(); // show it |
123 | |
124 | return a.exec(); // start event loop |
125 | } |
126 | |
127 | #include "test-password-qt6.moc" |
128 | |