Engauge Digitizer  2
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 
7 #include "DlgImportCroppingPdf.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 = 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 
62 PdfReturn 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 
87 PdfReturn 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);
98 
99  if (document != nullptr) {
100  if (!document->isLocked ()) {
101 
102  Page *page = document->page (FIRST_PAGE_1_BASED - 1);
103  if (page != nullptr) {
104 
105  image = page->renderToImage (resolution,
106  resolution,
107  X_TOP_LEFT,
108  Y_TOP_LEFT,
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 }
PdfReturn
PdfReturn
Return values from load operation.
Definition: Pdf.h:18
ImportCroppingUtilPdf::applyImportCropping
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 ...
Definition: ImportCroppingUtilPdf.cpp:17
DlgImportCroppingPdf
Dialog for selecting a page and frame on that page when importing an image from a pdf file.
Definition: DlgImportCroppingPdf.h:27
Document
Storage of one imported image and the data attached to that image.
Definition: Document.h:40
PDF_RETURN_FAILED
Definition: Pdf.h:20
Pdf::Pdf
Pdf()
Single constructor.
Definition: Pdf.cpp:21
Pdf::load
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
HEIGHT
const int HEIGHT
Definition: Pdf.cpp:18
Y_TOP_LEFT
const int Y_TOP_LEFT
Definition: Pdf.cpp:17
PDF_RETURN_SUCCESS
Definition: Pdf.h:21
Pdf.h
WIDTH
const int WIDTH
Definition: Pdf.cpp:18
ImportCropping
ImportCropping
Definition: ImportCropping.h:9
DlgImportCroppingPdf.h
ImportCroppingUtilPdf
Import of pdf files.
Definition: ImportCroppingUtilPdf.h:18
FIRST_PAGE_1_BASED
const int FIRST_PAGE_1_BASED
Definition: Pdf.cpp:19
PDF_RETURN_CANCELED
Definition: Pdf.h:19
X_TOP_LEFT
const int X_TOP_LEFT
Definition: Pdf.cpp:17
ImportCroppingUtilPdf.h
Poppler
Definition: DlgImportCroppingPdf.h:13