使用Ghostscript(PostScript)从标准输入中计算PDF页面数

6

我在stackoverflow上找到了如何使用Ghostscript计算PDF文件页面数量的方法,只需在shell上执行以下命令:

gs -q -dNODISPLAY -c "($PATH_TO_PDF) (r) file runpdfbegin pdfpagecount = quit"')

我希望从标准输入中获取pdf文件。

我已经尝试了一些方法,但是没有成功。

我的尝试方法如下:

gs -q -dNODISPLAY - -c "(%stdin) (r) file runpdfbegin pdfpagecount = quit"')

我没有输出。
有什么提示或建议吗?

1
一周多以来第一个有趣的后记问题,点赞! - luser droog
3个回答

5

无法使用stdin处理PDF文件,因为PDF格式使得能够随机访问文件的所有部分更加必要。

在Ghostscript从stdin读取PDF文件的情况下,它首先将其复制到本地文件,然后再进行处理,所以它根本不是从stdin工作。

简而言之,这是不可能做到的。


哎呀,好吧,谢谢你的回答。我本以为可以避免这个问题。 - Mathias

5

这是有效的:

gs -q -dNODISPLAY -c "($PATH_TO_PDF) (r) file runpdfbegin pdfpagecount = quit";

我认为你尝试使用的问题在于:
gs -q -dNODISPLAY -c "($PATH_TO_PDF) (r) file runpdfbegin pdfpagecount = quit"')

QUIT后面没有匹配的闭合括号


1
你实际上可以在末尾省略行分隔符 ';' -- 做得好。 - David C. Rankin

-3

可以做到。

String ars = "-q -dNODISPLAY  -dNOPAUSE -sDEVICE=tiffg3  -r150.4 -o" + outputImagesPath + "%d.tiff -sPAPERSIZE=a4 " + inputPDFFile + " -c quit";
Process proc = new Process();
proc.StartInfo.FileName = ghostScriptPath;
proc.StartInfo.Arguments = ars;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
//Raise Your Complete Job Event Here And User Directory.GetFiles("..").Count

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接