2

I have folder of different documents like: pdf, txt, rtf, images. My case is to send all documents to the printer (print it). Used framework is MFC and WinAPI. Current implementation has dialog box for choose documents and another dialog for choose printer.

Then question appears, how to print it all? Do I need to convert every documents to PDF, then merge it and print one pdf document? I will appreciate any advice in that field.

void CMultipleDocsPrintTestDlg::OnBnClickedButton1()
{
    TCHAR strFilter[] = { _T("Rule Profile (*.pdf)||") };

    // Create buffer for file names.
    const DWORD numberOfFileNames = 100;
    const DWORD fileNameMaxLength = MAX_PATH + 1;
    const DWORD bufferSize = (numberOfFileNames * fileNameMaxLength) + 1;

    CFileDialog fileDlg(TRUE, _T("pdf"), NULL, OFN_ALLOWMULTISELECT, strFilter);
    TCHAR* filenamesBuffer = new TCHAR[bufferSize];

    // Initialize beginning and end of buffer.
    filenamesBuffer[0] = NULL;
    filenamesBuffer[bufferSize - 1] = NULL;

    // Attach buffer to OPENFILENAME member.
    fileDlg.GetOFN().lpstrFile = filenamesBuffer;
    fileDlg.GetOFN().nMaxFile = bufferSize;

    // Create array for file names.
    CString fileNameArray[numberOfFileNames];
    if (fileDlg.DoModal() == IDOK)
    {
        // Retrieve file name(s).
        POSITION fileNamesPosition = fileDlg.GetStartPosition();
        int iCtr = 0;
        while (fileNamesPosition != NULL)
        {
            fileNameArray[iCtr++] = fileDlg.GetNextPathName(fileNamesPosition);
        }
    }
    // Release file names buffer.
    delete[] filenamesBuffer;


    CPrintDialog dlg(FALSE);
    dlg.m_pd.Flags |= PD_PRINTSETUP;

    CString printerName;
    if (dlg.DoModal() == IDOK)
    {
        printerName = dlg.GetDeviceName();
    }
    // What next ???
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
drewpol
  • 665
  • 1
  • 6
  • 26

1 Answers1

0

You could make use of ShellExecute to do this. The parameter lpOperation can be set to print. To quote:

Prints the file specified by lpFile. If lpFile is not a document file, the function fails.

As mentioned in a similar discussion here on StackOverflow (ShellExecute, "Print") you should keep in mind:

You need to make sure that the machine's associations are configured to handle the print verb.

You referred to pdf, txt, rtf, images which should all be supported I would think by this mechanism.

ShellExecute(NULL, "print", fileNameArray[0], nullptr, nullptr, SW_SHOWNORMAL);

The last parameter might have to be changed (SW_SHOWNORMAL). This code would be put in a loop so you could call it for each file. And note that the above code snippet has not been tested.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164