GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Oct 25, 2014 1:28 pm

All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: GTK+, Cairo & Poppler PDF Viewer example (C)
PostPosted: Thu Apr 23, 2009 11:14 am 
Offline
GTK+ Guru

Joined: Thu Jun 21, 2007 1:52 pm
Posts: 198
Location: Wilkes Barre Pa
This is an example of a basic PDF viewer.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <cairo.h>
#include <poppler.h>

/* gcc `pkg-config --cflags --libs gtk+-2.0 poppler-glib` -o pdfviewer pdfviewer.c */

static PopplerDocument* doc;
static PopplerPage* page;

static void
on_destroy(GtkWidget* w, gpointer data) {
    gtk_main_quit();
}

static gboolean
on_expose(GtkWidget* w, GdkEventExpose* e, gpointer data) {
    cairo_t* cr;
    cr = gdk_cairo_create(w->window);
    poppler_page_render(page, cr);
    cairo_destroy(cr);
    return FALSE;
}

int main(int argc, char* argv[]) {
    GtkWidget* win;
    GError* err = NULL;

    if (argc != 2) {
        printf("Useage: pdfviewer <uri>\n");
        return 1;
    }

    gtk_init(&argc, &argv);

    doc = poppler_document_new_from_file(argv[1], NULL, &err);
    if (!doc) {
        printf("%s\n", err->message);
        g_object_unref(err);
        return 2;
    }

    page = poppler_document_get_page(doc, 0);
    if(!page) {
        printf("Could not open first page of document\n");
        g_object_unref(doc);
        return 3;
    }

    int pages = poppler_document_get_n_pages(doc);
    printf("There are %d pages in this pdf.\n", pages);
     
    win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(G_OBJECT(win), "destroy",      G_CALLBACK(on_destroy), NULL);
    g_signal_connect(G_OBJECT(win), "expose-event", G_CALLBACK(on_expose), NULL);
    gtk_widget_set_app_paintable(win, TRUE);
    gtk_widget_show_all(win);

    gtk_main();

    g_object_unref(page);
    g_object_unref(doc);

    return 0;
}


Usage: ./pdfviewer "file:/home/caracal/pdfviewer/ooc.pdf"

Image

Portable Document Format (PDF)
Poppler "Poppler is a PDF rendering library and is used with cairo to render PDF's"
[HTML] http://poppler.freedesktop.org/
[Example Viewer] http://www.gtkforums.com/viewtopic.php?p=9086#9086

libharu "libHaru is a free, cross platform, open source library for generating PDF files."
[HTML] http://libharu.org/wiki/Main_Page
[HTML] http://libharu.org/wiki/Documentation
[NOTE] Cairo can also produce PDF's but libharu does a way better job and has tons more features.


Last edited by caracal on Fri Apr 24, 2009 8:10 pm, edited 3 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 23, 2009 11:48 am 
Sorry,

Compiled and ran the sample. I found it crashed every time.

WJG


Top
  
 
 Post subject:
PostPosted: Thu Apr 23, 2009 11:53 am 
Offline
GTK+ Guru

Joined: Thu Jun 21, 2007 1:52 pm
Posts: 198
Location: Wilkes Barre Pa
Make sure you URI is correct.
Usage: pdfviewer <uri>
Example: ./pdfviewer "file:/home/caracal/pdfviewer/ooc.pdf"
Has to be "file:/"
This is a minimal example.


Top
 Profile  
 
 Post subject: I should have read the small print!
PostPosted: Thu Apr 23, 2009 12:01 pm 
Thanks, I should have read the instructions more closely.

I've just quickly skimmed over the documentation and it wouldn't be too much of a task to wrap a Gnocl layer around the Popper package and build a gnocl::pdfviewer widget.

Thanks too for the posting,

Will


Top
  
 
 Post subject:
PostPosted: Thu Apr 23, 2009 12:21 pm 
Offline
GTK+ Guru

Joined: Thu Jun 21, 2007 1:52 pm
Posts: 198
Location: Wilkes Barre Pa
Check Gtk+ Tutorials & Resources
http://www.gtkforums.com/viewtopic.php?t=988

You might find some more stuff that interest you for Gnocl.
Its updated almost daily.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 23, 2009 12:42 pm 
Offline
GTK+ Guru

Joined: Thu Nov 16, 2006 5:08 am
Posts: 259
Location: INDIA
hi
i compiled the code above.
i am using vista.

Code:
pdf_viewer.c:7:21: poppler.h: No such file or directory
pdf_viewer.c:11: error: syntax error before '*' token
pdf_viewer.c:11: warning: type defaults to `int' in declaration of `doc'
pdf_viewer.c:11: warning: data definition has no type or storage class
pdf_viewer.c:12: error: syntax error before '*' token
pdf_viewer.c:12: warning: type defaults to `int' in declaration of `page'
pdf_viewer.c:12: warning: data definition has no type or storage class
pdf_viewer.c: In function `on_expose':
pdf_viewer.c:23: warning: implicit declaration of function `poppler_page_render'
pdf_viewer.c: In function `main':
pdf_viewer.c:40: warning: implicit declaration of function `poppler_document_new_from_file'
pdf_viewer.c:40: warning: assignment makes pointer from integer without a cast
pdf_viewer.c:47: warning: implicit declaration of function `poppler_document_get_page'
pdf_viewer.c:47: warning: assignment makes pointer from integer without a cast
pdf_viewer.c:54: warning: implicit declaration of function `poppler_document_get_n_pages'


how to install poppler in windows vista..

any help it should be appreciiable


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 23, 2009 12:53 pm 
Offline
GTK+ Guru

Joined: Thu Jun 21, 2007 1:52 pm
Posts: 198
Location: Wilkes Barre Pa
I don't use windows so i wouldn't be able to answer you question but i did find this via google.
http://cgit.freedesktop.org/poppler/pop ... ME.windows


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 28, 2010 2:58 am 
Offline

Joined: Sun Feb 07, 2010 7:30 pm
Posts: 2
thanks a lot, that was really helpful


Top
 Profile  
 
 Post subject: Re: GTK+, Cairo & Poppler PDF Viewer example (C)
PostPosted: Wed Jun 01, 2011 7:21 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
Sorry to sound anal, but wouldn't this be better in the project showcase forum?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group