New functionality for manipulating boxes

This commit is contained in:
John Whitington 2019-06-26 14:43:24 +01:00
parent 9cb5347618
commit b9576753a8
3 changed files with 116 additions and 14 deletions

View File

@ -2600,7 +2600,7 @@ let set_mediabox xywhlist pdf range =
in in
process_pages crop_page pdf range process_pages crop_page pdf range
let setBox box minx maxx miny maxy pdf range = (*let setBox box minx maxx miny maxy pdf range =
let set_box_page _ page = let set_box_page _ page =
{page with {page with
Pdfpage.rest = Pdfpage.rest =
@ -2608,16 +2608,16 @@ let setBox box minx maxx miny maxy pdf range =
page.Pdfpage.rest box page.Pdfpage.rest box
(Pdf.Array [Pdf.Real minx; Pdf.Real miny; Pdf.Real maxx; Pdf.Real maxy])} (Pdf.Array [Pdf.Real minx; Pdf.Real miny; Pdf.Real maxx; Pdf.Real maxy])}
in in
process_pages set_box_page pdf range process_pages set_box_page pdf range*)
(* \section{Cropping} *) (* \section{Cropping} *)
let crop_pdf xywhlist pdf range = let crop_pdf ?(box="/CropBox") xywhlist pdf range =
let crop_page pagenum page = let crop_page pagenum page =
{page with {page with
Pdfpage.rest = Pdfpage.rest =
(Pdf.add_dict_entry (Pdf.add_dict_entry
page.Pdfpage.rest page.Pdfpage.rest
"/CropBox" box
(let x, y, w, h = List.nth xywhlist (pagenum - 1) in (let x, y, w, h = List.nth xywhlist (pagenum - 1) in
(Pdf.Array (Pdf.Array
[Pdf.Real x; Pdf.Real y; [Pdf.Real x; Pdf.Real y;

View File

@ -305,15 +305,15 @@ val output_page_info : Pdf.t -> int list -> unit
val hasbox : Pdf.t -> int -> string -> bool val hasbox : Pdf.t -> int -> string -> bool
(** [crop_pdf xywhlist pdf range] sets the cropbox on the given pages. *) (** [crop_pdf xywhlist pdf range] sets the cropbox on the given pages. *)
val crop_pdf : (float * float * float * float) list -> Pdf.t -> int list -> Pdf.t val crop_pdf : ?box:string -> (float * float * float * float) list -> Pdf.t -> int list -> Pdf.t
val hard_box : Pdf.t -> int list -> string -> bool -> bool -> Pdf.t val hard_box : Pdf.t -> int list -> string -> bool -> bool -> Pdf.t
(** [set_mediabox xywhlist pdf range] sets the media box on the given pages. *) (** [set_mediabox xywhlist pdf range] sets the media box on the given pages. *)
val set_mediabox : (float * float * float * float) list -> Pdf.t -> int list -> Pdf.t val set_mediabox : (float * float * float * float) list -> Pdf.t -> int list -> Pdf.t
(** [setBox boxname x y w h pdf range] sets the given box on the given pages. *) (*(** [setBox boxname x y w h pdf range] sets the given box on the given pages. *)
val setBox : string -> float -> float -> float -> float -> Pdf.t -> int list -> Pdf.t val setBox : string -> float -> float -> float -> float -> Pdf.t -> int list -> Pdf.t*)
(** Remove any cropping from the given pages. *) (** Remove any cropping from the given pages. *)
val remove_cropping_pdf : Pdf.t -> int list -> Pdf.t val remove_cropping_pdf : Pdf.t -> int list -> Pdf.t

View File

@ -3,7 +3,7 @@ let demo = false
let noncomp = false let noncomp = false
let major_version = 2 let major_version = 2
let minor_version = 3 let minor_version = 3
let version_date = "(devel, build of 31st July 2018)" let version_date = "(devel, build of 4th June 2019)"
open Pdfutil open Pdfutil
open Pdfio open Pdfio
@ -112,7 +112,13 @@ type op =
| Compress | Compress
| Decompress | Decompress
| Crop | Crop
| Trim
| Bleed
| Art
| RemoveCrop | RemoveCrop
| RemoveArt
| RemoveTrim
| RemoveBleed
| CopyCropBoxToMediaBox | CopyCropBoxToMediaBox
| CopyBox | CopyBox
| MediaBox | MediaBox
@ -271,6 +277,12 @@ let string_of_op = function
| RemoveDictEntry _ -> "RemoveDictEntry" | RemoveDictEntry _ -> "RemoveDictEntry"
| ListSpotColours -> "ListSpotColours" | ListSpotColours -> "ListSpotColours"
| RemoveClipping -> "RemoveClipping" | RemoveClipping -> "RemoveClipping"
| Trim -> "Trim"
| Art -> "Art"
| Bleed -> "Bleed"
| RemoveArt -> "RemoveArt"
| RemoveTrim -> "RemoveTrim"
| RemoveBleed -> "RemoveBleed"
(* Inputs: filename, pagespec. *) (* Inputs: filename, pagespec. *)
type input_kind = type input_kind =
@ -546,15 +558,12 @@ let reset_arguments () =
args.labelprefix <- None; args.labelprefix <- None;
args.labelstartval <- 1; args.labelstartval <- 1;
args.squeeze <- false; args.squeeze <- false;
args.producer <- None;
args.creator <- None;
args.embedfonts <- true; args.embedfonts <- true;
args.creator <- None;
args.producer <- None;
args.extract_text_font_size <- None; args.extract_text_font_size <- None;
args.padwith <- None args.padwith <- None
(* Do not reset original_filename or cpdflin or was_encrypted or (* Do not reset original_filename or cpdflin or was_encrypted or
* was_decrypted_with_owner or recrypt, since we want these to work across ANDs. *) * was_decrypted_with_owner or recrypt or producer or creator, since we want
* these to work across ANDs. *)
let get_pagespec () = let get_pagespec () =
match args.inputs with match args.inputs with
@ -618,7 +627,7 @@ let banned banlist = function
| AddPageLabels | RemovePageLabels -> mem Pdfcrypt.NoAssemble banlist | AddPageLabels | RemovePageLabels -> mem Pdfcrypt.NoAssemble banlist
| CSP1|CSP3|TwoUp|TwoUpStack|RemoveBookmarks|AddRectangle|RemoveText| | CSP1|CSP3|TwoUp|TwoUpStack|RemoveBookmarks|AddRectangle|RemoveText|
Draft|Shift|Scale|ScaleToFit|RemoveAttachedFiles| Draft|Shift|Scale|ScaleToFit|RemoveAttachedFiles|
RemoveAnnotations|RemoveFonts|Crop|RemoveCrop| RemoveAnnotations|RemoveFonts|Crop|RemoveCrop|Trim|RemoveTrim|Bleed|RemoveBleed|Art|RemoveArt|
CopyCropBoxToMediaBox|CopyBox|MediaBox|HardBox _|SetTrapped|SetUntrapped|Presentation| CopyCropBoxToMediaBox|CopyBox|MediaBox|HardBox _|SetTrapped|SetUntrapped|Presentation|
BlackText|BlackLines|BlackFills|CopyFont _|CSP2 _|StampOn _|StampUnder _| BlackText|BlackLines|BlackFills|CopyFont _|CSP2 _|StampOn _|StampUnder _|
AddText _|ScaleContents _|AttachFile _|CopyAnnotations _|SetMetadata _| AddText _|ScaleContents _|AttachFile _|CopyAnnotations _|SetMetadata _|
@ -1011,6 +1020,18 @@ let setcrop s =
setop Crop (); setop Crop ();
args.rectangle <- s args.rectangle <- s
let settrim s =
setop Trim ();
args.rectangle <- s
let setbleed s =
setop Bleed ();
args.rectangle <- s
let setart s =
setop Art ();
args.rectangle <- s
let setmediabox s = let setmediabox s =
setop MediaBox (); setop MediaBox ();
args.rectangle <- s args.rectangle <- s
@ -1682,12 +1703,36 @@ and specs =
("-crop", ("-crop",
Arg.String setcrop, Arg.String setcrop,
" Crop specified pages"); " Crop specified pages");
("-cropbox",
Arg.String setcrop,
" Crop specified pages (synonym for -crop)");
("-artbox",
Arg.String setart,
" Set art box for specified pages");
("-bleedbox",
Arg.String setbleed,
" Set bleed box for specified pages");
("-trimbox",
Arg.String settrim,
" Set trim box for specified pages");
("-hard-box", ("-hard-box",
Arg.String sethardbox, Arg.String sethardbox,
" Hard crop specified pages to the given box"); " Hard crop specified pages to the given box");
("-remove-crop", ("-remove-crop",
Arg.Unit (setop RemoveCrop), Arg.Unit (setop RemoveCrop),
" Remove cropping on specified pages"); " Remove cropping on specified pages");
("-remove-cropbox",
Arg.Unit (setop RemoveCrop),
" Synonym for -remove-crop");
("-remove-trimbox",
Arg.Unit (setop RemoveTrim),
" Remove trim box on specified pages");
("-remove-bleedbox",
Arg.Unit (setop RemoveBleed),
" Remove bleed box on specified pages");
("-remove-artbox",
Arg.Unit (setop RemoveArt),
" Remove art box on specified pages");
("-copy-cropbox-to-mediabox", ("-copy-cropbox-to-mediabox",
Arg.Unit (setop CopyCropBoxToMediaBox), Arg.Unit (setop CopyCropBoxToMediaBox),
""); (* Undocumented now, since /frombox, /tobox now used *) ""); (* Undocumented now, since /frombox, /tobox now used *)
@ -3429,6 +3474,36 @@ let go () =
write_pdf false pdf write_pdf false pdf
| _ -> error "crop: bad command line" | _ -> error "crop: bad command line"
end end
| Some Art ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
let pdf = get_single_pdf (Some Art) false in
let xywhlist = parse_rectangles pdf args.rectangle in
let range = parse_pagespec pdf pagespec in
let pdf = Cpdf.crop_pdf ~box:"/ArtBox" xywhlist pdf range in
write_pdf false pdf
| _ -> error "crop: bad command line"
end
| Some Bleed ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
let pdf = get_single_pdf (Some Bleed) false in
let xywhlist = parse_rectangles pdf args.rectangle in
let range = parse_pagespec pdf pagespec in
let pdf = Cpdf.crop_pdf ~box:"/BleedBox" xywhlist pdf range in
write_pdf false pdf
| _ -> error "crop: bad command line"
end
| Some Trim ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
let pdf = get_single_pdf (Some Trim) false in
let xywhlist = parse_rectangles pdf args.rectangle in
let range = parse_pagespec pdf pagespec in
let pdf = Cpdf.crop_pdf ~box:"/TrimBox" xywhlist pdf range in
write_pdf false pdf
| _ -> error "crop: bad command line"
end
| Some MediaBox -> | Some MediaBox ->
begin match args.inputs, args.out with begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ -> | (_, pagespec, _, _, _, _)::_, _ ->
@ -3485,6 +3560,33 @@ let go () =
write_pdf false pdf write_pdf false pdf
| _ -> error "remove-crop: bad command line" | _ -> error "remove-crop: bad command line"
end end
| Some RemoveArt ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
let pdf = get_single_pdf (Some RemoveArt) false in
let range = parse_pagespec pdf pagespec in
let pdf = Cpdf.remove_art_pdf pdf range in
write_pdf false pdf
| _ -> error "remove-crop: bad command line"
end
| Some RemoveTrim ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
let pdf = get_single_pdf (Some RemoveTrim) false in
let range = parse_pagespec pdf pagespec in
let pdf = Cpdf.remove_trim_pdf pdf range in
write_pdf false pdf
| _ -> error "remove-crop: bad command line"
end
| Some RemoveBleed ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
let pdf = get_single_pdf (Some RemoveBleed) false in
let range = parse_pagespec pdf pagespec in
let pdf = Cpdf.remove_bleed_pdf pdf range in
write_pdf false pdf
| _ -> error "remove-crop: bad command line"
end
| Some CopyCropBoxToMediaBox -> | Some CopyCropBoxToMediaBox ->
begin match args.inputs, args.out with begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ -> | (_, pagespec, _, _, _, _)::_, _ ->