biz-Streamマニュアル Webサービスサーバ ガイド 第12章 サンプルプログラム(.NET) 12.9 ダイレクト印刷を利用するサンプル(WCF)

12.9 ダイレクト印刷を利用するサンプル(WCF)

概要

ダイレクト印刷を利用するサンプルプログラムです。


環境準備

事前に『2.7 クライアントアプリケーションの環境構築』を確認してください。



ソースサンプル

ダイレクト印刷を行うサンプルプログラムです。

    using System;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.Web;
    using BizStreamWebApplication.BizStreamServiceReference;
    using com.brainsellers.bizstream.directprint;

    namespace BizStreamWebApplication
    {
        public partial class WSS_WCF_Sample9 : System.Web.UI.Page
        {
            // Webサービスサーバのホスト名
            const string WSS_HOST_NAME = "wsssrv";
            
            // ユーザアプリケーションサーバのホスト名:ポート名
            const string AP_HOST_NAME = "localhost:50000";

            // プリンタ名
            const string PRINTER_NAME = "FinePrint";
 
            // Webサービスサーバ上のサンプルディレクトリの場所
            const string WSS_BIZSTREAM_SAMPLE_DIR = "\\bs\\sample";

            // Webサービスのエンドポイントプリフィックス
            const string WSS_SERVICE_LOCATION = "http://" + WSS_HOST_NAME + ":8080/axis2/services";

            // BASIC認証のユーザ名
            const string USER_NAME = @"bizuser1";

            // BASIC認証のパスワード
            const string PASSWORD = @"bizuser1";

            // 監査ロギング用のユーザ定義
            const string USER_DEF = "AAA";
            
            // 入力レイアウト定義ファイル名
            const string LAYOUT_FILE = WSS_BIZSTREAM_SAMPLE_DIR + "\\xml\\Sample1.xml";
        
            // 印刷結果応答URL
            const string RESPONSE_URL = "http://" + AP_HOST_NAME + "/WSS_WCF_Sample9_1.aspx";

            protected void Page_Load(object sender, EventArgs e)
            {
                // データの生成
                byte[] data = generate();

                // ダイレクト印刷ストリームの作成
                PDFDirectPrintStream direct = new PDFDirectPrintStream(Response);
                // 印刷応答URL
                direct.setResponseUrl(RESPONSE_URL);
                // 出力PDFファイル
    //            direct.setSaveFileName("C:/Temp/WSS_WCF_Sample9.pdf");
                // プリンタ名
                direct.setPrinterName(PRINTER_NAME);
                // 印刷部数
                direct.setNumberOfCopy(1);
                // 出力トレイ
                direct.setSelectedTray(PDFDirectPrintStream.TRAY_AUTO);
                // 印刷ジョブ名
                direct.setJobName("WSS_WCF_Sample9");
                // 印刷ダイアログ表示
                direct.setPrintDialog(false);
                // ブラウザのターゲットフレーム名 (IEでのみ有効)
                direct.setTarget("bizPrint");
                // sppファイル暗号化パスワード
    //            direct.setPassword("password");
                // エンコード済み暗号化パスワード
    //            direct.setPasswordWithEncoded("cGFzc3dvcmQ=");
                // 用紙に合わせて印刷
                direct.setDoFit(true);
                // 印刷開始ページ
                direct.setFromPage(1);
                // 印刷終了ページ
                direct.setToPage(-1);

                // PDFをストリームに書き出す
                direct.Write(data);
                direct.Close();
            }

            private byte[] generate()
            {
                // クライアントを生成
                bizstreamPortTypeClient client =
                    new bizstreamPortTypeClient("BizstreamSOAP11port",
                        WSS_SERVICE_LOCATION + "/bizstream?UserDef=" + USER_DEF);

                // 認証の設定
                BindingElementCollection elements = client.Endpoint.Binding.CreateBindingElements();
                elements.Find<HttpTransportBindingElement>().AuthenticationScheme =
                    System.Net.AuthenticationSchemes.Basic;
                client.ClientCredentials.UserName.UserName = USER_NAME;
                client.ClientCredentials.UserName.Password = PASSWORD;

                // MTOMを有効化
                elements.Remove<TextMessageEncodingBindingElement>();
                MtomMessageEncodingBindingElement mtom = new MtomMessageEncodingBindingElement();
                mtom.ReaderQuotas.MaxStringContentLength = 100000;
                elements.Insert(0, mtom);
                elements.Find<HttpTransportBindingElement>().KeepAliveEnabled = false;
                client.Endpoint.Binding = new CustomBinding(elements);

                // リクエストメッセージの作成
                generateDataRequest request = new generateDataRequest();

                // 出力ファイルの種類を設定
                request.dataType = new dataType();
                request.dataType.Item = new pdf();

                // レイアウト定義を設定
                request.layoutData = new layoutDefinition[1];
                request.layoutData[0] = new layoutDefinition();
                request.layoutData[0].uri = LAYOUT_FILE;

                // リクエストを送信
                fileData data = client.generateData(request);

                return data.contentData.Value;
            }
        }
    }

    


印刷応答URLとして呼び出す、印刷結果の処理サンプルプログラムです。

    using System;
    using System.Web;

    namespace BizStreamWebApplication
    {
        public partial class WSS_WCF_Sample9_1 : System.Web.UI.Page
        {

            protected void Page_Load(object sender, EventArgs e)
            {
                string result = Request.QueryString.Get("RESULT"); // 印刷結果
                string errCode = Request.QueryString.Get("ERROR_CODE"); // エラーコード
                string errCause = Request.QueryString.Get("ERROR_CAUSE"); // エラー原因
                string errDetails = Request.QueryString.Get("ERROR_DETAILS"); // エラー内容
                if (!String.IsNullOrEmpty(errDetails))
                {
                    errDetails = HttpUtility.UrlDecode(errDetails);
                }

                // ブラウザに結果を表示
                Response.Clear();
                Response.ContentType = "text/html;charset=UTF-8";
                Response.Write("<HTML><HEAD><TITLE>WSS_WCF_Sample9_1</TITLE></HEAD><BODY6gt;");
                Response.Write("<BR>RESULT = " + result); // 印刷結果
                Response.Write("<BR>ERROR_CODE = " + errCode); // エラーコード
                Response.Write("<BR>ERROR_CAUSE = " + errCause); // エラー原因
                Response.Write("<BR>ERROR_DETAILS = " + errDetails); // エラー内容
                Response.Write("<BR></BODY></HTML>");
                Response.Flush();
                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }

        }
    }