如何根据奇偶页在PDF中添加空白页。

huangapple go评论187阅读模式
英文:

How to add blank pages in pdf based on odd and even pages

问题

我想按姓氏字母顺序创建PDF文件,然后这个PDF文档将用于双面打印。
在创建PDF文件时,姓氏的第一个字母之前应该有分页符,然后在开始下一个字母之前应该有一张空白纸。
如果姓氏字母“A”在偶数页上打印完毕,字母“B”可以在下一个奇数页上开始打印。
如果姓氏字母“A”在奇数页上打印完毕,下一个偶数页应该是空白的,“B”字母应该在下一个奇数页上开始打印。
换句话说,每个字母都应该从下一个可用的奇数页开始打印。

我尝试了Telerik报告来生成PDF文件,但在Telerik报告中,我无法跟踪生成PDF文件时的页码。

英文:

I want to create pdf file accordingly lastname alphabet order and then this PDF document will be used for double-sided printing.
When creating a pdf file, there should be page breaks on the first letter of last name and then a blank sheet before starting the next letter.
If 'A' letter finish printing on an even page, "B" letter can begin printing on the next odd page.
If 'A' letter finish printing on an odd page, the next even page should be blank. 'B" letter should begin on the next odd page.
Another way to say this is that every letter should begin printing on the next available odd page.

I have tried telerik report to generate pdf file but in telerik report I did not able to track page numbers when generate pdf file.

答案1

得分: 1

你可以从你的程序中作为外部进程调用PDF工具包pdftk

使用命令pdftk.exe yourfile.pdf dump_data,你可以找到包含的页面数量。

示例输出:

c:\TEXTS>pdftk.exe example.pdf dump_data
InfoKey: Creator
InfoValue:
InfoKey: Title
InfoValue: Citations, mots célèbres 2
InfoKey: Producer
InfoValue: wkhtmltopdf
InfoKey: CreationDate
InfoValue: D:20130904112136+02'00'
NumberOfPages: 5

如果NumberOfPages是奇数,可以使用以下命令追加一个空白页面:

pdftk.exe A=yourfile.pdf B=empty_page.pdf cat A B output yourfile_even.pdf

一旦你将所有文件变为偶数,你可以将它们连接起来:

pdftk.exe A=a.pdf B=b.pdf C=c.pdf cat A B C output result.pdf

一个更近期的替代品是开源工具qpdf

英文:

You could call the PDF tool kit pdftk as external process from your program:

With pdftk.exe yourfile.pdf dump_data, you can find the number of contained pages.

Example output:

c:\TEXTS>pdftk.exe example.pdf dump_data
InfoKey: Creator
InfoValue:
InfoKey: Title
InfoValue: Citations, mots célèbres 2
InfoKey: Producer
InfoValue: wkhtmltopdf
InfoKey: CreationDate
InfoValue: D:20130904112136+02'00'
NumberOfPages: 5

If NumberOfPages is odd, use a command like to following to append an empty page:

pdftk.exe A=yourfile.pdf B=empty_page.pdf cat A B output yourfile_even.pdf

Once you have evened all files, you can concatenate them:

pdftk.exe A=a.pdf B=b.pdf C=c.pdf cat A B C output result.pdf

A more recent alternative to pdftk is the open source tool qpdf.

答案2

得分: 1

你可以分别生成每个文件,然后在每个文件上运行 cpdf -pad-multiple 2 in.pdf -o out.pdf,以添加一个空白页(如果需要的话)。然后,你可以使用 cpdf 1.pdf 2.pdf 3.pdf ... -o final.pdf 来合并它们。

英文:

You can produce each file separately, then run cpdf -pad-multiple 2 in.pdf -o out.pdf on each, to add a blank page if needed. Then you can merge them all with cpdf 1.pdf 2.pdf 3.pdf ... -o final.pdf.

答案3

得分: 0

通常使用qpdf或cpdf可执行文件可以根据需要添加额外的页面,但从这个问题的语气来看,您似乎希望在PDF构建过程中进行“内联”添加。在这种情况下,最好不要构建A.pdf和B.pdf,然后在合并之前添加单个空白页,而是最好构建完整的A-Z.pdf,然后根据需要注入空白页。

