cmd.exe to open pdf and print it

1.5k views Asked by At

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:

I have two different code that do two different task: 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

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

1

There are 1 answers

12
ramzes2 On

You can fetch all information from HKEY_CLASSES_ROOT of windows registry.

Here is a example how to fetch default path to printing software and how to run it. I tested it on 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;
}