test-poppler-glib.c 6.28 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
static void
print_index (PopplerIndexIter *iter)
{
  do
    {
      PopplerAction *action;
16
      PopplerIndexIter *child;
17
18
19
20

      action = poppler_index_iter_get_action (iter);
      g_print ("Action: %d\n", action->type);
      poppler_action_free (action);
21
      child = poppler_index_iter_get_child (iter);
22
23
24
25
26
27
28
      if (child)
	print_index (child);
      poppler_index_iter_free (child);
    }
  while (poppler_index_iter_next (iter));
}

29
30
31
static void
print_document_info (PopplerDocument *document)
{
32
33
  gchar *title, *format, *author, *subject, *keywords, *creator, *producer, *linearized;
  GTime creation_date, mod_date;
34
35
36
  PopplerPageLayout layout;
  PopplerPageMode mode;
  PopplerViewerPreferences view_prefs;
37
38
  PopplerFontInfo *font_info;
  PopplerFontsIter *fonts_iter;
39
  PopplerIndexIter *index_iter;
40
41
42
43
44
45
46
47
  GEnumValue *enum_value;

  g_object_get (document,
		"title", &title,
		"format", &format,
		"author", &author,
		"subject", &subject,
		"keywords", &keywords,
48
49
50
51
52
		"creation-date", &creation_date,
		"mod-date", &mod_date,
		"creator", &creator,
		"producer", &producer,	
		"linearized", &linearized,
53
54
55
56
57
		"page-mode", &mode,
		"page-layout", &layout,
		"viewer-preferences", &view_prefs,
		NULL);

58
59
60
61
62
63
  printf ("\t---------------------------------------------------------\n");
  printf ("\tDocument Metadata\n");
  printf ("\t---------------------------------------------------------\n");
  if (title)  printf   ("\ttitle:\t\t%s\n", title);
  if (format) printf   ("\tformat:\t\t%s\n", format);
  if (author) printf   ("\tauthor:\t\t%s\n", author);
64
  if (subject) printf  ("\tsubject:\t%s\n", subject);
65
66
67
68
69
  if (keywords) printf ("\tkeywords:\t%s\n", keywords);
  if (creator) printf ("\tcreator:\t%s\n", creator);
  if (producer) printf ("\tproducer:\t%s\n", producer);
  if (linearized) printf ("\tlinearized:\t%s\n", linearized);
  
70
71
72
73
74
  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);

75
76
77
  g_print ("\tcreation date:\t%d\n", creation_date);
  g_print ("\tmodified date:\t%d\n", mod_date);

78
79
80
81
82
83
84
85
86
87
88
89
  g_print ("\tfonts:\n");
  font_info = poppler_font_info_new (document);
  while (poppler_font_info_scan (font_info, 20, &fonts_iter)) {
    if (fonts_iter) {
      do {
        g_print ("\t\t\t%s\n", poppler_fonts_iter_get_name (fonts_iter));
      } while (poppler_fonts_iter_next (fonts_iter));
      poppler_fonts_iter_free (fonts_iter);
    }
  }
  poppler_font_info_free (font_info);

90
91
92
93
94
95
96
97
98
99
  index_iter = poppler_index_iter_new (document);
  if (index_iter)
    {
      g_print ("\tindex:\n");
      print_index (index_iter);
      poppler_index_iter_free (index_iter);
    }
  

  
100
101
102
103
104
105
106
  /* 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);
107
108
109
  g_free (creator);
  g_free (producer); 
  g_free (linearized);
110
111
}

112
113
114
int main (int argc, char *argv[])
{
  PopplerDocument *document;
115
  PopplerBackend backend;
116
  PopplerPage *page;
117
  GEnumValue *enum_value;
118
  char *label;
119
  GError *error;
120
  GdkPixbuf *pixbuf, *thumb;
121
  double width, height;
122
  GList *list, *l;
123
124
  char *text;
  PopplerRectangle area;
125

126
  if (argc != 3)
127
    FAIL ("usage: test-poppler-glib file://FILE PAGE");
128
129
130

  g_type_init ();

131
132
133
134
135
  g_print ("Poppler version %s\n", poppler_get_version ());
  backend = poppler_get_backend ();
  enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_BACKEND), backend);
  g_print ("Backend is %s\n", enum_value->value_name);

136
137
138
139
  error = NULL;
  document = poppler_document_new_from_file (argv[1], NULL, &error);
  if (document == NULL)
    FAIL (error->message);
140

141
  print_document_info (document); 
142

143
144
145
146
  page = poppler_document_get_page_by_label (document, argv[2]);
  if (page == NULL)
    FAIL ("page not found");

147
  poppler_page_get_size (page, &width, &height);
148
  printf ("\tpage size:\t%f inches by %f inches\n", width / 72, height / 72);
149

150
151
152
153
154
155
  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
156
      printf ("\tthumbnail:\tsaved as thumb.png\n");
157
158
159
    g_object_unref (G_OBJECT (thumb));
  }
  else
160
    printf ("\tthumbnail:\tno thumbnail for page\n");
161

162
  g_object_get (page, "label", &label, NULL);
163
  printf ("\tpage label:\t%s\n", label);
164
165
  g_free (label);

166
  pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 220, 220);
167
  gdk_pixbuf_fill (pixbuf, 0x00106000);
168
  poppler_page_render_to_pixbuf (page, 100, 100, 200, 200, 1, 0, pixbuf);
169
170

  gdk_pixbuf_save (pixbuf, "slice.png", "png", &error, NULL);
171
  printf ("\tslice:\t\tsaved 200x200 slice at (100, 100) as slice.png\n");
172
173
174
  if (error != NULL)
    FAIL (error->message);

175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
  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);
    }

192
  list = poppler_page_find_text (page, "Bitwise");
193
194
  printf ("\n");  
  printf ("\tFound text \"Bitwise\" at positions:\n");
195
196
197
198
199
200
  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);
    }
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220

  if (poppler_document_has_attachments (document))
    {
      int i = 0;

      g_print ("Attachments found:\n\n");

      list = poppler_document_get_attachments (document);
      for (l = list; l; l = l->next)
	{
	  PopplerAttachment *attachment;
	  char *name;

	  name = g_strdup_printf ("/tmp/attach%d", i);
	  attachment = l->data;
	  g_print ("\tname: %s\n", attachment->name);
	  g_print ("\tdescription: %s\n\n", attachment->description);
	  poppler_attachment_save (attachment, name, NULL);
	  i++;
	}
221
      g_list_foreach (list, (GFunc) g_object_unref, NULL);
222
223
224
225
      g_list_free (list);
    }
  else
    g_print ("no attachment\n");
226
227
228

  g_object_unref (G_OBJECT (page));

229
  g_object_unref (G_OBJECT (document));
230
231

  return 0;
232
}