Condividi tramite


Procedura: Inviare dati direttamente a una stampante XPS

Questo argomento descrive come inviare i dati di controllo della stampante direttamente alle stampanti che usano i driver della stampante XPSDrv.

I passaggi seguenti descrivono come inviare dati direttamente a una stampante. Questi passaggi sono illustrati anche nell'esempio di codice che segue.

  1. Chiamare OpenPrinter per ottenere un handle nella stampante.
  2. Inizializzare una struttura DOCINFO con i dati della stampante.
  3. Chiamare StartDocPrinter per indicare che l'applicazione invia i dati del documento alla stampante.
  4. Chiamare WritePrinter per inviare i dati.
  5. Chiamare EndDocPrinter per indicare che tutti i dati per questo documento sono stati inviati.
  6. Chiamare ClosePrinter per rilasciare le risorse.

Inviare i dati di controllo della stampante direttamente alle stampanti che usano i driver della stampante XPSDrv.

// 
//  RawDataToXpsPrinter - sends binary data directly to a printer 
//          with an XPSDrv Printer Driver 
//  
// szPrinterName: NULL-terminated string specifying printer name 
// lpData:        Pointer to raw data bytes 
// dwCount        Length of lpData in bytes 
//  
// Returns: TRUE for success, FALSE for failure. 
//  
BOOL RawDataToXpsPrinter (LPTSTR szPrinterName, LPBYTE lpData, DWORD dwCount)
{
    BOOL     bStatus = FALSE;
    HANDLE     hPrinter = NULL;
    DOC_INFO_1       DocInfo;
    DWORD    dwPrtJob = 0L;
    DWORD    dwBytesWritten = 0L;

    // Open a handle to the printer. 
    bStatus = OpenPrinter (szPrinterName, &hPrinter, NULL);
    
    if (bStatus) {
        // Fill in the structure with info about this "document." 
        DocInfo.pDocName = (LPTSTR)_T("My Document");
        DocInfo.pOutputFile = NULL;

        // Enter the datatype of this buffer.
        //  Use "XPS_PASS" when the data buffer should bypass the 
        //    print filter pipeline of the XPSDrv printer driver. 
        //    This datatype would be used to send the buffer directly 
        //    to the printer, such as when sending print head alignment 
        //    commands. Normally, a data buffer would be sent as the
        //    "RAW" datatype.
        //
        DocInfo.pDatatype = (LPTSTR)_T("XPS_PASS");

        dwPrtJob = StartDocPrinter (
                        hPrinter,
                        1,
                        (LPBYTE)&DocInfo);

        if (dwPrtJob > 0) {
                // Send the data to the printer. 
                bStatus = WritePrinter (
                hPrinter,
                lpData,
                dwCount,
                &dwBytesWritten);
        }
        
        EndDocPrinter (hPrinter);

        // Close the printer handle. 
        bStatus = ClosePrinter(hPrinter);
    }
    
    if (!bStatus || (dwCount != dwBytesWritten)) {
        bStatus = FALSE;
    } else {
        bStatus = TRUE;
    }

    return bStatus;
}

ClosePrinter

EndDocPrinter

EndPagePrinter

OpenPrinter

StartDocPrinter

StartPagePrinter

WritePrinter