Home > Software design >  How to add blank pages in pdf based on odd and even pages
How to add blank pages in pdf based on odd and even pages

Time:01-07

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.

CodePudding user response:

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.

CodePudding user response:

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)
  • Related