biz-Streamマニュアル Webサービスサーバ ガイド 第12章 サンプルプログラム(.NET) 12.4 アプリケーションでデータソース を設定してPDF を生成しブラウザでダウンロードさせるサンプル(WCF)

12.4 アプリケーションでデータソース を設定してPDF を生成しブラウザでダウンロードさせるサンプル(WCF)

概要

アプリケーションでデータソースを設定してPDFを生成しブラウザでダウンロードさせるサンプルプログラムです


スタブクラスの準備

事前に『10.2.6 .NET で開発するための環境構築』の手順を実行します。


サンプル構成

項目 ファイルパス
ドキュメントレイアウトサンプル <bswss-client_home>/sample/xml/Sample4-doc.xml サンプル1
ページレイアウトサンプル <bswss-client_home>/sample/xml/Sample4-page.xml サンプル1
ソースサンプル <bswss-client_home>/sample/dotNET/wcf/WSS_WCF_Sample4.aspx.cs サンプル1

ソースサンプル


    using System;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.Web;
    using BizStreamWebApplication.BizStreamServiceReference;


    namespace BizStreamWebApplication
    {
        public partial class WSS_WCF_Sample4 : System.Web.UI.Page
        {
            // Webサービスサーバのホスト名
            const string WSS_HOST_NAME = "wsssrv";

            // 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\\Sample4-doc.xml";

            // ダウンロードファイル名
            const string DOWNLOAD_FILE_NAME = "WSS_WCF_Sample4.pdf";

            const string RESOURCE_NAME = "resourceData";

            const int RECORD_NUM = 10;


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

                // HTTPヘッダを設定する
                Response.Clear();
                Response.HeaderEncoding = System.Text.Encoding.GetEncoding("SJIS");
                Response.ContentType = "application/pdf";
                Response.AppendHeader("Content-length", data.Length.ToString());
                Response.AppendHeader("Accept-Ranges", "bytes");
                Response.AppendHeader("Content-Disposition", "attachment; filename=\"" +
                    DOWNLOAD_FILE_NAME + "\"");
                Response.Flush();

                // PDFをストリームに書き出す
                Response.BinaryWrite(data);
                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }

            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.resource = new resourceData[1];
                request.resource[0] = makeResourceData();

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

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

                return data.contentData.Value;
            }

            private resourceData makeResourceData()
            {
                resourceData resData = new resourceData();
                resData.name = RESOURCE_NAME;
                resData.applicationData = new applicationData[3];

                string[,] headerNames = new string[3, 3] {
                    { "PROJNO", "PROJNAME", "PRSTAFF" },
                    { "INVOICE NO", "MODEL", "SEIHAN NO" },
                    { "製品コード", "製品名", "数量" } };
                for (int ds_NO = 0; ds_NO < 3; ds_NO++)
                {
                    resData.applicationData[ds_NO] = new applicationData();
                    resData.applicationData[ds_NO].masterName = "app-resource" + (ds_NO + 1);
                    recordData recData = new recordData();
                    recData.header = new headerColumn[3];
                    for (int col_NO = 0; col_NO < 3; col_NO++)
                    {
                        headerColumn hCol = new headerColumn();
                        hCol.value = headerNames[ds_NO, col_NO];
                        recData.header[col_NO] = hCol;
                    }
                    recData.detail = new detail[RECORD_NUM];
                    for (int record_NO = 0; record_NO < RECORD_NUM; record_NO++)
                    {
                        recData.detail[record_NO] = new detail();
                        recData.detail[record_NO].detailColumn = new detailColumn[3];
                        for (int col_NO = 0; col_NO < 3; col_NO++)
                        {
                            recData.detail[record_NO].detailColumn[col_NO] =
                                new detailColumn();
                            recData.detail[record_NO].detailColumn[col_NO].value =
                                "AAA" + ds_NO + "_" + record_NO + "_" + col_NO;
                        }
                    }
                    resData.applicationData[ds_NO].Item = recData;
                }
                return resData;
            }
        }
    }