00001
00002
00003
00004
00005
00006
00007 #include "DlgImportCroppingPdf.h"
00008 #include "ImportCroppingUtilPdf.h"
00009 #include "Pdf.h"
00010 #include "poppler-qt5.h"
00011 #include <QApplication>
00012 #include <QImage>
00013 #include <QString>
00014
00015 using namespace Poppler;
00016
00017 const int X_TOP_LEFT = 0, Y_TOP_LEFT = 0;
00018 const int WIDTH = -1, HEIGHT = -1;
00019 const int FIRST_PAGE_1_BASED = 1;
00020
00021 Pdf::Pdf ()
00022 {
00023 }
00024
00025 PdfReturn Pdf::load (const QString &fileName,
00026 QImage &image,
00027 int resolution,
00028 ImportCropping importCropping,
00029 bool isErrorReportRegressionTest) const
00030 {
00031 Document *document = 0;
00032
00033 ImportCroppingUtilPdf importCroppingUtil;
00034 bool cropping = importCroppingUtil.applyImportCropping (isErrorReportRegressionTest,
00035 fileName,
00036 importCropping,
00037 document);
00038
00039 PdfReturn rtn;
00040 QApplication::setOverrideCursor(Qt::BusyCursor);
00041 if (cropping) {
00042
00043 rtn = loadWithCropping (document,
00044 image,
00045 resolution);
00046
00047 } else {
00048
00049 rtn = loadWithoutCropping (fileName,
00050 image,
00051 resolution);
00052
00053 }
00054 QApplication::restoreOverrideCursor();
00055
00056 if (document != 0) {
00057 delete document;
00058 }
00059
00060 return rtn;
00061 }
00062
00063 PdfReturn Pdf::loadWithCropping (Document *document,
00064 QImage &image,
00065 int resolution) const
00066 {
00067 PdfReturn pdfReturn = PDF_RETURN_FAILED;
00068
00069
00070 DlgImportCroppingPdf dlg (*document,
00071 resolution);
00072 if (dlg.exec() == QDialog::Accepted) {
00073
00074
00075 image = dlg.image ();
00076
00077 if (!image.isNull()) {
00078 pdfReturn = PDF_RETURN_SUCCESS;
00079 }
00080
00081 } else {
00082 pdfReturn = PDF_RETURN_CANCELED;
00083 }
00084
00085 return pdfReturn;
00086 }
00087
00088 PdfReturn Pdf::loadWithoutCropping (const QString &fileName,
00089 QImage &image,
00090 int resolution) const
00091 {
00092 PdfReturn pdfReturn = PDF_RETURN_FAILED;
00093
00094
00095 if (fileName.right (4).toLower () == ".pdf") {
00096
00097
00098 Document *document = Document::load (fileName);
00099
00100 if (document != 0) {
00101 if (!document->isLocked ()) {
00102
00103 Page *page = document->page (FIRST_PAGE_1_BASED - 1);
00104 if (page != 0) {
00105
00106 image = page->renderToImage (resolution,
00107 resolution,
00108 X_TOP_LEFT,
00109 Y_TOP_LEFT,
00110 WIDTH,
00111 HEIGHT);
00112
00113 if (!image.isNull()) {
00114 pdfReturn = PDF_RETURN_SUCCESS;
00115 }
00116
00117 delete page;
00118 }
00119 }
00120
00121 delete document;
00122 }
00123 }
00124
00125 return pdfReturn;
00126 }