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

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

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

static void
print_document_info (PopplerDocument *document)
{
  gchar *title, *format, *author, *subject, *keywords;
  PopplerPageLayout layout;
  PopplerPageMode mode;
  PopplerViewerPreferences view_prefs;
  GEnumValue *enum_value;

  g_object_get (document,
		"title", &title,
		"format", &format,
		"author", &author,
		"subject", &subject,
		"keywords", &keywords,
		"page-mode", &mode,
		"page-layout", &layout,
		"viewer-preferences", &view_prefs,
		NULL);

  printf ("document metadata\n");
  if (title)  printf   ("\ttitle:\t%s\n", title);
  if (format) printf   ("\tformat:\t%s\n", format);
  if (author) printf   ("\tauthor:\t%s\n", author);
  if (subject) printf  ("\tsubject:\t%s\n", subject);
  if (keywords) printf ("\tdkeywords:\t%s\n", keywords);

  enum_value = g_enum_get_value ((GEnumClass *) g_type_class_peek (POPPLER_TYPE_PAGE_MODE), mode);
  g_print ("\tpage mode:\t%s\n", enum_value->value_name);
  enum_value = g_enum_get_value ((GEnumClass *) g_type_class_peek (POPPLER_TYPE_PAGE_LAYOUT), layout);
  g_print ("\tpage layout:\t%s\n", enum_value->value_name);

  /* FIXME: print out the view prefs when we support it */

  g_free (title);
  g_free (format);
  g_free (author);
  g_free (subject);
  g_free (keywords);
}

51
52
53
54
int main (int argc, char *argv[])
{
  PopplerDocument *document;
  PopplerPage *page;
55
  char *label;
56
  GError *error;
57
  GdkPixbuf *pixbuf, *thumb;
58
  double width, height;
59
  GList *list, *l;
60
61
  char *text;
  PopplerRectangle area;
62

63
64
  if (argc != 3)
    FAIL ("usage: test-poppler-glib FILE PAGE");
65
66
67
68
69
70
71
72

  g_type_init ();

  error = NULL;
  document = poppler_document_new_from_file (argv[1], NULL, &error);
  if (document == NULL)
    FAIL (error->message);
      
73
  print_document_info (document); 
74

75
76
77
78
  page = poppler_document_get_page_by_label (document, argv[2]);
  if (page == NULL)
    FAIL ("page not found");

79
80
  poppler_page_get_size (page, &width, &height);
  printf ("page size: %f inches by %f inches\n", width / 72, height / 72);
81

82
83
84
85
86
87
88
89
90
91
92
93
  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");

94
95
96
97
  g_object_get (page, "label", &label, NULL);
  printf ("page label: %s\n", label);
  g_free (label);

98
  pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 220, 220);
99
  gdk_pixbuf_fill (pixbuf, 0x00106000);
100
  poppler_page_render_to_pixbuf (page, 100, 100, 200, 200, 1, pixbuf, 10, 10);
101
102

  gdk_pixbuf_save (pixbuf, "slice.png", "png", &error, NULL);
103
  printf ("saved 200x200 slice at (100, 100) as slice.png\n");
104
105
106
  if (error != NULL)
    FAIL (error->message);

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  area.x1 = 0;
  area.y1 = 0;
  area.x2 = width;
  area.y2 = height;

  text = poppler_page_get_text (page, &area);
  if (text)
    {
      FILE *file = fopen ("dump.txt", "w");
      if (file)
	{
	  fwrite (text, strlen (text), 1, file);
	  fclose (file);
	}
      g_free (text);
    }

124
125
126
127
128
129
130
131
132
133
134
135
  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));

136
  g_object_unref (G_OBJECT (document));
137
138

  return 0;
139
}