简体   繁体   中英

Print a PDF file using Qt

I want to open and print a PDF file from particular path, My previous code work perfectly open and directly send print command to printer. Now what I want is multiple printer are there and I have to select one, and after that I want send print command, I don't want want use QPrintDialog, My printer name are stored in a TextBox and retrieve that name and print it through that printer which I set in textbox:

my previous code mention below:

#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;
}

Or you can change your printer as default printer during printing.

  1. change default printer to your printer
  2. print pdf
  3. restore old default printer

How to retrieve and set the default printer in Windows: http://support.microsoft.com/default.aspx?scid=kb;EN-US;246772

Since QT has no functions for system administration. For QT,

  1. Change default printer to your printer

    How to get default printer name?

    QPrinterInfo::defaultPrinterName()

    from: http://doc.qt.io/qt-5/qprinterinfo.html#defaultPrinterName

    How to set default printer?

    By excuting, RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "your printer name"

    from: http://windowsitpro.com/windows/jsi-tip-8415-how-can-i-set-users-default-printer-batch-script

  2. Print your pdf as you know

  3. Restore old default printer

    By executing, RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "old default printer name"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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