Ghostscript: PDF total pages

删除回忆录丶 提交于 2019-11-30 06:47:18

问题


I'm using Ghostscript library API (wrapping from C#) to print PDF documents from my application.
With the '-dFirstPage' and '-dLastPage' parameters I'm able to select an range of pages to be printed, but how about the total number of a PDF's pages?

It is not very nice to allow a user to select a page interval from 2 to 10 when, let me say, the PDF document has only 4 pages.

Consider that I'm using Ghostscript library through the gsapi_init_with_args API library call.


回答1:


Ghostscript can count and display the number of pages of a PDF on stdout. The commandline is

gswin32c ^
  -q ^
  -dNODISPLAY ^
  -c "(input.pdf) (r) file runpdfbegin pdfpagecount = quit" 

Here all the -c "..." stuff is a PostScript commandline snippet (using a few GS internal command extensions). And input.pdf is the PDF filename (could also be a full path like (c:/path/to/my.pdf)).

However, a better and faster tool for this kind of job would be to use pdfinfo (part of the XPDF-utilities, also available on Windows).


Update:

@ebyrob wants to know if one can modify my example command line so that it also displays the PDF in a single operation. Try this:

gswin32c ^
  -q ^
  -c "(input.pdf) (r) file runpdfbegin pdfpagecount =" ^
  -f input.pdf

Well, it's not a single operation -- it's just two different operations in a single commandline.




回答2:


I tried to make this script:

gswin32c ^
  -q ^
  -c "(input.pdf) (r) file runpdfbegin pdfpagecount =" ^
  -f input.pdf

work in a c# wrapped solution and kept getting error "/undefinedfilename". In this case ensure that your filepath has Slashes "/" as DirectorySeperator and not Backslashes "\". I know Kurt Pfeifle already wrote it, but it happened to me i just overlooked it.



来源:https://stackoverflow.com/questions/4826485/ghostscript-pdf-total-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!