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