From addbdd3bc39e3ed07259706e896f275a9f35ae9e Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Mon, 10 Feb 2014 16:18:46 +0000 Subject: Bug 695021: Fix pdf_insert_page operation with empty page tree. Patch from Thomas Fach-Pedersen to fix the operation of pdf_insert_page when called with an empty page tree. Many thanks! As noted in the code with a FIXME this currently throws an error. Also, cope with being told to add a page "at" INT_MAX as meaning to add it at the end of the document. Possibly this code should cope with a Root without a Pages entry, or a Pages without a Kids too, but we can fix this in future if it ever becomes a problem. --- source/pdf/pdf-page.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'source/pdf') diff --git a/source/pdf/pdf-page.c b/source/pdf/pdf-page.c index f3c929d2..fd6f71bc 100644 --- a/source/pdf/pdf-page.c +++ b/source/pdf/pdf-page.c @@ -590,11 +590,22 @@ pdf_insert_page(pdf_document *doc, pdf_page *page, int at) { if (count == 0) { - /* TODO: create new page tree? */ - fz_throw(ctx, FZ_ERROR_GENERIC, "empty page tree, cannot insert page"); + pdf_obj *root = pdf_dict_gets(pdf_trailer(doc), "Root"); + parent = pdf_dict_gets(root, "Pages"); + if (!parent) + fz_throw(doc->ctx, FZ_ERROR_GENERIC, "cannot find page tree"); + + kids = pdf_dict_gets(parent, "Kids"); + if (!kids) + fz_throw(doc->ctx, FZ_ERROR_GENERIC, "malformed page tree"); + + pdf_array_insert(kids, page_ref, 0); } else if (at >= count) { + if (at == INT_MAX) + at = count; + if (at > count) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot insert page beyond end of page tree"); -- cgit v1.2.3