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 std::unique_ptr<Document> document;
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 (std::move(document),
44 image,
45 resolution);
46
47 } else {
48
49 rtn = loadWithoutCropping (fileName,
50 image,
51 resolution);
52
53 }
54 QApplication::restoreOverrideCursor();
55
56 return rtn;
57}
58
59PdfReturn Pdf::loadWithCropping (std::unique_ptr<Document> document,
60 QImage &image,
61 int resolution) const
62{
63 PdfReturn pdfReturn = PDF_RETURN_FAILED;
64
65 // Get page and extent. At this point it is always true that the image can be read
66 DlgImportCroppingPdf dlg (*document,
67 resolution);
68 if (dlg.exec() == QDialog::Accepted) {
69
70 // Returned image is null if it could not be read
71 image = dlg.image ();
72
73 if (!image.isNull()) {
74 pdfReturn = PDF_RETURN_SUCCESS;
75 }
76
77 } else {
78 pdfReturn = PDF_RETURN_CANCELED;
79 }
80
81 return pdfReturn;
82}
83
84PdfReturn Pdf::loadWithoutCropping (const QString &fileName,
85 QImage &image,
86 int resolution) const
87{
88 PdfReturn pdfReturn = PDF_RETURN_FAILED;
89
90 // Simple check to prevent complaints from poppler code
91 if (fileName.right (4).toLower () == ".pdf") {
92
93 // Try to read the file
94 std::unique_ptr<Document> document = Document::load (fileName);
95
96 if (document != nullptr) {
97 if (!document->isLocked ()) {
98
99 std::unique_ptr<Page> page = document->page (FIRST_PAGE_1_BASED - 1);
100 if (page != nullptr) {
101
102 image = page->renderToImage (resolution,
103 resolution,
106 WIDTH,
107 HEIGHT);
108
109 if (!image.isNull()) {
110 pdfReturn = PDF_RETURN_SUCCESS;
111 }
112 }
113 }
114 }
115 }
116
117 return pdfReturn;
118}
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:20
@ PDF_RETURN_FAILED
Definition Pdf.h:22
@ PDF_RETURN_CANCELED
Definition Pdf.h:21
@ PDF_RETURN_SUCCESS
Definition Pdf.h:23
Dialog for selecting a page and frame on that page when importing an image from a pdf file.
bool applyImportCropping(bool isRegression, const QString &fileName, ImportCropping importCropping, std::unique_ptr< 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