如何在預覽列印 UI 中變更標準選項 (HTML)

[ 本文的目標對象是撰寫 Windows 執行階段 App 的 Windows 8.x 和 Windows Phone 8.x 開發人員。如果您正在開發適用於 Windows 10 的 App,請參閱 最新文件 ]

這個教學課程說明如何自訂預覽列印 UI 中的列印選項。

根據預設,預覽列印 UI 會顯示下列選項:

colorMode copies orientation

 

除了上述設定,Windows 還提供數個其他常見的印表機選項,讓您可以新增到預覽列印 UI。以下是其他常見選項。

binding collation duplex holePunch
inputBin mediaSize mediaType nUp
printQuality staple    

 

這些選項會在 standardPrintTaskOptions 類別中定義。您可以在預覽列印 UI 中顯示的選項清單中新增或移除選項。您也可以變更選項出現的順序,以及設定對使用者顯示的預設設定。

不過,您使用這個方法所做的修改只會影響預覽列印 UI。只要按一下預覽列印 UI 中的 [更多設定],使用者就一律可以存取印表機支援的所有選項。

雖然您的應用程式可以指定要顯示的任何列印選項,但是只有選定的印表機支援的選項才會在預覽列印 UI 中顯示。預覽列印 UI 不會顯示選定的印表機不支援的選項。

藍圖: 這個主題與其他主題的相關性?請參閱:

您必須知道的事

技術

先決條件

  • 您必須熟悉 HTML、JavaScript、Windows 事件和事件處理。
  • Microsoft Visual Studio 必須已經安裝完成。
  • 印表機必須已經安裝完成。
  • 您必須有要在其中新增列印按鈕的 Windows 市集應用程式。如果您沒有自己的應用程式,可以下載列印範例範例應用程式並使用該應用程式。
  • 您的應用程式必須已經支援基本的 Windows 列印。如果不支援,請檢閱快速入門:從應用程式列印,了解如何在應用程式中新增基本的 Windows 列印支援。

指示

步驟 1: 在 Visual Studio 中開啟您的應用程式

本教學課程中描述的程序參考列印範例範例應用程式中的 PrintSampleJS 應用程式。如果要自訂您自己的應用程式的預覽列印 UI,請在 Visual Studio 中開啟您的應用程式,而不是開啟列印範例範例應用程式

  1. 開啟列印範例範例應用程式,並將 JavaScript 範例下載到電腦上。
  2. 在 Visual Studio 中,按一下 [File] > [Open Project],然後前往包含範例應用程式 (上個步驟下載的應用程式) 方案檔的資料夾。
  3. 選取 PrintSampleJS 方案檔,然後按一下 [開啟]。

步驟 2: 建置和測試應用程式

  1. 按一下 [建置]**** > [建置方案],建置您要使用的應用程式。確定畫面底端的 [輸出]**** 窗格未出現任何錯誤訊息。
  2. 按一下 [偵錯] > [啟動但不偵錯]****。
  3. 請確認畫面會在數秒後顯示 [列印 JS 範例]** 應用程式。
  4. 如果應用程式執行無誤,請返回 Visual Studio,然後按一下 [偵錯] > [停止偵錯]****。

步驟 3: 定義要顯示的列印選項

應用程式的畫面載入時,會登錄列印協定。該登錄包含定義 PrintTaskRequested 事件處理常式。自訂預覽列印 UI 中所顯示選項的程式碼會新增到 PrintTaskRequested 事件處理常式中。

修改 PrintTaskRequested 事件處理常式以包含 printTask.options 指令,這些指令可用來設定您要在預覽列印 UI 中顯示的列印設定。

  1. 在應用程式中尋找 PrintTaskRequested 事件處理常式。在列印範例範例應用程式中,基本 PrintTaskRequested 事件處理常式看起來就像這個範例。

    function onPrintTaskRequested(printEvent) {
        var printTask = printEvent.request.createPrintTask("Print Sample", function (args) {
            args.setSource(MSApp.getHtmlPrintDocumentSource(document));
    
            // Register the handler for print task completion event
            printTask.oncompleted = onPrintTaskCompleted;
        });
    }
    
    function onPrintTaskCompleted(printTaskCompletionEvent) {
        // Notify the user about the failure
        if (printTaskCompletionEvent.completion === Windows.Graphics.Printing.PrintTaskCompletion.failed) {
            WinJS.log && WinJS.log("Failed to print.", "sample", "error");
        }
    }
    
  2. 在預覽列印 UI 中新增要顯示的列印選項。選項會依您新增的順序,從上到下出現在預覽列印 UI 中。您新增的第一個選項將位於最上方,其餘選項則依序位於其下方。

    注意  這個程式碼來自列印範例範例應用程式的 scenario3.js。如果您要將這個程式碼新增到應用程式中,請在預覽列印 UI 中指派要對使用者顯示的列印選項。

     

    重要  呼叫 printTask.options.displayedOptions.clear() 會移除預覽列印 UI 中的所有列印選項,僅留下 [更多設定] 連結。務必使用 append 方法,指定您要在預覽列印 UI 上顯示的選項。

     

    function onPrintTaskRequested(printEvent) {
        var printTask = printEvent.request.createPrintTask("Print Sample", function (args) {
            args.setSource(MSApp.getHtmlPrintDocumentSource(document));
    
            // Choose the printer options to be shown.
            // The order in which the options are appended determines the order in which they appear in the UI
            printTask.options.displayedOptions.clear();
            printTask.options.displayedOptions.append(Windows.Graphics.Printing.StandardPrintTaskOptions.copies);
            printTask.options.displayedOptions.append(Windows.Graphics.Printing.StandardPrintTaskOptions.mediaSize);
            printTask.options.displayedOptions.append(Windows.Graphics.Printing.StandardPrintTaskOptions.orientation);
            printTask.options.displayedOptions.append(Windows.Graphics.Printing.StandardPrintTaskOptions.duplex);
    
            // Preset the default value of the printer option
            printTask.options.mediaSize = Windows.Graphics.Printing.PrintMediaSize.northAmericaLegal;
    
            // Register the handler for print task completion event
            printTask.oncompleted = onPrintTaskCompleted;
        });
    }
    
  3. 依照上面所述建置和測試您的應用程式。

備註

如需 Windows 市集應用程式中的其他列印情況,請參閱列印範例範例應用程式

相關主題

Windows 市集應用程式列印範例

快速入門:從應用程式列印

開發具備列印功能的 Windows 市集應用程式的最佳做法