From f573ba8e0981ea174c6879f570761e93815186a4 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Mon, 20 Nov 2023 12:53:51 +0200 Subject: [PATCH] Beginning chop implementation --- cpdfchop.ml | 26 ++++++++++++++++++-------- cpdfcommand.ml | 4 +++- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/cpdfchop.ml b/cpdfchop.ml index cbd9703..8b9b0c9 100644 --- a/cpdfchop.ml +++ b/cpdfchop.ml @@ -1,12 +1,22 @@ open Pdfutil open Cpdferror -(* Prevent duplication of content by sharing content streams *) -(* Resources - patterns etc. - pattern matrices *) -(* Bookmarks: merge/duplicate them *) -(* Annotations: merge/duplicate them *) -(* Page boxes - mediabox or cropbox used? Output modifies all boxes? *) -(* fast/slow *) +(* Resources - what to do with each. Bookmarks? Annotations - must be duplicated... *) +(* Test how the sharing affects other cpdf operations. *) -(* Chop pages into pieces *) -let chop ~x ~y pdf range = pdf +(* Chop a single page into pieces. We prefer the cropbox when available. We set + mediabox only, and delete any other boxes. *) +let chop_boxes x y p = [p; p] + +(* Chop pages in the range into pieces *) +let chop ~x ~y pdf range = + let pages = Pdfpage.pages_of_pagetree pdf in + let pages = + flatten + (map2 + (fun n p -> if mem n range then (chop_boxes x y p) else [p]) + (ilist 1 (Pdfpage.endpage pdf)) + pages) + in + let changes = [] in (* FIXME *) + Pdfpage.change_pages ~changes true pdf pages diff --git a/cpdfcommand.ml b/cpdfcommand.ml index 778ca84..e78692f 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -4379,7 +4379,9 @@ let go () = in Cpdfcomposition.show_composition filesize json pdf | Some (Chop (x, y)) -> - () + let pdf = get_single_pdf args.op false in + let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in + write_pdf false (Cpdfchop.chop ~x ~y pdf range) (* Advise the user if a combination of command line flags makes little sense, or error out if it make no sense at all. *)