12.16 PDFを生成しファイルシステムへ出力後、ダイレクト印刷するサンプルプログラム(WCF)
概要
PDFを生成しファイルシステムへ出力後、ダイレクト印刷するサンプルプログラムです。
サンプル構成
| 項目 | ファイルパス |
|---|---|
| ソースサンプル |
<bswss-client_home>/sample/dotNET/wcf/WSS_WCF_Sample16.aspx.cs
|
ソースサンプル
using System;
using System.IO;
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_Sample16 : 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";
// Webサービスサーバ上の出力先ファイルパス
const string TARGET_FILE_PATH = "/tmp/WSS_WCF_Sample16.pdf";
// ユーザアプリケーションサーバのホスト名:ポート名
const string AP_HOST_NAME = "localhost:50000";
// プリンタ名
const string PRINTER_NAME = "FinePrint";
// 印刷結果応答URL
const string RESPONSE_URL = "http://" + AP_HOST_NAME + "/WSS_WCF_Sample9_1.aspx";
protected void Page_Load(object sender, EventArgs e)
{
// PDFファイルをファイルシステムへ出力
generateOutputDataResponse response = generate();
// レスポンスより格納したファイルの情報を取得
filesystem s3 = response.filesystem;
String filePath = s3.filePath; // ファイルパス
// ファイルデータの取得
byte[] data = getContentData(response);
// 取得したファイルをダイレクト印刷のストリームに出力
PDFDirectPrintStream direct = makePDFDirectPrintStream(Response);
direct.Write(data);
direct.Close();
}
private generateOutputDataResponse 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);
// リクエストメッセージの作成
generateOutputDataRequest request = new generateOutputDataRequest();
// ファイルシステムへの出力設定
filesystem fs = new filesystem();
fs.filePath = TARGET_FILE_PATH; // 出力先ファイルパス (必須)
// 出力ファイルの種類を設定
pdf pdfData = new pdf();
pdfData.Item = fs;
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.generateOutputData(request);
}
private byte[] getContentData(generateOutputDataResponse response)
{
// コンテンツデータの配列を取得
base64Binary[] list = response.contentDataGroup.contentData;
// PDF, Excelの場合は要素が1つのみ
base64Binary base64 = list[0];
// ファイルデータを取得
return base64.Value;
}
private PDFDirectPrintStream makePDFDirectPrintStream(HttpResponse response)
{
//インスタンス生成
PDFDirectPrintStream direct = new PDFDirectPrintStream(response);
// 印刷応答URL
direct.setResponseUrl(RESPONSE_URL);
// 出力PDFファイル
// direct.setSaveFileName("C:/Temp/WSS_WCF_Sample16.pdf");
// プリンタ名
direct.setPrinterName(PRINTER_NAME);
// 印刷部数
direct.setNumberOfCopy(1);
// 出力トレイ
direct.setSelectedTray(PDFDirectPrintStream.TRAY_AUTO);
// 印刷ジョブ名
direct.setJobName("WSS_WCF_Sample16");
// 印刷ダイアログ表示
direct.setPrintDialog(false);
// ブラウザのターゲットフレーム名(IEでのみ有効)
direct.setTarget("bizPrint");
// sppファイル暗号化パスワード
// direct.setPassword("password");
// エンコード済み暗号化パスワード
// direct.setPasswordWithEncoded("cGFzc3dvcmQ=");
// 用紙に合わせて印刷
direct.setDoFit(true);
// 印刷開始ページ
direct.setFromPage(1);
// 印刷終了ページ
direct.setToPage(-1);
return direct;
}
}
}
