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

View File

@ -1239,7 +1239,8 @@ 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
let getbox page box =
if box = "/MediaBox" then if box = "/MediaBox" then
match page.Pdfpage.mediabox with match page.Pdfpage.mediabox with
| Pdf.Array [a; b; c; d] -> | Pdf.Array [a; b; c; d] ->
@ -1252,12 +1253,14 @@ let output_page_info pdf =
Printf.sprintf "%f %f %f %f" Printf.sprintf "%f %f %f %f"
(Pdf.getnum a) (Pdf.getnum b) (Pdf.getnum c) (Pdf.getnum d) (Pdf.getnum a) (Pdf.getnum b) (Pdf.getnum c) (Pdf.getnum d)
| _ -> "" | _ -> ""
in let rotation page = and rotation page =
Pdfpage.int_of_rotation page.Pdfpage.rotate Pdfpage.int_of_rotation page.Pdfpage.rotate
in in
for pnum = 1 to Pdfpage.endpage pdf do for pnum = 1 to Pdfpage.endpage pdf do
let page = select pnum pages in let page = select pnum pages in
Printf.printf "Page %i:\n" pnum; Printf.printf "Page %i:\n" pnum;
Printf.printf "Label: %s\n"
(try Pdfpagelabels.pagelabeltext_of_pagenumber pnum labels with Not_found -> "");
Printf.printf "MediaBox: %s\n" (getbox page "/MediaBox"); Printf.printf "MediaBox: %s\n" (getbox page "/MediaBox");
Printf.printf "CropBox: %s\n" (getbox page "/CropBox"); Printf.printf "CropBox: %s\n" (getbox page "/CropBox");
Printf.printf "BleedBox: %s\n" (getbox page "/BleedBox"); Printf.printf "BleedBox: %s\n" (getbox page "/BleedBox");

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)