diff --git a/cpdf.ml b/cpdf.ml index bf990ff..31e0285 100644 --- a/cpdf.ml +++ b/cpdf.ml @@ -2533,6 +2533,48 @@ let stamp relative_to_cropbox position topline midline fast scale_to_fit isover let new_marks = map (change_bookmark changetable) marks in Pdfmarks.add_bookmarks new_marks changed +let add_page_as_xobject pdf page name = + pdf + +let stamp_as_xobject pdf over = + let prefix = Pdfpage.shortest_unused_prefix pdf in + Pdfpage.add_prefix over prefix; + let marks = Pdfmarks.read_bookmarks pdf in + let marks_refnumbers = Pdf.page_reference_numbers pdf in + let pdf = Pdfmarks.remove_bookmarks pdf in + let over = Pdfmarks.remove_bookmarks over in + let pageseqs = ilist 1 (Pdfpage.endpage pdf) in + let over_firstpage_pdf = + match Pdfpage.pages_of_pagetree over with + | [] -> error "empty PDF" + | h::_ -> Pdfpage.change_pages ~changes:[(1, 1)] true over [h] + in + let merged = + Pdfmerge.merge_pdfs + false false ["a"; "b"] [pdf; over_firstpage_pdf] [pageseqs; [1]] + in + let merged = + {merged with Pdf.saved_encryption = pdf.Pdf.saved_encryption} + in + let merged = copy_id true pdf merged in + let merged_pages = Pdfpage.pages_of_pagetree merged in + let under_pages, over_page = + all_but_last merged_pages, last merged_pages + in + let new_pages = under_pages in + let changed = + let changes = + map (fun x -> (x, x)) (ilist 1 (length new_pages)) + in + Pdfpage.change_pages ~changes true merged new_pages + in + let new_refnumbers = Pdf.page_reference_numbers changed in + let changetable = hashtable_of_dictionary (combine marks_refnumbers new_refnumbers) in + let new_marks = map (change_bookmark changetable) marks in + let pdf = Pdfmarks.add_bookmarks new_marks changed in + let name = "/X0" in + (add_page_as_xobject pdf over_page name, name) + (* Combine pages from two PDFs. For now, assume equal length. *) (* If [over] has more pages than [under], chop the excess. If the converse, pad diff --git a/cpdf.mli b/cpdf.mli index fd4b50e..0e47fcd 100644 --- a/cpdf.mli +++ b/cpdf.mli @@ -458,3 +458,5 @@ val ocg_rename : string -> string -> Pdf.t -> unit val ocg_order_all : Pdf.t -> unit +val stamp_as_xobject : Pdf.t -> Pdf.t -> Pdf.t * string + diff --git a/cpdfcommand.ml b/cpdfcommand.ml index a28e2f6..266c67f 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -3594,9 +3594,6 @@ let write_json output pdf = CpdfwriteJSON.write f args.jsonparsecontentstreams args.jsonnostreamdata pdf; close_out f -let stamp_as_xobject pdf stamp_pdf = - (pdf, "/X0") - (* Main function *) let go () = match args.op with @@ -4508,7 +4505,7 @@ let go () = in let pdf = get_single_pdf args.op false in let pdf, xobj_name = - stamp_as_xobject pdf stamp_pdf + Cpdf.stamp_as_xobject pdf stamp_pdf in Printf.printf "%s\n" xobj_name; flush stdout;