Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

api - Ghostscript: PDF total pages

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...