test-poppler-qt.cpp 1.88 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
#include <qapplication.h>
#include <qpainter.h>
#include <qpixmap.h>
#include <qwidget.h>
#include <qmessagebox.h>
#include <qfile.h>
#include <ctype.h>
#include <poppler-qt.h>

class PDFDisplay : public QWidget           // picture display widget
{
public:
13
    PDFDisplay( Poppler::Document *d );
14
15
16
17
18
19
20
21
   ~PDFDisplay();
protected:
    void        paintEvent( QPaintEvent * );
private:
    QPixmap	*pixmap;
    Poppler::Document *doc;
};

22
PDFDisplay::PDFDisplay( Poppler::Document *d )
23
{
24
  doc = d;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  if (doc) {
    Poppler::Page *page = doc->getPage(0);
    if (page) {
      page->renderToPixmap(&pixmap, -1, -1, -1, -1);
      delete page;
    }
  } else {
    printf("doc not loaded\n");
  }
}

PDFDisplay::~PDFDisplay()
{
  delete doc;
  delete pixmap;
}

void PDFDisplay::paintEvent( QPaintEvent *e )
{
  QPainter paint( this );                     // paint widget
  if (pixmap)
    paint.drawPixmap(0, 0, *pixmap);
}

int main( int argc, char **argv )
{
  QApplication a( argc, argv );               // QApplication required!

53
54
55
56
  if ( argc < 2  || (argc == 3 && strcmp(argv[2], "-extract") != 0) || argc > 3)
  {
    // use argument as file name
    printf("usage: test-poppler-qt filename [-extract]\n");
57
58
    exit(1);
  }
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  
  Poppler::Document *doc = Poppler::Document::load(argv[1]);
  if (!doc)
  {
    printf("doc not loaded\n");
    exit(1);
  }
  
  if (argc == 2)
  {  
    PDFDisplay test( doc );        // create picture display
    a.setMainWidget( &test);                // set main widget
    test.setCaption("Poppler-Qt Test");
    test.show();                            // show it
73

74
75
76
77
78
    return a.exec();                        // start event loop
  }
  else
  {
    Poppler::Page *page = doc->getPage(0);
79
80
81
82

    QLabel *l = new QLabel(page->getText(Poppler::Rectangle()), 0);
    l->show();
    a.setMainWidget(l);                // set main widget
83
84
    delete page;
    delete doc;
85
    return a.exec();
86
  }
87
}