12.8 PDF を生成しリポジトリに格納後、ファイルシステムへ出力するサンプル(WCF)
概要
PDF を生成しリポジトリに格納後、ファイルシステムへ出力するサンプルプログラムです
サンプル構成
| 項目 | ファイルパス | 
|---|---|
| ページレイアウトサンプル | <bswss-client_home>/sample/xml/Sample1.xml   | 
| ソースサンプル | <bswss-client_home>/sample/dotNET/wcf/WSS_WCF_Sample8.aspx.cs   | 
ソースサンプル
    using System;
    using System.IO;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.Web;
    using BizStreamWebApplication.BizStreamServiceReference;
    using BizStreamWebApplication.CmsServiceReference;
    namespace BizStreamWebApplication
    {
        public partial class WSS_WCF_Sample8 : 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\\Sample1.xml";
            // 格納するファイルのリポジトリ内のパス
            const string TARGET_FILE_PATH = "/WSS_WCF_Sample8.pdf";
            // 出力ファイル名
            const string OUTPUT_FILE = @"C:\\Temp\\WSS_WCF_Sample8.pdf";
            protected void Page_Load(object sender, EventArgs e)
            {
                // リポジトリにPDFファイルを格納
                generate();
                // リポジトリからPDFファイルを取得
                byte[] data = get();
                // バイト配列をファイルにすべて書き込む
                File.WriteAllBytes(OUTPUT_FILE, data);
                // ブラウザに結果を表示
                Response.Clear();
                Response.ContentType = "text/html;charset=UTF-8";
                Response.Write("<HTML><HEAD6gt;<TITLE>WSS_WCF_Sample8</TITLE></HEAD><BODY>");
                Response.Write("正常終了しました。
");
                Response.Write("リポジトリに格納されたファイル: " + TARGET_FILE_PATH + "
");
                Response.Write("ファイルシステムに出力されたファイル: " + TARGET_FILE_PATH + " -> " + OUTPUT_FILE);
                Response.Write("</BODY></HTML>");
                Response.Flush();
                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }
            private generateOutputResponse 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);
                // リクエストメッセージの作成
                generateOutputRequest request = new generateOutputRequest();
                // 出力ファイルの種類を設定
                file fileData = new file();
                fileData.name = TARGET_FILE_PATH;
                pdf pdfData = new pdf();
                pdfData.Item = fileData;
                output outputData = new output();
                outputData.Item = pdfData;
                request.output = outputData;
                
                // レイアウト定義を設定
                request.layoutData = new layoutDefinition[1];
                request.layoutData[0] = new layoutDefinition();
                request.layoutData[0].uri = LAYOUT_FILE;
                // リクエストを送信
                return client.generateOutput(request);
            }
            private byte[] get()
            {
                // クライアントを生成
                cmsPortTypeClient client =
                    new cmsPortTypeClient("CmsSOAP11port",
                        WSS_SERVICE_LOCATION + "/cms?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);
                
                // リクエストメッセージの作成
                getRequest request = new getRequest();
                request.Value = TARGET_FILE_PATH;
                // リクエストを送信
                BizStreamWebApplication.CmsServiceReference.fileData data = client.get(request);
                return data.contentData.Value;
            }
        }
    }
    
    
