Engauge Digitizer 2
Loading...
Searching...
No Matches
Pdf.cpp
Go to the documentation of this file.
1/******************************************************************************************************
2 * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3 * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4 * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5 ******************************************************************************************************/
6
9#include "Pdf.h"
10#include "poppler-qt6.h"
11#include <QApplication>
12#include <QImage>
13#include <QString>
14
15using namespace Poppler;
16
17const int X_TOP_LEFT = 0, Y_TOP_LEFT = 0;
18const int WIDTH = -1, HEIGHT = -1; // Negative values give full page
19const int FIRST_PAGE_1_BASED = 1;
20
22{
23}
24
25PdfReturn Pdf::load (const QString &fileName,
26 QImage &image,
27 int resolution,
28 ImportCropping importCropping,
29 bool isErrorReportRegressionTest) const
30{
31 Document *document = nullptr;
32
33 ImportCroppingUtilPdf importCroppingUtil;
34 bool cropping = importCroppingUtil.applyImportCropping (isErrorReportRegressionTest,
35 fileName,
36 importCropping,
37 document);
38
39 PdfReturn rtn;
40 QApplication::setOverrideCursor(Qt::BusyCursor); // Since loading can be slow
41 if (cropping) {
42
43 rtn = loadWithCropping (document,
44 image,
45 resolution);
46
47 } else {
48
49 rtn = loadWithoutCropping (fileName,
50 image,
51 resolution);
52
53 }
54 QApplication::restoreOverrideCursor();
55
56 delete document;
57 document = nullptr;
58
59 return rtn;
60}
61
62PdfReturn Pdf::loadWithCropping (Document *document,
63 QImage &image,
64 int resolution) const
65{
66 PdfReturn pdfReturn = PDF_RETURN_FAILED;
67
68 // Get page and extent. At this point it is always true that the image can be read
69 DlgImportCroppingPdf dlg (*document,
70 resolution);
71 if (dlg.exec() == QDialog::Accepted) {
72
73 // Returned image is null if it could not be read
74 image = dlg.image ();
75
76 if (!image.isNull()) {
77 pdfReturn = PDF_RETURN_SUCCESS;
78 }
79
80 } else {
81 pdfReturn = PDF_RETURN_CANCELED;
82 }
83
84 return pdfReturn;
85}
86
87PdfReturn Pdf::loadWithoutCropping (const QString &fileName,
88 QImage &image,
89 int resolution) const
90{
91 PdfReturn pdfReturn = PDF_RETURN_FAILED;
92
93 // Simple check to prevent complaints from poppler code
94 if (fileName.right (4).toLower () == ".pdf") {
95
96 // Try to read the file
97 Document *document = Document::load (fileName).get();
98
99 if (document != nullptr) {
100 if (!document->isLocked ()) {
101
102 Page *page = document->page (FIRST_PAGE_1_BASED - 1).get();
103 if (page != nullptr) {
104
105 image = page->renderToImage (resolution,
106 resolution,
109 WIDTH,
110 HEIGHT);
111
112 if (!image.isNull()) {
113 pdfReturn = PDF_RETURN_SUCCESS;
114 }
115
116 delete page;
117 }
118 }
119
120 delete document;
121 }
122 }
123
124 return pdfReturn;
125}
const int X_TOP_LEFT
const int WIDTH
const int FIRST_PAGE_1_BASED
const int HEIGHT
const int Y_TOP_LEFT
ImportCropping
PdfReturn
Return values from load operation.
Definition Pdf.h:19
@ PDF_RETURN_FAILED
Definition Pdf.h:21
@ PDF_RETURN_CANCELED
Definition Pdf.h:20
@ PDF_RETURN_SUCCESS
Definition Pdf.h:22
Dialog for selecting a page and frame on that page when importing an image from a pdf file.
Storage of one imported image and the data attached to that image.
Definition Document.h:44
bool applyImportCropping(bool isRegression, const QString &fileName, ImportCropping importCropping, Poppler::Document *&document) const
For pdf files, skip cropping dialog during regression testing, otherwise crop if it is always turned ...
Pdf()
Single constructor.
Definition Pdf.cpp:21
PdfReturn load(const QString &fileName, QImage &image, int resolution, ImportCropping importCropping, bool isErrorReportRegressionTest) const
Try to load the specified file. Success is indicated in the function return value.
Definition Pdf.cpp:25