簡體   English   中英

如何從 Web 服務器打印格式化文本並確認打印成功?

[英]How to print formatted text from a web server with confirmation of successful printing?

我正在嘗試構建一個系統,該系統將從 Web 服務器下載格式化文本,打印格式化文本,確認打印作業成功完成,然后響應 Web 服務器以使其知道文本已打印。 全部無需用戶輸入。

我已經成功地使用Web 瀏覽器控件下載 HTML,然后在不需要用戶輸入的情況下打印它。 然而,這不足以確認打印。

看起來在System.Printing 中,您可以訪問 PrintServer 和 PrintQueue,並使用它們來啟動打印作業並查找打印作業的狀態。

我還沒有能夠確認打印作業,但是我已經能夠啟動簡單的打印。 但是,這不攜帶來自 Web 服務器的任何 HTML 格式。 我不依賴於 HTML,但它必須是一些可以由 Web 服務器生成的格式,以便可以在不需要更新客戶端應用程序的情況下對其進行更改。

如何打印來自 Web 服務器的輸出,格式正確,並知道打印作業是成功還是失敗?

我假設您願意使用 WebBrowser 控件。 這是確認打印的解決方案。 基本上,您需要處理PrintTemplateTeardown事件以等待打印作業完成。

以下是從答案中提取的示例代碼: Print html document from Windows Service without print dialog

using System.Reflection;
using System.Threading;
using SHDocVw;

namespace HTMLPrinting
{
  public class HTMLPrinter
  {
    private bool documentLoaded;
    private bool documentPrinted;

    private void ie_DocumentComplete(object pDisp, ref object URL)
    {
      documentLoaded = true;
    }

    private void ie_PrintTemplateTeardown(object pDisp)
    {
      documentPrinted = true;
    }

    public void Print(string htmlFilename)
    {
      documentLoaded = false;
      documentPrinted = false;

      InternetExplorer ie = new InternetExplorerClass();
      ie.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(ie_DocumentComplete);
      ie.PrintTemplateTeardown += new DWebBrowserEvents2_PrintTemplateTeardownEventHandler(ie_PrintTemplateTeardown);

      object missing = Missing.Value;

      ie.Navigate(htmlFilename, ref missing, ref missing, ref missing, ref missing);
      while (!documentLoaded && ie.QueryStatusWB(OLECMDID.OLECMDID_PRINT) != OLECMDF.OLECMDF_ENABLED)
        Thread.Sleep(100);

      ie.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref missing, ref missing);
      while (!documentPrinted)
        Thread.Sleep(100);

      ie.DocumentComplete -= ie_DocumentComplete;
      ie.PrintTemplateTeardown -= ie_PrintTemplateTeardown;
      ie.Quit();
    }
  }
}

也可以參考: https : //jiangsheng.net/2021/03/24/how-to-determine-when-a-page-is-done-printing-in-webbrowser-control/

希望能幫助到你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM