diff --git a/pdfio-dict.c b/pdfio-dict.c index 423ada1..1f89c9a 100644 --- a/pdfio-dict.c +++ b/pdfio-dict.c @@ -524,6 +524,36 @@ pdfioDictIterateKeys( } } +// +// 'pdfioDictGetNumKeys()' - Get the number of keys on the selected dictionary +// + +size_t // O - Number of keys +pdfioDictGetNumKeys(pdfio_dict_t *dict) // I - Dictionary +{ + return (dict->num_pairs); +} + +// +// 'pdfioDictGetKeyByIndex()' - Get name and type of a key by index from a dictionary +// +// Alternative form to enumerate the keys of a dictionary + +bool // O - Value +pdfioDictGetKeyByIndex(pdfio_dict_t *dict, // I - Dictionary + size_t index, // I - Index + pdfio_dictKey_t *key) // I - struct pointer +{ +if (index < dict->num_pairs) + { + if (key){ + key->key = dict->pairs[index].key; + key->type = (pdfio_valtype_t **)(dict->pairs[index].value.type); + return (true); + } + } + return (false); +} // // '_pdfioDictRead()' - Read a dictionary from a PDF file. diff --git a/pdfio.h b/pdfio.h index fb299b9..bfc45f0 100644 --- a/pdfio.h +++ b/pdfio.h @@ -123,6 +123,12 @@ typedef enum pdfio_valtype_e // PDF value types PDFIO_VALTYPE_NUMBER, // Number (integer or real) PDFIO_VALTYPE_STRING // String } pdfio_valtype_t; + // Alternative PDF dict key enumeration +typedef struct pdfio_dictKey_s +{ + const char *key; + pdfio_valtype_t **type; +} pdfio_dictKey_t; // @@ -165,6 +171,8 @@ extern pdfio_obj_t *pdfioDictGetObj(pdfio_dict_t *dict, const char *key) _PDFIO_ extern pdfio_rect_t *pdfioDictGetRect(pdfio_dict_t *dict, const char *key, pdfio_rect_t *rect) _PDFIO_PUBLIC; extern const char *pdfioDictGetString(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; extern pdfio_valtype_t pdfioDictGetType(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; +extern size_t pdfioDictGetNumKeys(pdfio_dict_t *dict) _PDFIO_PUBLIC; +extern bool pdfioDictGetKeyByIndex(pdfio_dict_t *dict, size_t index, pdfio_dictKey_t *keyData) _PDFIO_PUBLIC; extern void pdfioDictIterateKeys(pdfio_dict_t *dict, pdfio_dict_cb_t cb, void *cb_data) _PDFIO_PUBLIC; extern bool pdfioDictSetArray(pdfio_dict_t *dict, const char *key, pdfio_array_t *value) _PDFIO_PUBLIC; extern bool pdfioDictSetBinary(pdfio_dict_t *dict, const char *key, const unsigned char *value, size_t valuelen) _PDFIO_PUBLIC; diff --git a/test_mod b/test_mod new file mode 100755 index 0000000..2e6161a Binary files /dev/null and b/test_mod differ diff --git a/test_mod.c b/test_mod.c new file mode 100644 index 0000000..2ef6155 --- /dev/null +++ b/test_mod.c @@ -0,0 +1,33 @@ +#include +#include +#include "pdfio.h" + +/* +* Usage: ./test_mod [file.pdf] +* +* Compiled as: +* gcc test_mod.c -L. -lpdfio -lm -lz -o test_mod +* +*/ + + +int main (int argc, char **argv) +{ + pdfio_file_t *pdf = pdfioFileOpen(argv[1], NULL, NULL, NULL, NULL); + pdfio_obj_t *obj_page = pdfioFileGetPage(pdf, 0); + pdfio_dict_t *dict_page = pdfioObjGetDict(obj_page); + + size_t num_keys = pdfioDictGetNumKeys(dict_page); + printf("Number of keys in this page: %d\n", num_keys); + + for (unsigned int i = 0; i < num_keys; ++i) + { + pdfio_dictKey_t dict_key; + pdfioDictGetKeyByIndex(dict_page, i, &dict_key); + printf("\t%s (%d)\n", dict_key.key, dict_key.type); + } + + pdfioFileClose(pdf); + + return 0; +}