简体   繁体   English

cmd.exe打开pdf并打印

[英]cmd.exe to open pdf and print it

I am using Qt4.8, What I want is open a pdf and print that pdf automatically through cmd.exe, without clicking on print button in pdf reader by using QProcess: 我正在使用Qt4.8,我想要的是打开一个pdf文件,并通过cmd.exe自动打印该pdf文件,而无需使用QProcess在pdf阅读器中单击“打印”按钮:

I have two different code that do two different task: Opne Pdf 我有两个不同的代码可以执行两个不同的任务:Opne Pdf

QString scmd= "cmd.exe";
list.push_back("/C");
list.push_back("test.pdf");
Process.start(scmd, list);
Sleep(2000);

Print pdf without open it 打印pdf而不打开它

QString scmd2 = "C:/Program Files (x86)/Adobe/Reader 11.0/Reader/AcroRd32.exe.exe"
list2.push_back("/t");
list2.push_back("test.pdf");
Process.start(scmd2, list2);
Sleep(2000);

So I want to merge this command, I dont know how I can do that? 所以我想合并此命令,我不知道该怎么做? Please suggest me something 请给我一些建议

You can fetch all information from HKEY_CLASSES_ROOT of windows registry. 您可以从Windows注册表的HKEY_CLASSES_ROOT获取所有信息。

Here is a example how to fetch default path to printing software and how to run it. 这是一个示例,说明如何获取打印软件的默认路径以及如何运行它。 I tested it on Qt 5.7 我在Qt 5.7上测试过

#include <QSettings>
#include <QProcess>
#include <QDebug>

int main(int argc, char *argv[])
{
    const QString classesRoot = "HKEY_CLASSES_ROOT";

    // get ID of .pdf extension
    QSettings pdfSettings(classesRoot + "\\.pdf", QSettings::NativeFormat);
    QString pdfId = pdfSettings.value("Default").toString();

    // get path to default program that associated with PDF files
    QString printPath = QSettings(classesRoot + "\\" + pdfId + "\\shell\\print\\command", QSettings::NativeFormat).value("Default").toString();
    QString openPath = QSettings(classesRoot + "\\" + pdfId + "\\shell\\open\\command", QSettings::NativeFormat).value("Default").toString();
    qDebug() << "print path" << printPath;
    qDebug() << "open path" << openPath;

    // open .pdf file
    QProcess::startDetached(openPath.arg("full path to pdf file.pdf") );

    // print .pdf file
    QProcess printProcess;
    printProcess.start(printPath.arg("full path to pdf file.pdf") );
    printProcess.waitForFinished(-1);

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM