MESCIUS SPREAD for Windows Forms 17.0J サンプルコード集 > 印刷 > ユーザー定義印刷を行う(既存クラスの継承) |
デフォルトの印刷機能では1シート/1ページとなりますが、ユーザー定義印刷機能を用いることによって複数シート/1ページなどの自由なレイアウト作成することができます。以下のサンプルではフォーム上に2つのSPREADを配置し、印刷時には各シートの出力位置等を指定することで2シート/1ページでの出力を実現しています。(System.Drawing.Printing.PrintDocumentクラスを継承したサブクラスを作成)
//新しいPrintPreviewDialogコントロールのインスタンスを生成します private PrintPreviewDialog PrintPreviewDialog1 = new PrintPreviewDialog(); private void Form1_Load(object sender, System.EventArgs e) { fpSpread1.ActiveSheet.Columns.Default.Width = 60; fpSpread1.ActiveSheet.RowCount = 5; fpSpread1.ActiveSheet.ColumnCount = 6; fpSpread1.ActiveSheet.DefaultStyle.CellType = new FarPoint.Win.Spread.CellType.NumberCellType(); fpSpread1.ActiveSheet.DefaultStyle.ForeColor = Color.Red; fpSpread1.ActiveSheet.ColumnHeader.Rows[0].BackColor = Color.LightPink; for (int i = 0; i <= fpSpread1.ActiveSheet.RowCount -1 ; i++) { for (int j = 0; j <= fpSpread1.ActiveSheet.ColumnCount -1 ; j++) { fpSpread1.ActiveSheet.SetValue(i, j, i + j); } } fpSpread2.ActiveSheet.Columns.Default.Width = 60;? fpSpread2.ActiveSheet.RowCount = 5; fpSpread2.ActiveSheet.ColumnCount = 8; fpSpread2.ActiveSheet.DefaultStyle.CellType = new FarPoint.Win.Spread.CellType.NumberCellType(); fpSpread2.ActiveSheet.DefaultStyle.ForeColor = Color.Blue; fpSpread2.ActiveSheet.ColumnHeader.Rows[0].BackColor = Color.LightCyan; for (int i = 0; i <= fpSpread2.ActiveSheet.RowCount -1 ; i++) { for (int j = 0; j <= fpSpread2.ActiveSheet.ColumnCount -1 ; j++) { fpSpread2.ActiveSheet.SetValue(i, j, i + j); } } } private void button1_Click(object sender, System.EventArgs e) { //各シートのPrintInfoを設定します fpSpread1.ActiveSheet.PrintInfo.ShowColor = true; fpSpread1.ActiveSheet.PrintInfo.ShowGrid = false; fpSpread1.ActiveSheet.PrintInfo.ShowRowHeaders = false; fpSpread2.ActiveSheet.PrintInfo.ShowColor = true; fpSpread2.ActiveSheet.PrintInfo.ShowGrid = false; fpSpread2.ActiveSheet.PrintInfo.ShowRowHeaders = false; //ユーザー定義印刷クラスSPREADコントロールを渡します。 OwnerPrintDocument aDoc = new OwnerPrintDocument(fpSpread1, fpSpread2); //各シートのタイトルを設定します aDoc.Title1 = "FpSpread1のタイトル"; aDoc.Title2 = "FpSpread2のタイトル"; //PrintPreview にユーザー定義の印刷ドキュメントを設定します PrintPreviewDialog1.Document = aDoc; PrintPreviewDialog1.PrintPreviewControl.Zoom = 0.75; //印刷プレビューを表示します PrintPreviewDialog1.ShowDialog(); }
'新しいPrintPreviewDialogコントロールのインスタンスを生成します Private PrintPreviewDialog1 As New PrintPreviewDialog Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load FpSpread1.ActiveSheet.Columns.Default.Width = 60 FpSpread1.ActiveSheet.RowCount = 5 FpSpread1.ActiveSheet.ColumnCount = 6 FpSpread1.ActiveSheet.DefaultStyle.CellType = New FarPoint.Win.Spread.CellType.NumberCellType FpSpread1.ActiveSheet.DefaultStyle.ForeColor = Color.Red FpSpread1.ActiveSheet.ColumnHeader.Rows(0).BackColor = Color.LightPink For i As Integer = 0 To FpSpread1.ActiveSheet.RowCount - 1 For j As Integer = 0 To FpSpread1.ActiveSheet.ColumnCount - 1 FpSpread1.ActiveSheet.SetValue(i, j, i + j) Next Next FpSpread2.ActiveSheet.Columns.Default.Width = 60 FpSpread2.ActiveSheet.RowCount = 5 FpSpread2.ActiveSheet.ColumnCount = 8 FpSpread2.ActiveSheet.DefaultStyle.CellType = New FarPoint.Win.Spread.CellType.TextCellType FpSpread2.ActiveSheet.DefaultStyle.ForeColor = Color.Blue FpSpread2.ActiveSheet.ColumnHeader.Rows(0).BackColor = Color.LightCyan For i As Integer = 0 To FpSpread2.ActiveSheet.RowCount - 1 For j As Integer = 0 To FpSpread2.ActiveSheet.ColumnCount - 1 FpSpread2.ActiveSheet.SetValue(i, j, "Cell:" + Convert.ToString(i + j)) Next Next End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ''各シートのPrintInfoを設定します FpSpread1.ActiveSheet.PrintInfo.ShowColor = True FpSpread1.ActiveSheet.PrintInfo.ShowGrid = False FpSpread1.ActiveSheet.PrintInfo.ShowRowHeaders = False FpSpread2.ActiveSheet.PrintInfo.ShowColor = True FpSpread2.ActiveSheet.PrintInfo.ShowGrid = False FpSpread2.ActiveSheet.PrintInfo.ShowRowHeaders = False ''ユーザー定義印刷クラスSPREADコントロールを渡します Dim aDoc = New OwnerPrintDocument(FpSpread1, FpSpread2) ''各シートのタイトルを設定します aDoc.Title1 = "FpSpread1のタイトル" aDoc.Title2 = "FpSpread2のタイトル" ''PrintPreview にユーザー定義の印刷ドキュメントを設定します PrintPreviewDialog1.Document = aDoc PrintPreviewDialog1.PrintPreviewControl.Zoom = 0.75 ''印刷プレビューを表示します PrintPreviewDialog1.ShowDialog() End Sub
[Serializable()] public class OwnerPrintDocument : System.Drawing.Printing.PrintDocument //PrintDocumentクラスを継承します { //印刷する2つのSPREAD private FarPoint.Win.Spread.FpSpread op_Spread1; private FarPoint.Win.Spread.FpSpread op_Spread2; //各タイトル public string Title1; public string Title2; public OwnerPrintDocument(FarPoint.Win.Spread.FpSpread Spread_1, FarPoint.Win.Spread.FpSpread Spread_2) : base() { op_Spread1 = Spread_1; op_Spread2 = Spread_2; } protected override void OnBeginPrint(System.Drawing.Printing.PrintEventArgs ev) { //OnBeginPrintメソッドをオーバーライドします base.OnBeginPrint(ev); } protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs e) { //OnPrintPage メソッドをオーバーライドします base.OnPrintPage(e); //******************** // FpSpread1の出力 //******************** //描画位置を設定します Rectangle rect1 = new Rectangle(e.PageBounds.X + 30, e.PageBounds.Y + 40, e.PageBounds.Width / 2, e.PageBounds.Height / 2); //必要なページ数を取得します int cnt1 = op_Spread1.GetOwnerPrintPageCount(e.Graphics, rect1, 0); //印刷するページが存在する場合のみ出力します if (cnt1 > 0) { op_Spread1.OwnerPrintDraw(e.Graphics, rect1, 0, cnt1); e.HasMorePages = false; } //************************************** // FpSpread1上部にタイトルを描画します //************************************** RectangleF drect1 = new RectangleF(); drect1.X = e.PageBounds.X + 30; drect1.Y = e.PageBounds.Y + 10; drect1.Width = e.PageBounds.Width / 2; drect1.Height = e.PageBounds.Height / 2; Brush b1 =new SolidBrush(Color.Red); e.Graphics.DrawString(Title1, new Font("MS Pゴシック", 14, FontStyle.Bold | FontStyle.Italic), b1, drect1); b1.Dispose(); //******************** // FpSpread2の出力 //******************** //描画位置を設定します Rectangle rect2 = new Rectangle(e.PageBounds.X + 30, e.PageBounds.Y + 210, e.PageBounds.Width - 100, e.PageBounds.Height / 2); //必要なページ数を取得します int cnt2 = op_Spread2.GetOwnerPrintPageCount(e.Graphics, rect2, 0); //印刷するページが存在する場合のみ出力します if (cnt2 > 0) { op_Spread2.OwnerPrintDraw(e.Graphics, rect2, 0, cnt2); e.HasMorePages = false; } //************************************** // FpSpread2上部にタイトルを描画します //************************************** RectangleF drect2 = new RectangleF(); drect2.X = e.PageBounds.X + 180; drect2.Y = e.PageBounds.Y + 180; drect2.Width = e.PageBounds.Width / 2; drect2.Height = e.PageBounds.Height / 2; Brush b2 =new SolidBrush(Color.Blue); e.Graphics.DrawString(Title2, new Font("Times New Roman", 18, FontStyle.Underline), b2, drect2); b2.Dispose(); } }
<Serializable()> Public Class OwnerPrintDocument 'PrintDocumentクラスを継承します Inherits System.Drawing.Printing.PrintDocument '印刷する2つのSPREAD Private op_Spread1 As FarPoint.Win.Spread.FpSpread Private op_Spread2 As FarPoint.Win.Spread.FpSpread '各タイトル Public Title1 As String Public Title2 As String Public Sub New(ByVal Spread_1 As FarPoint.Win.Spread.FpSpread, ByVal Spread_2 As FarPoint.Win.Spread.FpSpread) op_Spread1 = Spread_1 op_Spread2 = Spread_2 End Sub Protected Overrides Sub OnBeginPrint(ByVal e As System.Drawing.Printing.PrintEventArgs) 'OnBeginPrintメソッドをオーバーライドします MyBase.OnBeginPrint(e) End Sub Protected Overrides Sub OnPrintPage(ByVal e As System.Drawing.Printing.PrintPageEventArgs) 'OnPrintPageメソッドをオーバーライドします MyBase.OnPrintPage(e) '******************** ' FpSpread1の出力 '******************** '描画位置を設定します Dim rect1 As New Rectangle(e.PageBounds.X + 30, e.PageBounds.Y + 40, e.PageBounds.Width / 2, e.PageBounds.Height / 2) '必要なページ数を取得します Dim cnt1 As Integer = op_Spread1.GetOwnerPrintPageCount(e.Graphics, rect1, 0) '印刷するページが存在する場合のみ出力します If cnt1 > 0 Then op_Spread1.OwnerPrintDraw(e.Graphics, rect1, 0, cnt1) e.HasMorePages = False End If '************************************** ' FpSpread1上部にタイトルを描画します '************************************** Dim drect1 As New RectangleF drect1.X = e.PageBounds.X + 30 drect1.Y = e.PageBounds.Y + 10 drect1.Width = e.PageBounds.Width / 2 drect1.Height = e.PageBounds.Height / 2 Dim b1 As Brush = New SolidBrush(Color.Red) e.Graphics.DrawString(Title1, New Font("MS Pゴシック", 14, FontStyle.Bold Or FontStyle.Italic), b1, drect1) b1.Dispose() '******************** ' FpSpread2の出力 '******************** '描画位置を設定します Dim rect2 As New Rectangle(e.PageBounds.X + 30, e.PageBounds.Y + 210, e.PageBounds.Width - 100, e.PageBounds.Height / 2) '必要なページ数を取得します Dim cnt2 As Integer = op_Spread2.GetOwnerPrintPageCount(e.Graphics, rect2, 0) '印刷するページが存在する場合のみ出力します If cnt2 > 0 Then op_Spread2.OwnerPrintDraw(e.Graphics, rect2, 0, cnt2) e.HasMorePages = False End If '************************************** ' FpSpread2上部にタイトルを描画します '************************************** Dim drect2 As New RectangleF drect2.X = e.PageBounds.X + 180 drect2.Y = e.PageBounds.Y + 180 drect2.Width = e.PageBounds.Width / 2 drect2.Height = e.PageBounds.Height / 2 Dim b2 As Brush = New SolidBrush(Color.Blue) e.Graphics.DrawString(Title2, New Font("Times New Roman", 18, FontStyle.Underline), b2, drect2) b2.Dispose() End Sub End Class