将A4 PDF页面拆分为两个A5并再次返回 [英] Split A4 PDF page into two A5 and back again

查看:835
本文介绍了将A4 PDF页面拆分为两个A5并再次返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有A4页的PDF。出于打印原因,每个页面包含两个相同的A5页面。我想在我的Java程序中做的是拆分这些页面并使用每个唯一的A5页面零次多次作为模板添加/替换一些文本。完成此操作后,我想再将A5页面粘贴回A4页面(出于相同的打印原因)。

I have a PDF with A4 pages. Each page contains two identical A5 pages for printing reasons. What I want to do in my Java program is to split these pages and use each unique A5 page zero to many times as a template to add/replace some text. After this is done I want to glue the A5 pages back again to A4 pages (for the same printing reasons).

示例:使用第一页三次和第二页一次。

An example: Use page one three times and page two one time.

  • 拆分页面。 (并丢弃相同的右A5页)
  • 创建第一页的三个副本和第二页的一个副本。
  • 添加/替换文本。
  • 将页面粘合在一起,这样我就得到两张A4页面。第一个是前两个页面一个,第二个是第三个第一页和唯一的第二页。
  • Split the pages. (And throw away the identical right A5 pages)
  • Create three copies of the first page and one copy of the second page.
  • Add/replace the text.
  • Glue the pages together so that I get two A4 pages. The first one with the first two "page ones" and the second one with the third "page one" and the only "page two".

这应该是可能的吗?不应该吗?
我正在考虑使用iText。但是,如果有人有任何其他建议我很乐意改变我的想法。

This should be possible? Shouldn't it? I'm thinking of using iText. But if anyone has any other recommendation I'm happy to change my mind about that.

推荐答案

我曾经做过类似的事情。 camlpdf 。在我的情况下,我有一个PDF,其中一个物理A4页面由两个逻辑A5页面组成,我想得到一个普通的PDF页面(即逻辑和物理页面相同)。

I once did something like that with camlpdf. In my case, I had a PDF where a physical A4 page consisted of two logical A5 pages and I wanted to get a normal PDF with A5 pages (i.e. where logical and physical page were the same).

这是在OCaml中(camlpdf也存在于F#中),我的代码如下:

This was in OCaml (camlpdf also exists for F#) and my code was the following:

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 ;;

如果iText提供类似的抽象,也许你可以这样做。程序如下:

If iText offers similar abstractions, perhaps you can do something like this. The procedure is the following:


  1. 阅读并(可选)解密pdf

  2. 删除书签(可选)

  3. 从页面树中获取页面

  4. 操作页面:您可以重新排列,复制和删除页面,还可以更改其媒体箱(边界框);这应该足以达到你的目的了吗?

  5. 用新页面重建文档

  6. 删除未引用的对象(如垃圾收集)

  7. 写出结果PDF

  1. Read and (optionally) decrypt the pdf
  2. Remove bookmarks (optional)
  3. Obtain the pages from the page tree
  4. Manipulate the pages: you can rearrange, duplicate and remove pages, and you can change their mediabox (bounding box); that should be enough for your purpose?
  5. Reconstruct the document with the new pages
  6. Remove unreferenced objects (like a garbage collect)
  7. Write out the resulting PDF

这篇关于将A4 PDF页面拆分为两个A5并再次返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