2023-11-15 18:26:43 +01:00
|
|
|
open Pdfutil
|
|
|
|
open Cpdferror
|
|
|
|
|
2023-11-20 14:21:44 +01:00
|
|
|
(* FIXME Resources - what to do with each. Bookmarks? Annotations - must be duplicated... *)
|
|
|
|
(* FIXME Test how the sharing affects other cpdf operations - especially with -fast! *)
|
2023-11-16 19:18:11 +01:00
|
|
|
|
2023-11-20 11:53:51 +01:00
|
|
|
(* Chop a single page into pieces. We prefer the cropbox when available. We set
|
|
|
|
mediabox only, and delete any other boxes. *)
|
2023-11-20 14:21:44 +01:00
|
|
|
(* FIXME btt / rtl *)
|
2023-11-20 15:12:33 +01:00
|
|
|
let get_box pdf page =
|
|
|
|
match Pdf.lookup_direct pdf "/CropBox" page.Pdfpage.rest with
|
|
|
|
| Some r -> Pdf.parse_rectangle pdf r
|
|
|
|
| None -> Pdf.parse_rectangle pdf page.Pdfpage.mediabox
|
|
|
|
|
|
|
|
let erase_boxes d =
|
|
|
|
let f = Pdf.remove_dict_entry in
|
|
|
|
f (f (f (f d "/CropBox") "/BleedBox") "/TrimBox") "/ArtBox"
|
|
|
|
|
|
|
|
let chop_boxes pdf x y p =
|
2023-11-20 12:29:17 +01:00
|
|
|
if x < 1 || y < 1 then Cpdferror.error "chop_boxes bad specification" else
|
2023-11-20 15:12:33 +01:00
|
|
|
let move_page mx my p w h dx dy =
|
|
|
|
(*Printf.printf "move_page by %f %f\n" dx dy;*)
|
|
|
|
let nminx, nminy, nmaxx, nmaxy = (mx +. dx, my +. dy, mx +. w +. dx, my +. h +. dy) in
|
|
|
|
(*Printf.printf "new box: %f, %f, %f, %f\n" nminx nminy nmaxx nmaxy;*)
|
|
|
|
{p with
|
|
|
|
Pdfpage.mediabox = Pdf.Array [Pdf.Real nminx; Pdf.Real nminy; Pdf.Real nmaxx; Pdf.Real nmaxy];
|
|
|
|
Pdfpage.rest = erase_boxes p.Pdfpage.rest}
|
2023-11-20 14:21:44 +01:00
|
|
|
in
|
2023-11-20 15:12:33 +01:00
|
|
|
let minx, miny, maxx, maxy = get_box pdf p in
|
|
|
|
(*Printf.printf "minx, miny, maxx, maxy = %f, %f, %f, %f\n" minx miny maxx maxy;*)
|
2023-11-20 12:29:17 +01:00
|
|
|
let w, h = (maxx -. minx) /. float_of_int x, (maxy -. miny) /. float_of_int y in
|
|
|
|
let ps = ref [] in
|
2023-11-20 14:21:44 +01:00
|
|
|
for ty = y - 1 downto 0 do
|
|
|
|
for tx = 0 to x - 1 do
|
2023-11-20 15:12:33 +01:00
|
|
|
ps =| move_page minx miny p w h (w *. float_of_int tx) (h *. float_of_int ty)
|
2023-11-20 12:29:17 +01:00
|
|
|
done
|
|
|
|
done;
|
|
|
|
rev !ps
|
2023-11-20 11:53:51 +01:00
|
|
|
|
|
|
|
(* 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
|
2023-11-20 15:12:33 +01:00
|
|
|
(fun n p -> if mem n range then (chop_boxes pdf x y p) else [p])
|
2023-11-20 11:53:51 +01:00
|
|
|
(ilist 1 (Pdfpage.endpage pdf))
|
|
|
|
pages)
|
|
|
|
in
|
|
|
|
let changes = [] in (* FIXME *)
|
|
|
|
Pdfpage.change_pages ~changes true pdf pages
|