这需要您构建一个个性化的解决方案,其中您提取页面编号,例如,不使用您的示例:

  • 章节A = 第1页
  • 章节B = 第4页
  • 章节C = 第6页
  • 章节D = 第9页

因此,您知道在第4页之前需要注入空白页,以将4移到5,这将将6移到7,所以现在您不需要移动第6页,但第9页现在是第10页,所以现在需要在第10页之前注入空白页。因此,您需要跟踪以前的提升情况。

您可以将其编写为一个简单的子程序,以调用报告页面内容作为编号并最好包括空白页插入的库之一的方法。

在伪代码中,您需要2个跟踪器:

  • 如果上次注入是偶数,则下一个是奇数,反之亦然(翻转)
  • 页面注入是当前页面+之前的空白页数(增量值)

注意,cpdf的后续回答由JohnWhitington显示,在上述情况下,使用以下方式最容易
cpdf -pad-before in.pdf 4,9 -o output.pdf
cpdf将执行第2部分的增量移动,使4变成5(6变成7),并且旧的9将移动到11,但您仍然需要确定哪些页面需要在它们之前插入空白页(奇数或偶数)的翻转状态。

大致上,您需要做以下操作:

设置标志为偶数(EVEN)

循环处理下一页
如果页面是最后一页,则退出循环
页面是否为章节标题?
  如果页面是奇数且标志为奇数,则保存页码并将标志设置为偶数,然后进入下一页
  如果页面是奇数且标志为偶数,则进入下一页
  如果页面是偶数且标志为奇数,则进入下一页
  如果页面是偶数且标志为偶数,则保存页码并将标志设置为奇数,然后进入下一页
否则,页面不是章节标题,继续下一页
  • 1 保持标志为偶数
  • 2和3不是章节
  • 4是章节,是偶数,标志为偶数,保存页码,将标志设置为奇数,然后进入下一页
  • 5不是章节
  • 6是章节,是偶数,标志为奇数(跳过保存页码)
  • 7和8不是章节
  • 9是章节,是奇数,标志为奇数,保存页码,将标志设置为偶数,然后进入下一页

...等等。

英文:

Generally using qpdf or cpdf executables the extra page can be added as required, https://stackoverflow.com/a/73575464/10802527 however it seems from the tone of this question you wish to do that "inline" during PDF building, In that case it is best NOT to build A.pdf B.pdf and add single blanks before merge it is better to build the full A-Z.pdf then inject the blanks as needed.

This requires you build a personalized solution where you extract page numbers so lets say without your example

  • Chapter A = page 1
  • Chapter B = page 4
  • Chapter C = page 6
  • Chapter D = page 9

so you know you need inject blank before page 4 to move 4 to 5 and that will move 6 to 7 so now you don't need to shift page 6 however page 9 is now page 10 so WILL now need inject blank before page 10. So you need to keep track of the previous up lifts.

You can write that as a simple subroutine to call one of the libraries that reports the page content as number and ideally also includes a blank page insertion, method.

in pseudo code you need 2 trackers

  • if last inject was even next is odd and vice versa (flip flop)
  • page inject is current + previous blanks (incremental value)

NOTE the later answer by JohnWhitington from cpdf shows that this will be easiest in above case by use
cpdf -pad-before in.pdf 4,9 -o output.pdf

cpdf will do the 2nd shifting incremental part so 4 becomes 5 (& 6 becomes 7) and also old 9 will then move to 11 but you will still need to determine the flip flop state (odd or even) of which pages need a blank before them.

roughly you need to

set flag=EVEN

loop NEXT PAGE
if page is last page then exit from loop
is page a section heading ?
  then if page odd and flag is ODD save page num and set flag as EVEN and go NEXT PAGE
       if page odd and flag is EVEN go next page
       if page even and flag is ODD go next page
       if page even and flag is EVEN save page num and set flag as ODD and go NEXT PAGE
else page not section heading go NEXT PAGE
  • 1 keeps flag as EVEN
  • 2 & 3 is not section
  • 4 is section, is even, flag is EVEN, save page num, set flag ODD
  • 5 is not section
  • 6 is section, is even, flag is ODD (skip save num)
  • 7 & 8 is not section
  • 9 is section, is odd, flag is ODD, save page num, set flag EVEN

... etc.

huangapple
  • 本文由 发表于 2023年1月6日 15:12:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定