1 | #include <QtCore/QDebug> |
---|---|
2 | #include <QtCore/QFile> |
3 | #include <QGuiApplication> |
4 | #include <QImage> |
5 | |
6 | #include <poppler-qt6.h> |
7 | |
8 | int main(int argc, char **argv) |
9 | { |
10 | QGuiApplication a(argc, argv); // QApplication required! |
11 | |
12 | if (argc < 2 || (argc == 3 && strcmp(s1: argv[2], s2: "-qpainter") != 0) || argc > 3) { |
13 | // use argument as file name |
14 | qWarning() << "usage: test-render-to-file-qt6 filename [-qpainter]"; |
15 | exit(status: 1); |
16 | } |
17 | |
18 | std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(filePath: QFile::decodeName(localFileName: argv[1])); |
19 | if (!doc) { |
20 | qWarning() << "doc not loaded"; |
21 | exit(status: 1); |
22 | } |
23 | |
24 | if (doc->isLocked()) { |
25 | qWarning() << "document locked (needs password)"; |
26 | exit(status: 0); |
27 | } |
28 | |
29 | if (doc->numPages() <= 0) { |
30 | qDebug() << "Doc has no pages"; |
31 | return 0; |
32 | } |
33 | |
34 | QString backendString; |
35 | if (argc == 3 && strcmp(s1: argv[2], s2: "-qpainter") == 0) { |
36 | backendString = QStringLiteral("QPainter"); |
37 | doc->setRenderBackend(Poppler::Document::QPainterBackend); |
38 | } else { |
39 | backendString = QStringLiteral("Splash"); |
40 | doc->setRenderBackend(Poppler::Document::SplashBackend); |
41 | } |
42 | doc->setRenderHint(hint: Poppler::Document::Antialiasing, on: true); |
43 | doc->setRenderHint(hint: Poppler::Document::TextAntialiasing, on: true); |
44 | |
45 | for (int i = 0; i < doc->numPages(); ++i) { |
46 | std::unique_ptr<Poppler::Page> page = doc->page(index: i); |
47 | if (page) { |
48 | qDebug() << "Rendering page using"<< backendString << "backend: "<< i; |
49 | QTime t = QTime::currentTime(); |
50 | QImage image = page->renderToImage(); |
51 | qDebug() << "Rendering took"<< t.msecsTo(t: QTime::currentTime()) << "msecs"; |
52 | image.save(QStringLiteral("test-render-to-file%1.png").arg(a: i)); |
53 | } |
54 | } |
55 | |
56 | return 0; |
57 | } |
58 |