From c711e3aa776a8efadf37693849fc6b47d8fd79ea Mon Sep 17 00:00:00 2001 From: John Whitington Date: Tue, 21 Dec 2021 15:06:40 +0000 Subject: [PATCH] more --- cpdf.ml | 34 ---------------------------------- cpdf.mli | 3 +-- cpdfbookmarks.ml | 34 ++++++++++++++++++++++++++++++++++ cpdfbookmarks.mli | 3 +++ cpdfcommand.ml | 4 ++-- 5 files changed, 40 insertions(+), 38 deletions(-) diff --git a/cpdf.ml b/cpdf.ml index 516d2f9..32ee20f 100644 --- a/cpdf.ml +++ b/cpdf.ml @@ -1965,38 +1965,6 @@ let remove_unused_resources_page pdf n page = let remove_unused_resources pdf = Cpdfpage.process_pages (ppstub (remove_unused_resources_page pdf)) pdf (ilist 1 (Pdfpage.endpage pdf)) -(* Indent bookmarks in each file by one and add a title bookmark pointing to the first page. *) -let add_bookmark_title filename use_title pdf = - let title = - if use_title then - match Cpdfmetadata.get_info_utf8 pdf "/Title", Cpdfmetadata.get_xmp_info pdf "/Title" with - "", x | x, "" | _, x -> x - else - Filename.basename filename - in - let marks = Pdfmarks.read_bookmarks pdf in - let page1objnum = - match Pdfpage.page_object_number pdf 1 with - None -> error "add_bookmark_title: page not found" - | Some x -> x - in - let newmarks = - {Pdfmarks.level = 0; - Pdfmarks.text = title; - Pdfmarks.target = Pdfdest.XYZ (Pdfdest.PageObject page1objnum, None, None, None); - Pdfmarks.isopen = false} - ::map (function m -> {m with Pdfmarks.level = m.Pdfmarks.level + 1}) marks - in - Pdfmarks.add_bookmarks newmarks pdf - -let bookmarks_open_to_level n pdf = - let marks = Pdfmarks.read_bookmarks pdf in - let newmarks = - map - (fun m -> {m with Pdfmarks.isopen = m.Pdfmarks.level < n}) - marks - in - Pdfmarks.add_bookmarks newmarks pdf let create_pdf pages pagesize = let page = @@ -2006,5 +1974,3 @@ let create_pdf pages pagesize = in let pdf, pageroot = Pdfpage.add_pagetree (many page pages) (Pdf.empty ()) in Pdfpage.add_root pageroot [] pdf - - diff --git a/cpdf.mli b/cpdf.mli index 6c0d03e..42241ff 100644 --- a/cpdf.mli +++ b/cpdf.mli @@ -138,10 +138,9 @@ val image_resolution : Pdf.t -> int list -> float -> (int * string * int * int * val copy_box : string -> string -> bool -> Pdf.t -> int list -> Pdf.t -val add_bookmark_title : string -> bool -> Pdf.t -> Pdf.t val remove_unused_resources : Pdf.t -> Pdf.t -val bookmarks_open_to_level : int -> Pdf.t -> Pdf.t + val create_pdf : int -> Pdfpaper.t -> Pdf.t diff --git a/cpdfbookmarks.ml b/cpdfbookmarks.ml index dc7fd92..8be3464 100644 --- a/cpdfbookmarks.ml +++ b/cpdfbookmarks.ml @@ -359,3 +359,37 @@ let name_of_spec encoding marks (pdf : Pdf.t) splitlevel spec n filename startpa else process_others encoding marks pdf splitlevel filename n startpage endpage (implode before ^ fill (length percents) n ^ implode after) + +(* Indent bookmarks in each file by one and add a title bookmark pointing to the first page. *) +let add_bookmark_title filename use_title pdf = + let title = + if use_title then + match Cpdfmetadata.get_info_utf8 pdf "/Title", Cpdfmetadata.get_xmp_info pdf "/Title" with + "", x | x, "" | _, x -> x + else + Filename.basename filename + in + let marks = Pdfmarks.read_bookmarks pdf in + let page1objnum = + match Pdfpage.page_object_number pdf 1 with + None -> error "add_bookmark_title: page not found" + | Some x -> x + in + let newmarks = + {Pdfmarks.level = 0; + Pdfmarks.text = title; + Pdfmarks.target = Pdfdest.XYZ (Pdfdest.PageObject page1objnum, None, None, None); + Pdfmarks.isopen = false} + ::map (function m -> {m with Pdfmarks.level = m.Pdfmarks.level + 1}) marks + in + Pdfmarks.add_bookmarks newmarks pdf + +let bookmarks_open_to_level n pdf = + let marks = Pdfmarks.read_bookmarks pdf in + let newmarks = + map + (fun m -> {m with Pdfmarks.isopen = m.Pdfmarks.level < n}) + marks + in + Pdfmarks.add_bookmarks newmarks pdf + diff --git a/cpdfbookmarks.mli b/cpdfbookmarks.mli index f4a8a35..ade5d69 100644 --- a/cpdfbookmarks.mli +++ b/cpdfbookmarks.mli @@ -16,3 +16,6 @@ val list_bookmarks : json:bool -> Cpdfmetadata.encoding -> int list -> Pdf.t -> val name_of_spec : Cpdfmetadata.encoding -> Pdfmarks.t list -> Pdf.t -> int -> string -> int -> string -> int -> int -> string + +val add_bookmark_title : string -> bool -> Pdf.t -> Pdf.t +val bookmarks_open_to_level : int -> Pdf.t -> Pdf.t diff --git a/cpdfcommand.ml b/cpdfcommand.ml index e7d11ab..40639b4 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -3038,7 +3038,7 @@ let go () = let pdfs = if args.merge_add_bookmarks then map2 - (fun filename pdf -> Cpdf.add_bookmark_title filename args.merge_add_bookmarks_use_titles pdf) + (fun filename pdf -> Cpdfbookmarks.add_bookmark_title filename args.merge_add_bookmarks_use_titles pdf) (map (function InFile s -> s | StdIn -> "" | AlreadyInMemory _ -> "") names) pdfs else @@ -3852,7 +3852,7 @@ let go () = Cpdffont.embed_missing_fonts args.path_to_ghostscript args.gs_quiet fi fo | Some (BookmarksOpenToLevel n) -> let pdf = get_single_pdf args.op false in - write_pdf false (Cpdf.bookmarks_open_to_level n pdf) + write_pdf false (Cpdfbookmarks.bookmarks_open_to_level n pdf) | Some CreatePDF -> let pdf = Cpdf.create_pdf args.createpdf_pages args.createpdf_pagesize in write_pdf false pdf