GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

GTK+, Cairo & Poppler PDF Viewer example (C)

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code
Author Message
caracal
GTK+ Guru


Joined: 21 Jun 2007
Posts: 207
Location: Wilkes Barre Pa

PostPosted: Thu Apr 23, 2009 11:14 am    Post subject: GTK+, Cairo & Poppler PDF Viewer example (C) Reply with quote

This is an example of a basic PDF viewer.

Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

#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"



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
Back to top
wjgiddings
GTK+ Geek


Joined: 06 Feb 2009
Posts: 71
Location: The Midlands, England

PostPosted: Thu Apr 23, 2009 11:48 am    Post subject: Reply with quote

Sorry,

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

WJG
Back to top
caracal
GTK+ Guru


Joined: 21 Jun 2007
Posts: 207
Location: Wilkes Barre Pa

PostPosted: Thu Apr 23, 2009 11:53 am    Post subject: Reply with quote

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.
Back to top
wjgiddings
GTK+ Geek


Joined: 06 Feb 2009
Posts: 71
Location: The Midlands, England

PostPosted: Thu Apr 23, 2009 12:01 pm    Post subject: I should have read the small print! Reply with quote

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
Back to top
caracal
GTK+ Guru


Joined: 21 Jun 2007
Posts: 207
Location: Wilkes Barre Pa

PostPosted: Thu Apr 23, 2009 12:21 pm    Post subject: Reply with quote

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.
Back to top
ramesh
GTK+ Guru


Joined: 16 Nov 2006
Posts: 270
Location: INDIA

PostPosted: Thu Apr 23, 2009 12:42 pm    Post subject: Reply with quote

hi
i compiled the code above.
i am using vista.

Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
Back to top
caracal
GTK+ Guru


Joined: 21 Jun 2007
Posts: 207
Location: Wilkes Barre Pa

PostPosted: Thu Apr 23, 2009 12:53 pm    Post subject: Reply with quote

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/poppler/tree/README.windows
Back to top
m.nour



Joined: 07 Feb 2010
Posts: 3

PostPosted: Sun Feb 28, 2010 2:58 am    Post subject: Reply with quote

thanks a lot, that was really helpful
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP