New functionality: print and remove page labels

This commit is contained in:
John Whitington 2013-10-24 11:21:52 +01:00
parent b9dbd3d367
commit 43c9c43aec
2 changed files with 54 additions and 26 deletions

55
cpdf.ml
View File

@ -1239,32 +1239,35 @@ let split_on_bookmarks pdf level =
(* Output information for each page *) (* Output information for each page *)
let output_page_info pdf = let output_page_info pdf =
let pages = Pdfpage.pages_of_pagetree pdf let pages = Pdfpage.pages_of_pagetree pdf
in let getbox page box = and labels = Pdfpagelabels.read pdf in
if box = "/MediaBox" then let getbox page box =
match page.Pdfpage.mediabox with if box = "/MediaBox" then
| Pdf.Array [a; b; c; d] -> match page.Pdfpage.mediabox with
Printf.sprintf "%f %f %f %f" | Pdf.Array [a; b; c; d] ->
(Pdf.getnum a) (Pdf.getnum b) (Pdf.getnum c) (Pdf.getnum d) Printf.sprintf "%f %f %f %f"
| _ -> "" (Pdf.getnum a) (Pdf.getnum b) (Pdf.getnum c) (Pdf.getnum d)
else | _ -> ""
match Pdf.lookup_direct pdf box page.Pdfpage.rest with else
| Some (Pdf.Array [a; b; c; d]) -> match Pdf.lookup_direct pdf box page.Pdfpage.rest with
Printf.sprintf "%f %f %f %f" | Some (Pdf.Array [a; b; c; d]) ->
(Pdf.getnum a) (Pdf.getnum b) (Pdf.getnum c) (Pdf.getnum d) Printf.sprintf "%f %f %f %f"
| _ -> "" (Pdf.getnum a) (Pdf.getnum b) (Pdf.getnum c) (Pdf.getnum d)
in let rotation page = | _ -> ""
Pdfpage.int_of_rotation page.Pdfpage.rotate and rotation page =
in Pdfpage.int_of_rotation page.Pdfpage.rotate
for pnum = 1 to Pdfpage.endpage pdf do in
let page = select pnum pages in for pnum = 1 to Pdfpage.endpage pdf do
Printf.printf "Page %i:\n" pnum; let page = select pnum pages in
Printf.printf "MediaBox: %s\n" (getbox page "/MediaBox"); Printf.printf "Page %i:\n" pnum;
Printf.printf "CropBox: %s\n" (getbox page "/CropBox"); Printf.printf "Label: %s\n"
Printf.printf "BleedBox: %s\n" (getbox page "/BleedBox"); (try Pdfpagelabels.pagelabeltext_of_pagenumber pnum labels with Not_found -> "");
Printf.printf "TrimBox: %s\n" (getbox page "/TrimBox"); Printf.printf "MediaBox: %s\n" (getbox page "/MediaBox");
Printf.printf "ArtBox: %s\n" (getbox page "/ArtBox"); Printf.printf "CropBox: %s\n" (getbox page "/CropBox");
Printf.printf "Rotation: %i\n" (rotation page) Printf.printf "BleedBox: %s\n" (getbox page "/BleedBox");
done Printf.printf "TrimBox: %s\n" (getbox page "/TrimBox");
Printf.printf "ArtBox: %s\n" (getbox page "/ArtBox");
Printf.printf "Rotation: %i\n" (rotation page)
done
(* Does the page have a defined box e.g "/CropBox" *) (* Does the page have a defined box e.g "/CropBox" *)
let hasbox pdf page boxname = let hasbox pdf page boxname =

View File

@ -152,6 +152,9 @@ type op =
| PrintLinearization | PrintLinearization
| OpenAtPage of int | OpenAtPage of int
| OpenAtPageFit of int | OpenAtPageFit of int
| AddPageLabels of string
| RemovePageLabels
| PrintPageLabels
(* Inputs: filename, pagespec. *) (* Inputs: filename, pagespec. *)
type input_kind = type input_kind =
@ -1187,6 +1190,9 @@ let setopenatpage n =
let setopenatpagefit n = let setopenatpagefit n =
args.op <- Some (OpenAtPageFit n) args.op <- Some (OpenAtPageFit n)
let setaddpagelabels s =
args.op <- Some (AddPageLabels s)
(* Parse a control file, make an argv, and then make Arg parse it. *) (* Parse a control file, make an argv, and then make Arg parse it. *)
let rec make_control_argv_and_parse filename = let rec make_control_argv_and_parse filename =
control_args := !control_args @ parse_control_file filename control_args := !control_args @ parse_control_file filename
@ -1647,6 +1653,15 @@ and specs =
("-copy-id-from", ("-copy-id-from",
Arg.String setcopyid, Arg.String setcopyid,
" Copy one file's ID tag to another"); " Copy one file's ID tag to another");
("-print-page-labels",
Arg.Unit (setop PrintPageLabels),
" Print page labels");
("-remove-page-labels",
Arg.Unit (setop RemovePageLabels),
" Remove page labels");
("-add-page-labels",
Arg.String setaddpagelabels,
" Add or replace page labels");
(* These items are for cpdftk *) (* These items are for cpdftk *)
("-update-info", Arg.String setupdateinfo, ""); ("-update-info", Arg.String setupdateinfo, "");
("-printf-format", Arg.Unit setprintfformat, ""); ("-printf-format", Arg.Unit setprintfformat, "");
@ -3352,6 +3367,16 @@ let go () =
Pdfread.print_linearization (Pdfio.input_of_channel (open_in_bin inname)) Pdfread.print_linearization (Pdfio.input_of_channel (open_in_bin inname))
| _ -> raise (Arg.Bad "-print-linearization: supply a single file name") | _ -> raise (Arg.Bad "-print-linearization: supply a single file name")
end end
| Some (AddPageLabels labelspec) -> ()
| Some RemovePageLabels ->
let pdf = get_single_pdf args.op false in
Pdfpagelabels.remove pdf;
write_pdf false pdf
| Some PrintPageLabels ->
let pdf = get_single_pdf args.op true in
iter
print_string
(map Pdfpagelabels.string_of_pagelabel (Pdfpagelabels.read pdf))
let parse_argv () = let parse_argv () =
Arg.parse_argv ~current:(ref 0) Arg.parse_argv ~current:(ref 0)