1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <time.h>
5 #include "config.h"
6 #include "poppler.h"
7
8 #define FAIL(msg) \
9 do { fprintf (stderr, "FAIL: %s\n", msg); exit (-1); } while (0)
10
11 static gchar *
poppler_format_date(GTime utime)12 poppler_format_date (GTime utime)
13 {
14 time_t time = (time_t) utime;
15 char s[256];
16 const char *fmt_hack = "%c";
17 size_t len;
18 #ifdef HAVE_LOCALTIME_R
19 struct tm t;
20 if (time == 0 || !localtime_r (&time, &t)) return NULL;
21 len = strftime (s, sizeof (s), fmt_hack, &t);
22 #else
23 struct tm *t;
24 if (time == 0 || !(t = localtime (&time)) ) return NULL;
25 len = strftime (s, sizeof (s), fmt_hack, t);
26 #endif
27
28 if (len == 0 || s[0] == '\0') return NULL;
29
30 return g_locale_to_utf8 (s, -1, NULL, NULL, NULL);
31 }
32
33 static void
print_index(PopplerIndexIter * iter,gint deph)34 print_index (PopplerIndexIter *iter, gint deph)
35 {
36 do
37 {
38 PopplerAction *action;
39 PopplerIndexIter *child;
40 int i;
41
42 action = poppler_index_iter_get_action (iter);
43 for (i = 0; i < deph; i++)
44 g_print (" ");
45 g_print ("+ %s\n", action->any.title);
46 poppler_action_free (action);
47 child = poppler_index_iter_get_child (iter);
48 if (child)
49 print_index (child, deph + 1);
50 poppler_index_iter_free (child);
51 }
52 while (poppler_index_iter_next (iter));
53 }
54
55 static void
print_layers(PopplerLayersIter * iter,gint deph)56 print_layers (PopplerLayersIter *iter, gint deph)
57 {
58 do
59 {
60 PopplerLayersIter *child;
61 PopplerLayer *layer;
62 gint i;
63
64 for (i = 0; i < deph; i++)
65 g_print (" ");
66
67 layer = poppler_layers_iter_get_layer (iter);
68 if (layer)
69 {
70 g_print ("+ %s (%s)\n", poppler_layer_get_title (layer),
71 poppler_layer_is_visible (layer) ?
72 "Visible" : "Hidden");
73 g_object_unref (layer);
74 }
75
76 child = poppler_layers_iter_get_child (iter);
77 if (child)
78 {
79 gchar *title;
80
81 title = poppler_layers_iter_get_title (iter);
82 if (title)
83 {
84 g_print ("+ %s\n", title);
85 g_free (title);
86 }
87 print_layers (child, deph + 1);
88 }
89 poppler_layers_iter_free (child);
90 }
91 while (poppler_layers_iter_next (iter));
92 }
93
94 static void
print_document_info(PopplerDocument * document)95 print_document_info (PopplerDocument *document)
96 {
97 gchar *title, *format, *author, *subject, *keywords, *creator, *producer, *linearized;
98 GTime creation_date, mod_date;
99 gchar *strdate;
100 PopplerPageLayout layout;
101 PopplerPageMode mode;
102 PopplerViewerPreferences view_prefs;
103 PopplerPermissions permissions;
104 PopplerFontInfo *font_info;
105 PopplerFontsIter *fonts_iter;
106 PopplerIndexIter *index_iter;
107 GEnumValue *enum_value;
108
109 g_object_get (document,
110 "title", &title,
111 "format", &format,
112 "author", &author,
113 "subject", &subject,
114 "keywords", &keywords,
115 "creation-date", &creation_date,
116 "mod-date", &mod_date,
117 "creator", &creator,
118 "producer", &producer,
119 "linearized", &linearized,
120 "page-mode", &mode,
121 "page-layout", &layout,
122 "viewer-preferences", &view_prefs,
123 "permissions", &permissions,
124 NULL);
125
126 printf ("\t---------------------------------------------------------\n");
127 printf ("\tDocument Metadata\n");
128 printf ("\t---------------------------------------------------------\n");
129 if (title) printf ("\ttitle:\t\t%s\n", title);
130 if (format) printf ("\tformat:\t\t%s\n", format);
131 if (author) printf ("\tauthor:\t\t%s\n", author);
132 if (subject) printf ("\tsubject:\t%s\n", subject);
133 if (keywords) printf ("\tkeywords:\t%s\n", keywords);
134 if (creator) printf ("\tcreator:\t%s\n", creator);
135 if (producer) printf ("\tproducer:\t%s\n", producer);
136 if (linearized) printf ("\tlinearized:\t%s\n", linearized);
137
138 enum_value = g_enum_get_value ((GEnumClass *) g_type_class_peek (POPPLER_TYPE_PAGE_MODE), mode);
139 g_print ("\tpage mode:\t%s\n", enum_value->value_name);
140 enum_value = g_enum_get_value ((GEnumClass *) g_type_class_peek (POPPLER_TYPE_PAGE_LAYOUT), layout);
141 g_print ("\tpage layout:\t%s\n", enum_value->value_name);
142
143 strdate = poppler_format_date (creation_date);
144 if (strdate)
145 {
146 g_print ("\tcreation date:\t%s\n", strdate);
147 g_free (strdate);
148 }
149 strdate = poppler_format_date (mod_date);
150 if (strdate)
151 {
152 g_print ("\tmodified date:\t%s\n", strdate);
153 g_free (strdate);
154 }
155
156 g_print ("\tfonts:\n");
157 font_info = poppler_font_info_new (document);
158 while (poppler_font_info_scan (font_info, 20, &fonts_iter)) {
159 if (fonts_iter) {
160 do {
161 g_print ("\t\t\t%s\n", poppler_fonts_iter_get_name (fonts_iter));
162 } while (poppler_fonts_iter_next (fonts_iter));
163 poppler_fonts_iter_free (fonts_iter);
164 }
165 }
166 g_object_unref (font_info);
167
168 index_iter = poppler_index_iter_new (document);
169 if (index_iter)
170 {
171 g_print ("\tindex:\n");
172 print_index (index_iter, 0);
173 poppler_index_iter_free (index_iter);
174 }
175
176 printf ("\t---------------------------------------------------------\n");
177 printf ("\tDocument Permissions\n");
178 printf ("\t---------------------------------------------------------\n");
179
180 printf ("\tOk to Print: %s\n",
181 permissions & POPPLER_PERMISSIONS_OK_TO_PRINT ? "Yes" : "No");
182 printf ("\tOk to Modify: %s\n",
183 permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY ? "Yes" : "No");
184 printf ("\tOk to Copy: %s\n",
185 permissions & POPPLER_PERMISSIONS_OK_TO_COPY ? "Yes" : "No");
186 printf ("\tOk to Add Notes: %s\n",
187 permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES ? "Yes" : "No");
188 printf ("\tOk to Fill Forms: %s\n",
189 permissions & POPPLER_PERMISSIONS_OK_TO_FILL_FORM ? "Yes" : "No");
190
191 printf ("\n");
192
193 /* FIXME: print out the view prefs when we support it */
194
195 g_free (title);
196 g_free (format);
197 g_free (author);
198 g_free (subject);
199 g_free (keywords);
200 g_free (creator);
201 g_free (producer);
202 g_free (linearized);
203 }
204
205 static const gchar *
transition_effect_name(PopplerPageTransitionType type)206 transition_effect_name (PopplerPageTransitionType type)
207 {
208 switch (type)
209 {
210 case POPPLER_PAGE_TRANSITION_REPLACE:
211 return "Replace";
212 case POPPLER_PAGE_TRANSITION_SPLIT:
213 return "Split";
214 case POPPLER_PAGE_TRANSITION_BLINDS:
215 return "Blinds";
216 case POPPLER_PAGE_TRANSITION_BOX:
217 return "Box";
218 case POPPLER_PAGE_TRANSITION_WIPE:
219 return "Wipe";
220 case POPPLER_PAGE_TRANSITION_DISSOLVE:
221 return "Dissolve";
222 case POPPLER_PAGE_TRANSITION_GLITTER:
223 return "Glitter";
224 case POPPLER_PAGE_TRANSITION_FLY:
225 return "Fly";
226 case POPPLER_PAGE_TRANSITION_PUSH:
227 return "Push";
228 case POPPLER_PAGE_TRANSITION_COVER:
229 return "Cover";
230 case POPPLER_PAGE_TRANSITION_UNCOVER:
231 return "Uncover";
232 case POPPLER_PAGE_TRANSITION_FADE:
233 return "Fade";
234 }
235
236 return "Unknown";
237 }
238
239 static void
print_page_transition(PopplerPageTransition * transition)240 print_page_transition (PopplerPageTransition *transition)
241 {
242 printf ("\t\tEffect: %s\n", transition_effect_name (transition->type));
243 printf ("\t\tAlignment: %s\n",
244 transition->alignment == POPPLER_PAGE_TRANSITION_HORIZONTAL ?
245 "Horizontal" : "Vertical");
246 printf ("\t\tDirection: %s\n",
247 transition->direction == POPPLER_PAGE_TRANSITION_INWARD ?
248 "Inward" : "Outward");
249 printf ("\t\tDuration: %d\n", transition->duration);
250 printf ("\t\tAngle: %d\n", transition->angle);
251 printf ("\t\tScale: %.2f\n", transition->scale);
252 printf ("\t\tRectangular: %s\n", transition->rectangular ? "Yes" : "No");
253 }
254
255 static void
form_field_text_print(PopplerFormField * field)256 form_field_text_print (PopplerFormField *field)
257 {
258 PopplerFormTextType type;
259 gchar *text;
260
261 type = poppler_form_field_text_get_text_type (field);
262 printf ("\t\tType:\t\tText\n");
263 printf ("\t\tMultiline:\t%s\n",
264 type == POPPLER_FORM_TEXT_MULTILINE ? "Yes" : "No");
265 printf ("\t\tFileSelect:\t%s\n",
266 type == POPPLER_FORM_TEXT_FILE_SELECT ? "Yes" : "No");
267 printf ("\t\tDoSpellCheck:\t%s\n",
268 poppler_form_field_text_do_spell_check (field) ? "Yes" : "No");
269 printf ("\t\tDoScroll:\t%s\n",
270 poppler_form_field_text_do_scroll (field) ? "Yes" : "No");
271 printf ("\t\tIsRichText:\t%s\n",
272 poppler_form_field_text_is_rich_text (field) ? "Yes" : "No");
273 printf ("\t\tPassword:\t%s\n",
274 poppler_form_field_text_is_password (field) ? "Yes" : "No");
275 printf ("\t\tMaxLen:\t\t%d\n", poppler_form_field_text_get_max_len (field));
276 text = poppler_form_field_text_get_text (field);
277 printf ("\t\tContent:\t%s\n", text ? text : "");
278 g_free (text);
279 }
280
281 static void
form_field_button_print(PopplerFormField * field)282 form_field_button_print (PopplerFormField *field)
283 {
284 PopplerFormButtonType button_type;
285 const gchar *button_type_str;
286
287 button_type = poppler_form_field_button_get_button_type (field);
288
289 switch (button_type)
290 {
291 case POPPLER_FORM_BUTTON_PUSH:
292 button_type_str = "Push";
293 break;
294 case POPPLER_FORM_BUTTON_CHECK:
295 button_type_str = "Check box";
296 break;
297 case POPPLER_FORM_BUTTON_RADIO:
298 button_type_str = "Radio Button";
299 break;
300 default:
301 g_assert_not_reached ();
302 }
303
304 printf ("\t\tType:\t\tButton\n");
305 printf ("\t\tButton type:\t%s\n", button_type_str);
306 if (button_type != POPPLER_FORM_BUTTON_PUSH)
307 printf ("\t\tState:\t\t%s\n",
308 poppler_form_field_button_get_state (field) ? "Active" : "Inactive");
309 }
310
311 static void
form_field_choice_print(PopplerFormField * field)312 form_field_choice_print (PopplerFormField *field)
313 {
314 gint i, n_items;
315
316 printf ("\t\tType:\t\tChoice\n");
317 printf ("\t\tSubType:\t%s\n",
318 poppler_form_field_choice_get_choice_type (field) == POPPLER_FORM_CHOICE_COMBO ?
319 "Combo" : "List");
320 printf ("\t\tEditable:\t%s\n",
321 poppler_form_field_choice_is_editable (field) ? "Yes" : "No");
322 printf ("\t\tCan select multiple: %s\n",
323 poppler_form_field_choice_can_select_multiple (field) ? "Yes" : "No");
324 printf ("\t\tDoSpellCheck:\t%s\n",
325 poppler_form_field_choice_do_spell_check (field) ? "Yes" : "No");
326 printf ("\t\tCommit on change: %s\n",
327 poppler_form_field_choice_commit_on_change (field) ? "Yes" : "No");
328
329 n_items = poppler_form_field_choice_get_n_items (field);
330 for (i = 0; i < n_items; i++)
331 {
332 gchar *item;
333
334 item = poppler_form_field_choice_get_item (field, i);
335 printf ("\t\t\tItem %d: %s %s\n", i, item ? item : "",
336 poppler_form_field_choice_is_item_selected (field, i) ?
337 "(selected)" : "");
338 g_free (item);
339 }
340 }
341
342 static void
form_field_print(PopplerFormField * field)343 form_field_print (PopplerFormField *field)
344 {
345 printf ("\t\tFont Size:\t%.2f\n",
346 poppler_form_field_get_font_size (field));
347 printf ("\t\tReadOnly:\t%s\n",
348 poppler_form_field_is_read_only (field) ? "Yes" : "No");
349
350 switch (poppler_form_field_get_field_type (field))
351 {
352 case POPPLER_FORM_FIELD_TEXT:
353 form_field_text_print (field);
354 break;
355 case POPPLER_FORM_FIELD_BUTTON:
356 form_field_button_print (field);
357 break;
358 case POPPLER_FORM_FIELD_CHOICE:
359 form_field_choice_print (field);
360 break;
361 default:
362 printf ("\t\tUnknown form field\n");
363 }
364 printf ("\n");
365 }
366
367 static void
annot_print(PopplerAnnot * annot)368 annot_print (PopplerAnnot *annot)
369 {
370 GEnumValue *enum_value;
371 gchar *text;
372
373 enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_ANNOT_TYPE),
374 poppler_annot_get_annot_type (annot));
375 g_print ("\t\tType: %s\n", enum_value->value_name);
376 text = poppler_annot_get_contents (annot);
377 g_print ("\t\tContents: %s\n", text);
378 g_free (text);
379 printf ("\n");
380 }
381
main(int argc,char * argv[])382 int main (int argc, char *argv[])
383 {
384 PopplerDocument *document;
385 PopplerBackend backend;
386 PopplerPage *page;
387 PopplerPageTransition *transition;
388 PopplerFormField *field;
389 GEnumValue *enum_value;
390 char *label;
391 GError *error;
392 GdkPixbuf *pixbuf, *thumb;
393 double width, height;
394 GList *list, *l;
395 char *text;
396 double duration;
397 gint num_images;
398 gint num_forms;
399 gint num_links;
400 gint num_annots;
401 gint form_id = 0;
402 PopplerLayersIter *layers_iter;
403
404 if (argc != 3)
405 FAIL ("usage: test-poppler-glib file://FILE PAGE");
406
407 g_type_init ();
408
409 g_print ("Poppler version %s\n", poppler_get_version ());
410 backend = poppler_get_backend ();
411 enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_BACKEND), backend);
412 g_print ("Backend is %s\n", enum_value->value_name);
413
414 error = NULL;
415 document = poppler_document_new_from_file (argv[1], NULL, &error);
416 if (document == NULL)
417 FAIL (error->message);
418
419 print_document_info (document);
420
421 page = poppler_document_get_page_by_label (document, argv[2]);
422 if (page == NULL)
423 FAIL ("page not found");
424
425 poppler_page_get_size (page, &width, &height);
426 printf ("\tpage size:\t%f inches by %f inches\n", width / 72, height / 72);
427
428 duration = poppler_page_get_duration (page);
429 if (duration > 0)
430 printf ("\tpage duration:\t%f second(s)\n", duration);
431 else
432 printf ("\tpage duration:\tno duration for page\n");
433
434 transition = poppler_page_get_transition (page);
435 if (transition) {
436 printf ("\tpage transition:\n");
437 print_page_transition (transition);
438 poppler_page_transition_free (transition);
439 } else {
440 printf ("\tpage transition:no transition effect for page\n");
441 }
442
443 thumb = poppler_page_get_thumbnail_pixbuf (page);
444 if (thumb != NULL) {
445 gdk_pixbuf_save (thumb, "thumb.png", "png", &error, NULL);
446 if (error != NULL)
447 FAIL (error->message);
448 else
449 printf ("\tthumbnail:\tsaved as thumb.png\n");
450 g_object_unref (G_OBJECT (thumb));
451 }
452 else
453 printf ("\tthumbnail:\tno thumbnail for page\n");
454
455 g_object_get (page, "label", &label, NULL);
456 printf ("\tpage label:\t%s\n", label);
457 g_free (label);
458
459 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 220, 220);
460 gdk_pixbuf_fill (pixbuf, 0x00106000);
461 poppler_page_render_to_pixbuf (page, 100, 100, 200, 200, 1, 0, pixbuf);
462
463 gdk_pixbuf_save (pixbuf, "slice.png", "png", &error, NULL);
464 printf ("\tslice:\t\tsaved 200x200 slice at (100, 100) as slice.png\n");
465 if (error != NULL) {
466 FAIL (error->message);
467 g_error_free (error);
468 }
469
470 g_object_unref (G_OBJECT (pixbuf));
471
472 list = poppler_page_get_link_mapping (page);
473 num_links = g_list_length (list);
474 if (num_links > 0)
475 printf ("\tFound %d links at positions:\n", num_links);
476 else
477 printf ("\tNo links found\n");
478
479 for (l = list; l != NULL; l = l->next)
480 {
481 PopplerLinkMapping *mapping = (PopplerLinkMapping *)l->data;
482
483 printf ("\t\t(%f, %f) - (%f, %f)\n",
484 mapping->area.x1,
485 mapping->area.y1,
486 mapping->area.x2,
487 mapping->area.y2);
488 enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_ACTION_TYPE),
489 mapping->action->type);
490 g_print ("\t\t\tAction: %s (%d)\n", enum_value->value_name, mapping->action->type);
491 switch (mapping->action->type)
492 {
493 case POPPLER_ACTION_GOTO_DEST:
494 printf("\t\t\tDest title: %s\n", mapping->action->goto_dest.title);
495 printf("\t\t\tNamed dest: %s\n", mapping->action->goto_dest.dest->named_dest);
496 break;
497 default:
498 printf("\t\t\tDetails unimplemented for this action type\n");
499 }
500 }
501 poppler_page_free_link_mapping (list);
502
503 text = poppler_page_get_text (page);
504 if (text)
505 {
506 FILE *file = fopen ("dump.txt", "w");
507 if (file)
508 {
509 fwrite (text, strlen (text), 1, file);
510 fclose (file);
511 }
512 g_free (text);
513 }
514
515 list = poppler_page_find_text (page, "Bitwise");
516 printf ("\n");
517 printf ("\tFound text \"Bitwise\" at positions:\n");
518 for (l = list; l != NULL; l = l->next)
519 {
520 PopplerRectangle *rect = (PopplerRectangle *)l->data;
521
522 printf (" (%f,%f)-(%f,%f)\n", rect->x1, rect->y1, rect->x2, rect->y2);
523 }
524
525 list = poppler_page_get_image_mapping (page);
526 num_images = g_list_length (list);
527 printf ("\n");
528 if (num_images > 0)
529 printf ("\tFound %d images at positions:\n", num_images);
530 else
531 printf ("\tNo images found\n");
532 for (l = list; l != NULL; l = l->next)
533 {
534 PopplerImageMapping *mapping;
535 cairo_surface_t *image;
536
537 mapping = (PopplerImageMapping *)l->data;
538 printf ("\t\t(%f, %f) - (%f, %f)\n",
539 mapping->area.x1,
540 mapping->area.y1,
541 mapping->area.x2,
542 mapping->area.y2);
543
544 image = poppler_page_get_image (page, mapping->image_id);
545 printf ("\t\tImage: %p\n", image);
546 cairo_surface_destroy (image);
547 }
548 poppler_page_free_image_mapping (list);
549
550 list = poppler_page_get_form_field_mapping (page);
551 num_forms = g_list_length (list);
552 printf ("\n");
553 if (num_forms > 0)
554 printf ("\tFound %d form fields at positions:\n", num_forms);
555 else
556 printf ("\tNo forms fields found\n");
557 for (l = list; l != NULL; l = l->next)
558 {
559 PopplerFormFieldMapping *mapping;
560
561 mapping = (PopplerFormFieldMapping *)l->data;
562
563 form_id = poppler_form_field_get_id (mapping->field);
564
565 printf ("\t\tId: %d: (%f, %f) - (%f, %f)\n",
566 form_id,
567 mapping->area.x1,
568 mapping->area.y1,
569 mapping->area.x2,
570 mapping->area.y2);
571 form_field_print (mapping->field);
572 }
573 poppler_page_free_form_field_mapping (list);
574
575 if (num_forms > 0)
576 {
577 field = poppler_document_get_form_field (document, form_id);
578 printf ("\tForm field for id %d\n", form_id);
579 form_field_print (field);
580 g_object_unref (field);
581 }
582
583 list = poppler_page_get_annot_mapping (page);
584 num_annots = g_list_length (list);
585 if (num_annots > 0)
586 printf ("\tFound %d annotations at positions:\n", num_annots);
587 else
588 printf ("\tNo annotations found\n");
589 for (l = list; l != NULL; l = l->next)
590 {
591 PopplerAnnotMapping *mapping = (PopplerAnnotMapping *)l->data;
592
593 printf ("\t\t(%f, %f) - (%f, %f)\n",
594 mapping->area.x1,
595 mapping->area.y1,
596 mapping->area.x2,
597 mapping->area.y2);
598
599 annot_print (mapping->annot);
600 }
601 poppler_page_free_annot_mapping (list);
602
603 if (poppler_document_has_attachments (document))
604 {
605 int i = 0;
606
607 g_print ("Attachments found:\n\n");
608
609 list = poppler_document_get_attachments (document);
610 for (l = list; l; l = l->next)
611 {
612 PopplerAttachment *attachment;
613 char *filename, *strdate;
614
615 filename = g_strdup_printf ("/tmp/attach%d", i);
616 attachment = (PopplerAttachment *)l->data;
617 g_print ("\tname: %s\n", attachment->name);
618 g_print ("\tdescription: %s\n", attachment->description);
619 g_print ("\tsize: %" G_GSIZE_FORMAT "\n", attachment->size);
620 strdate = poppler_format_date (attachment->ctime);
621 if (strdate)
622 {
623 g_print ("\tcreation date: %s\n", strdate);
624 g_free (strdate);
625 }
626 strdate = poppler_format_date (attachment->mtime);
627 if (strdate)
628 {
629 g_print ("\tmodification date: %s\n", strdate);
630 g_free (strdate);
631 }
632 poppler_attachment_save (attachment, filename, NULL);
633 g_free (filename);
634 g_print ("\n");
635 i++;
636 }
637 g_list_foreach (list, (GFunc) g_object_unref, NULL);
638 g_list_free (list);
639 }
640 else
641 g_print ("\tNo attachments found\n");
642
643 layers_iter = poppler_layers_iter_new (document);
644 if (layers_iter)
645 {
646 g_print ("\tLayers:\n");
647 print_layers (layers_iter, 0);
648 poppler_layers_iter_free (layers_iter);
649 }
650 else
651 g_print ("\tNo layers found\n");
652
653 g_object_unref (G_OBJECT (page));
654 g_object_unref (G_OBJECT (document));
655
656 return 0;
657 }
658