Divide the PDF PDF page into two A5 and vice versa

I have a PDF with A4 pages. Each page contains two identical A5 pages for printing. What I want to do in my Java program is to split these pages and reuse each unique A5 page as a template for adding / replacing some text. After that I want to stick A5 pages back to pages A4 (for the same reasons, printing).

Example: use one page three times and a page two times.

  • Separate the pages. (And throw away the identical right-hand pages of A5)
  • Create three copies of the first page and one copy of the second page.
  • Add / replace text.
  • Paste the pages together to get two A4 pages. The first - with the first two "pages" and the second with the third "page one" and only the "second page".

Should it be possible? Is not it? I am thinking about using iText. But if anyone has any other recommendation, I’d be happy to change my mind about it.

+2
java pdf
source share
3 answers

I somehow did something similar with camlpdf . In my case, I had a PDF file where the A4 physical page consisted of two A5 logical pages, and I wanted to get a regular PDF file with A5 pages (that is, where the logical and physical page were the same).

This was in OCaml (camlpdf also exists for F #), and my code was as follows:

let pdf = Pdfread.pdf_of_file None in_file ;; let pdf = let (pdf,_perms) = Pdfcrypt.decrypt_pdf "" pdf in match pdf with | Some pdf -> pdf | None -> failwith "Could not decrypt" ;; let pdf = Pdfmarks.remove_bookmarks pdf ;; let pages = Pdfdoc.pages_of_pagetree pdf ;; let pages = List.fold_right (fun page acc -> let (y1,x1,y2,x2) = Pdf.parse_rectangle page.Pdfdoc.mediabox in let box y1 x1 y2 x2 = Pdf.Array [ Pdf.Real y1; Pdf.Real x1; Pdf.Real y2; Pdf.Real x2 ] in let xm = x1 *. 0.5 +. x2 *. 0.5 in let pagel = {page with Pdfdoc.mediabox = box y1 x1 y2 xm} and pager = {page with Pdfdoc.mediabox = box y1 xm y2 x2} in pagel::pager::acc ) pages [] ;; let pdf = Pdfdoc.change_pages false pdf pages ;; Pdf.remove_unreferenced pdf ;; Pdfwrite.pdf_to_file pdf out_file ;; 

If iText offers similar abstractions, maybe you can do something like this. The procedure is as follows:

  • Reading and (optionally) decrypting pdf
  • Delete bookmarks (optional)
  • Get pages from the page tree
  • Manipulate pages: you can reorder, duplicate and delete pages, and you can change their mediabox (bounding box); should this be enough for your purpose?
  • Recover document with new pages
  • Remove objects without references (eg, garbage collection)
  • Record the resulting PDF
+2
source share

Perhaps a less complicated recording solution using pdfjam related bits. If test.pdf is an A4 album, which should be divided into a portrait of A5:

1) remove the left half-countries

 pdfcrop --bbox "0 0 421 595" --clip --papersize "a5" test.pdf test-left.pdf 

Note: --bbox "<left> <bottom> <right> <top>" works in bp units

2) Remove the right polustranitsy:

 pdfcrop --bbox "421 0 842 595" --clip --papersize "a5" test.pdf test-right.pdf 

3) Sort optional page, e.g.

 pdfjoin test-left.pdf test-right.pdf "1" --outfile test-collated.pdf 

4) reglue:

 pdfnup --nup 2x1 test-collated.pdf --a4paper --outfile test-done.pdf 
+4
source share

Try iText library http://itextpdf.com/ . You can use an existing pdf file for a template, edit rotation and split existing documents. Useful samples can be found here: http://www.1t3xt.info/examples/browse/

0
source share

All Articles