共用方式為


如何:將資料直接傳送至 XPS 印表機

本主題描述如何將印表機控制項資料直接傳送至使用 XPSDrv 印表機驅動程式的印表機。

下列步驟說明如何將資料直接傳送至印表機。 下列步驟也會在下列程式碼範例中說明。

  1. 呼叫 OpenPrinter 以取得印表機的控制碼。
  2. 使用印表機資料初始化 DOCINFO 結構。
  3. 呼叫 StartDocPrinter ,指出應用程式會將檔資料傳送至印表機。
  4. 呼叫 WritePrinter 以傳送資料。
  5. 呼叫 EndDocPrinter 以指出已傳送此檔的所有資料。
  6. 呼叫 ClosePrinter 以釋放資源。

將印表機控制資料直接傳送至使用 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