test-poppler-glib.c 2.05 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <stdlib.h>
#include "poppler.h"

#define FAIL(msg) \
	do { fprintf (stderr, "FAIL: %s\n", msg); exit (-1); } while (0)

int main (int argc, char *argv[])
{
  PopplerDocument *document;
  PopplerPage *page;
12
  char *title, *label;
13
  GError *error;
14
  GdkPixbuf *pixbuf, *thumb;
15
  double width, height;
16
  GList *list, *l;
17

18
19
  if (argc != 3)
    FAIL ("usage: test-poppler-glib FILE PAGE");
20
21
22
23
24
25
26
27

  g_type_init ();

  error = NULL;
  document = poppler_document_new_from_file (argv[1], NULL, &error);
  if (document == NULL)
    FAIL (error->message);
      
28
29
30
31
  g_object_get (document, "title", &title, NULL);
  printf ("document title: %s\n", title);
  g_free (title);

32
33
34
35
  page = poppler_document_get_page_by_label (document, argv[2]);
  if (page == NULL)
    FAIL ("page not found");

36
37
  poppler_page_get_size (page, &width, &height);
  printf ("page size: %f inches by %f inches\n", width / 72, height / 72);
38

39
40
41
42
43
44
45
46
47
48
49
50
  thumb = poppler_page_get_thumbnail (page);
  if (thumb != NULL) {
    gdk_pixbuf_save (thumb, "thumb.png", "png", &error, NULL);
    if (error != NULL)
      FAIL (error->message);
    else
      printf ("saved thumbnail as thumb.png\n");
    g_object_unref (G_OBJECT (thumb));
  }
  else
    printf ("no thumbnail for page\n");

51
52
53
54
  g_object_get (page, "label", &label, NULL);
  printf ("page label: %s\n", label);
  g_free (label);

55
  pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 220, 220);
56
  gdk_pixbuf_fill (pixbuf, 0x00106000);
57
  poppler_page_render_to_pixbuf (page, 100, 100, 200, 200, 1, pixbuf, 10, 10);
58
59

  gdk_pixbuf_save (pixbuf, "slice.png", "png", &error, NULL);
60
  printf ("saved 200x200 slice at (100, 100) as slice.png\n");
61
62
63
  if (error != NULL)
    FAIL (error->message);

64
65
66
67
68
69
70
71
72
73
74
75
  list = poppler_page_find_text (page, "Bitwise");
  printf ("Found text \"Bitwise\" at positions:\n");
  for (l = list; l != NULL; l = l->next)
    {
      PopplerRectangle *rect = l->data;

      printf ("  (%f,%f)-(%f,%f)\n", rect->x1, rect->y1, rect->x2, rect->y2);
    }
    

  g_object_unref (G_OBJECT (page));

76
  g_object_unref (G_OBJECT (document));
77
78

  return 0;
79
}