-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
74 lines (62 loc) · 1.83 KB
/
main.c
1
2
3
4
5
6
7
8
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <mupdf/fitz.h>
#include <json-c/json.h>
void printOutline(fz_context *ctx, fz_outline *outline, int level, struct json_object* parent)
{
struct json_object* titleObj = json_object_new_string(outline->title);
json_object_array_add(parent, titleObj);
if (outline->down != NULL) {
struct json_object* childrenObj = json_object_new_array();
json_object_array_add(parent, childrenObj);
printOutline(ctx, outline->down, level + 1, childrenObj);
}
if (outline->next != NULL) {
printOutline(ctx, outline->next, level, parent);
}
}
int main(int argc, char *argv[])
{
fz_context *ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
fz_try(ctx)
{
fz_register_document_handlers(ctx);
}
fz_catch(ctx)
{
printf("Failed to register document handlers: %s\n", fz_caught_message(ctx));
fz_drop_context(ctx);
return 1;
}
fz_document *doc = NULL;
fz_try(ctx)
{
doc = fz_open_document(ctx, argv[1]);
}
fz_catch(ctx)
{
printf("Failed to open document: %s\n", fz_caught_message(ctx));
fz_drop_context(ctx);
return 1;
}
fz_outline *outline = NULL;
fz_try(ctx)
{
outline = fz_load_outline(ctx, doc);
}
fz_catch(ctx)
{
printf("Failed to load outline: %s\n", fz_caught_message(ctx));
fz_drop_document(ctx, doc);
fz_drop_context(ctx);
return 1;
}
struct json_object* rootObj = json_object_new_array();
printOutline(ctx, outline, 0, rootObj);
const char* jsonString = json_object_to_json_string_ext(rootObj, JSON_C_TO_STRING_PRETTY);
printf("%s\n", jsonString);
json_object_put(rootObj);
fz_drop_outline(ctx, outline);
fz_drop_document(ctx, doc);
fz_drop_context(ctx);
return 0;
}