First working version of -stamp-as-xobject

This commit is contained in:
John Whitington 2020-03-20 16:29:54 +00:00
parent 090dd50e02
commit e0308ccee9
1 changed files with 33 additions and 4 deletions

37
cpdf.ml
View File

@ -2533,13 +2533,31 @@ let stamp relative_to_cropbox position topline midline fast scale_to_fit isover
let new_marks = map (change_bookmark changetable) marks in let new_marks = map (change_bookmark changetable) marks in
Pdfmarks.add_bookmarks new_marks changed Pdfmarks.add_bookmarks new_marks changed
let add_xobject_to_page xobjname xobjnum page pdf =
let resources' =
let xobjects =
match Pdf.lookup_direct pdf "/XObject" page.Pdfpage.resources with
| Some xobjects -> xobjects
| _ -> Pdf.Dictionary []
in
let new_xobjects =
Pdf.add_dict_entry xobjects xobjname (Pdf.Indirect xobjnum)
in
Pdf.add_dict_entry page.Pdfpage.resources "/XObject" new_xobjects
in
{page with Pdfpage.resources = resources'}
let add_page_as_xobject pdf range page name = let add_page_as_xobject pdf range page name =
let xobject_data = bytes_of_string "0 0 m 0 100 l 100 100 l 100 0 l f" in let xobject_data =
match Pdfops.stream_of_ops (Pdfops.parse_operators pdf page.Pdfpage.resources page.Pdfpage.content) with
Pdf.Stream {contents = (_, Got b)} -> b
| _ -> assert false
in
let xobject_dict = let xobject_dict =
["/Type", Pdf.Name "/XObject"; ["/Type", Pdf.Name "/XObject";
"/Subtype", Pdf.Name "/Form"; "/Subtype", Pdf.Name "/Form";
"/BBox", Pdf.Array [Pdf.Real 0.; Pdf.Real 0.; Pdf.Real 1000.; Pdf.Real 1000.]; "/BBox", Pdf.Array [Pdf.Real 0.; Pdf.Real 0.; Pdf.Real 1000.; Pdf.Real 1000.];
"/Resources", Pdf.Dictionary []; "/Resources", page.Pdfpage.resources;
"/Length", Pdf.Integer (bytes_size xobject_data); "/Length", Pdf.Integer (bytes_size xobject_data);
] ]
in in
@ -2547,7 +2565,18 @@ let add_page_as_xobject pdf range page name =
Pdf.Stream {contents = (Pdf.Dictionary xobject_dict, Pdf.Got xobject_data)} Pdf.Stream {contents = (Pdf.Dictionary xobject_dict, Pdf.Got xobject_data)}
in in
let xobject_objnum = Pdf.addobj pdf xobject in let xobject_objnum = Pdf.addobj pdf xobject in
pdf (* For each page in range, add the xobject to the list of xobjects in the resources of that page *)
let pages = Pdfpage.pages_of_pagetree pdf in
let new_pages =
List.map2
(fun page pnum ->
if mem pnum range
then add_xobject_to_page name xobject_objnum page pdf
else page)
pages
(indx pages)
in
Pdfpage.change_pages true pdf new_pages
(* n.b the use of change_pages here ensures no inheritable resources in the (* n.b the use of change_pages here ensures no inheritable resources in the
* stamp, therefore creation of xobject from page is as simple as expected. *) * stamp, therefore creation of xobject from page is as simple as expected. *)
@ -2587,7 +2616,7 @@ let stamp_as_xobject pdf range over =
let changetable = hashtable_of_dictionary (combine marks_refnumbers new_refnumbers) in let changetable = hashtable_of_dictionary (combine marks_refnumbers new_refnumbers) in
let new_marks = map (change_bookmark changetable) marks in let new_marks = map (change_bookmark changetable) marks in
let pdf = Pdfmarks.add_bookmarks new_marks changed in let pdf = Pdfmarks.add_bookmarks new_marks changed in
let name = "/X0" in let name = "/" ^ Pdfpage.shortest_unused_prefix pdf ^ "CPDFXObj" in
(add_page_as_xobject pdf range over_page name, name) (add_page_as_xobject pdf range over_page name, name)
(* Combine pages from two PDFs. For now, assume equal length. *) (* Combine pages from two PDFs. For now, assume equal length. *)