我正在使用 GhostScript API 来测试 PDF 是否受用户密码保护(只有拥有密码才能打开文档,这与仅保护内容免遭未经授权复制或打印的所有者保护有何不同)
我在 Qt 中使用这段代码:
const bool PDFTools::isUserProtected(const QString &iFilePath)
{
void *minst;
const QString filePath = iFilePath.toUtf8();
const std::string inputFile = QString( "%1" ).arg(filePath).toStdString();
int initiationCode = 0;
int executionCode = 0;
// GhostScript arguments
const int gsArgumentCount = 2;
char * gsArgumentValues[gsArgumentCount];
gsArgumentValues[0] = "-dNODISPLAY";
gsArgumentValues[1] = const_cast<char*>( inputFile.c_str() );
initiationCode = gsapi_new_instance(&minst, NULL);
if(initiationCode < 0)
return true;
initiationCode = gsapi_set_arg_encoding(minst, GS_ARG_ENCODING_UTF8);
if (initiationCode < 0)
{
gsapi_delete_instance(minst);
return true;
}
executionCode = gsapi_init_with_args(minst, gsArgumentCount, gsArgumentValues);
if(executionCode < 0)
{
gsapi_exit(minst);
gsapi_delete_instance(minst);
return true;
}
gsapi_exit(minst);
gsapi_delete_instance(minst);
return false;
}
当文档被保护时返回真,因为没有密码无法打开,Ghostscript 返回执行错误,这没关系。
问题是当文档不 protected 时它会打开第一页并一直等待按下返回键以便它可以转到下一页。
>>showpage, press <return> to continue<<
您可以将“-dNOPAUSE”和“-dBATCH”添加到 gsArgumentValues,但这会导致另一个问题。如果我的 PDF 非常大且页数很大,这将花费很多时间来遍历所有页面,然后它会返回 false。
有谁知道当Ghostscript提示返回时我该如何退出进程?在那个阶段我已经知道 PDF 不 protected ,我可以退出并返回 false。
我试过回调函数,但没有成功。
提前致谢。
最佳答案
尝试使用 -dFirstPage=1 -dLastPage=1 和 -dNOPAUSE -dBATCH。然后它只处理第一页。
否则,您将不得不拦截按任意键并中断进程,或者修改设备以在第一页或其他内容之后抛出错误。
注意 尽管您可以将这些开关用于 PDF 以外的其他类型的输入,但它们仍然必须处理整个输入文件,因为与 PDF 不同,没有简单的方法可以找到第 n 个PostScript 或 PCL 文件中的页面。
关于c++ - Ghostscript api 请求 "press <return> to continue",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25806839/