[poppler] poppler/qt: poppler-page.cc, 1.3, 1.4 poppler-qt.h, 1.4, 1.5 test-poppler-qt.cpp, 1.1, 1.2

Jeff Muizelaar jrmuizel at freedesktop.org
Wed Apr 20 15:48:55 PDT 2005


Update of /cvs/poppler/poppler/qt
In directory gabe:/tmp/cvs-serv23907/qt

Modified Files:
	poppler-page.cc poppler-qt.h test-poppler-qt.cpp 
Log Message:
2005-04-13  Jeff Muizelaar  <jrmuizel at nit.ca>

	* qt/poppler-page.cc (Page::getText):
	* qt/poppler-qt.h: add a getText method for getting
	the text on a page

	* qt/test-poppler-qt.c (PDFDisplay::PDFDisplay): 
	add the option to display the text on a page

	Patch from Albert Astals Cid.


Index: poppler-page.cc
===================================================================
RCS file: /cvs/poppler/poppler/qt/poppler-page.cc,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- poppler-page.cc	8 Apr 2005 03:11:01 -0000	1.3
+++ poppler-page.cc	20 Apr 2005 22:48:53 -0000	1.4
@@ -24,6 +24,7 @@
 #include <Catalog.h>
 #include <ErrorCodes.h>
 #include <SplashOutputDev.h>
+#include <TextOutputDev.h>
 #include <splash/SplashBitmap.h>
 #include "poppler-private.h"
 
@@ -69,4 +70,40 @@
   delete output_dev;
 }
 
+QString Page::getText(const Rectangle &r) const
+{
+  TextOutputDev *output_dev;
+  GooString *s;
+  PDFRectangle *rect;
+  QString result;
+  ::Page *p;
+  
+  output_dev = new TextOutputDev(0, gFalse, gFalse, gFalse);
+  data->doc->data->doc.displayPageSlice(output_dev, data->index + 1, 72, 72,
+      0, -1, -1, -1, -1,
+      true,
+      false);
+  p = data->doc->data->doc.getCatalog()->getPage(data->index + 1);
+  if (r.isNull())
+  {
+    rect = p->getBox();
+    s = output_dev->getText(rect->x1, rect->y1, rect->x2, rect->y2);
+  }
+  else
+  {
+    double height, y1, y2;
+    height = p->getHeight();
+    y1 = height - r.m_y2;
+    y2 = height - r.m_y1;
+    s = output_dev->getText(r.m_x1, y1, r.m_x2, y2);
+  }
+
+  // TODO look if QString::fromUTF8 yields better results
+  result = s->getCString();
+  
+  delete output_dev;
+  delete s;
+  return result;
+}
+
 }

Index: poppler-qt.h
===================================================================
RCS file: /cvs/poppler/poppler/qt/poppler-qt.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- poppler-qt.h	9 Apr 2005 18:14:39 -0000	1.4
+++ poppler-qt.h	20 Apr 2005 22:48:53 -0000	1.5
@@ -25,12 +25,32 @@
 
 namespace Poppler {
 
+/* A rectangle on a page, with coordinates in PDF points. */
+class Rectangle
+{
+  public:
+    Rectangle(double x1 = 0, double y1 = 0, double x2 = 0, double y2 = 0) : 
+      m_x1(x1), m_y1(y1), m_x2(x2), m_y2(y2) {}
+    bool isNull() const { return m_x1 == 0 && m_y1 == 0 && m_x2 == 0 && m_y2 == 0; }
+  
+    double m_x1;
+    double m_y1;
+    double m_x2;
+    double m_y2;
+};
+
 class PageData;
 class Page {
   friend class Document;
   public:
     ~Page();
     void renderToPixmap(QPixmap **q, int x, int y, int w, int h) const;
+    
+    /**
+    * Returns the text that is inside the Rectangle r
+    * If r is a null Rectangle all text of the page is given
+    **/
+    QString getText(const Rectangle &r) const;
   private:
     Page(const Document *doc, int index);
     PageData *data;

Index: test-poppler-qt.cpp
===================================================================
RCS file: /cvs/poppler/poppler/qt/test-poppler-qt.cpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- test-poppler-qt.cpp	6 Apr 2005 14:39:40 -0000	1.1
+++ test-poppler-qt.cpp	20 Apr 2005 22:48:53 -0000	1.2
@@ -10,7 +10,7 @@
 class PDFDisplay : public QWidget           // picture display widget
 {
 public:
-    PDFDisplay( const char *fileName );
+    PDFDisplay( Poppler::Document *d );
    ~PDFDisplay();
 protected:
     void        paintEvent( QPaintEvent * );
@@ -19,9 +19,9 @@
     Poppler::Document *doc;
 };
 
-PDFDisplay::PDFDisplay( const char *fileName )
+PDFDisplay::PDFDisplay( Poppler::Document *d )
 {
-  doc = Poppler::Document::load(fileName);
+  doc = d;
   if (doc) {
     Poppler::Page *page = doc->getPage(0);
     if (page) {
@@ -50,14 +50,34 @@
 {
   QApplication a( argc, argv );               // QApplication required!
 
-  if ( argc != 2 )  {                          // use argument as file name
-    printf("usage: test-poppler-qt filename\n");
+  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");
     exit(1);
   }
-  PDFDisplay test( argv[1] );        // create picture display
-  a.setMainWidget( &test);                // set main widget
-  test.setCaption("Poppler-Qt Test");
-  test.show();                            // show it
+  
+  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
 
-  return a.exec();                        // start event loop
+    return a.exec();                        // start event loop
+  }
+  else
+  {
+    Poppler::Page *page = doc->getPage(0);
+    qDebug(page->getText(Poppler::Rectangle()));
+    delete page;
+    delete doc;
+  }
 }



More information about the poppler mailing list