Engauge Digitizer  2
Pdf.cpp
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 
7 #include "DlgImportCroppingPdf.h"
8 #include "ImportCroppingUtilPdf.h"
9 #include "Pdf.h"
10 #include "poppler-qt5.h"
11 #include <QApplication>
12 #include <QImage>
13 #include <QString>
14 
15 using namespace Poppler;
16 
17 const int X_TOP_LEFT = 0, Y_TOP_LEFT = 0;
18 const int WIDTH = -1, HEIGHT = -1; // Negative values give full page
19 const int FIRST_PAGE_1_BASED = 1;
20 
22 {
23 }
24 
25 PdfReturn Pdf::load (const QString &fileName,
26  QImage &image,
27  int resolution,
28  ImportCropping importCropping,
29  bool isErrorReportRegressionTest) const
30 {
31  Document *document = 0;
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  if (document != 0) {
57  delete document;
58  }
59 
60  return rtn;
61 }
62 
63 PdfReturn Pdf::loadWithCropping (Document *document,
64  QImage &image,
65  int resolution) const
66 {
67  PdfReturn pdfReturn = PDF_RETURN_FAILED;
68 
69  // Get page and extent. At this point it is always true that the image can be read
70  DlgImportCroppingPdf dlg (*document,
71  resolution);
72  if (dlg.exec() == QDialog::Accepted) {
73 
74  // Returned image is null if it could not be read
75  image = dlg.image ();
76 
77  if (!image.isNull()) {
78  pdfReturn = PDF_RETURN_SUCCESS;
79  }
80 
81  } else {
82  pdfReturn = PDF_RETURN_CANCELED;
83  }
84 
85  return pdfReturn;
86 }
87 
88 PdfReturn Pdf::loadWithoutCropping (const QString &fileName,
89  QImage &image,
90  int resolution) const
91 {
92  PdfReturn pdfReturn = PDF_RETURN_FAILED;
93 
94  // Simple check to prevent complaints from poppler code
95  if (fileName.right (4).toLower () == ".pdf") {
96 
97  // Try to read the file
98  Document *document = Document::load (fileName);
99 
100  if (document != 0) {
101  if (!document->isLocked ()) {
102 
103  Page *page = document->page (FIRST_PAGE_1_BASED - 1);
104  if (page != 0) {
105 
106  image = page->renderToImage (resolution,
107  resolution,
108  X_TOP_LEFT,
109  Y_TOP_LEFT,
110  WIDTH,
111  HEIGHT);
112 
113  if (!image.isNull()) {
114  pdfReturn = PDF_RETURN_SUCCESS;
115  }
116 
117  delete page;
118  }
119  }
120 
121  delete document;
122  }
123  }
124 
125  return pdfReturn;
126 }
Pdf()
Single constructor.
Definition: Pdf.cpp:21
QImage image() const
Image that was selected. Value is null if loading failed.
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 ...
Storage of one imported image and the data attached to that image.
Definition: Document.h:41
Import of pdf files.
Dialog for selecting a page and frame on that page when importing an image from a pdf file...
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