Silverlight-printing
Silverlight-印刷
印刷は、特定の種類のアプリケーションにとって重要な機能です。 この章では、Silverlightの関連機能について説明します。
- 印刷API、およびすべてのSilverlightアプリケーションが印刷する場合に実行する必要のある基本手順。 ウォータープリントを選択するためのさまざまなオプション。
- 最も簡単なのは、すでに画面上にあるユーザーインターフェイス要素のコピーを印刷することです。
- ほとんどのアプリケーションは、これよりも少し高度になり、印刷に特化したコンテンツを生成することを望みます。場合によっては、コンテンツを複数のページに分割する必要があります。
印刷の手順
スナップショットを印刷する場合でも、既に画面上にあるものを印刷する場合でも、完全にカスタマイズされた複数ページの印刷出力を行う場合でも、同じ基本手順が必要です。
- 印刷APIの中核にあるのはPrintDocumentクラスです。
- これらのいずれかを作成することから始め、そのPrintメソッドを呼び出すと、印刷ジョブを開始するための標準ユーザーインターフェイスが表示されます。
- ユーザーは通常どおりプリンターを選択し、設定を構成できます。 ユーザーが Print をクリックして先に進むことを決定した場合、 PrintDocument はすぐに PrintPage イベントを発生させ、そのイベントのハンドラーは印刷するコンテンツを提供します。
- イベント引数は、この目的のために PageVisual プロパティを提供します。 *任意のSilverlightユーザーインターフェイス要素に設定できます。画面に既に表示されている要素、または特に印刷用に作成した新しい要素のいずれかです。
既存の要素の印刷
要素最も簡単なオプションは、Silverlightアプリケーションで既に画面上にあるコンテンツを印刷することです。* PrintPage イベント引数 *PageVisual はすべてのユーザーインターフェイス要素を受け入れるため、ユーザーインターフェイスで何でも選択して印刷できます。
PrintScreenキーを使用してスクリーンショットを取得するのは、わずかなステップアップです。 ユーザーがスクリーンショットをトリミングして印刷するために他のプログラムにスクリーンショットを手動で貼り付ける必要がないため、それはそれよりわずかに優れています。 まだわずかな改善にすぎません。
既に画面上にあるコンテンツの印刷には問題があります。
まず、画面上で機能するレイアウトが紙に適しているという保証はありません。
*ScrollViewer* にいくつかのUI要素とその画面に適合したレイアウトが含まれる簡単な例を見てみましょう。 ブラウザウィンドウのサイズに基づいてサイズが変更され、スクロールバーが提供されて、収まらない場合でもすべてにアクセスできるようにします。
以下にXAMLコードを示します。
<UserControl
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d = "http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sdk = "http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
x:Class = "SilverlightPrinting.MainPage"
mc:Ignorable = "d"
d:DesignHeight = "300" d:DesignWidth = "500">
<Grid x:Name = "LayoutRoot" Background = "White">
<Button x:Name = "print" Content = "Print" Click = "print_Click" Width = "60"
Height = "20" Margin = "10,10,430,270"/>
<ScrollViewer x:Name = "myScrollViewer"
HorizontalScrollBarVisibility = "Auto"
VerticalScrollBarVisibility = "Auto"
Width = "400" Margin = "90,0,10,0">
<StackPanel>
<Rectangle Fill = "Gray" Width = "100" Height = "100"/>
<Button x:Name = "button" Content = "Button" Width = "75"/>
<sdk:Calendar Height = "169" Width = "230"/>
<Rectangle Fill = "AliceBlue" Width = "475" Height = "100"/>
</StackPanel>
</ScrollViewer>
</Grid>
</UserControl>
*ScrollViewer* とその表示データを印刷する* Printボタン*クリックイベントの実装を次に示します。
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Printing;
namespace SilverlightPrinting {
public partial class MainPage : UserControl {
public MainPage() {
InitializeComponent();
}
private void print_Click(object sender, RoutedEventArgs e) {
PrintDocument pd = new PrintDocument();
pd.PrintPage += new System.EventHandler<PrintPageEventArgs>(pd_PrintPage);
pd.Print("Print Screen Content");
}
private void pd_PrintPage(object sender, PrintPageEventArgs e) {
e.PageVisual = myScrollViewer;
}
}
}
- ご覧のように、 PrintDocument オブジェクトが作成される Print button click event で、PrintPageイベントにハンドラーをアタッチします。
- ScrollViewer を参照するように PageVisual プロパティを設定できます。
- 次に* Printメソッド*が呼び出されます。 これは文字列を取り、印刷キューのジョブ名として表示されます。
上記のコードをコンパイルして実行すると、次の出力が表示されます。
[印刷]ボタンをクリックすると、標準の[印刷]ダイアログが表示されます。
次に、デフォルトのプリンターを選択します。 デモンストレーションのために、 OneNote を選択し、印刷*ボタンをクリックします。 *ScrollViewer が印刷されていることがわかります。
スクロールバーは ScrollViewer にまだ表示されていることに注意してください。
カスタムUIツリー
すでに画面上にあるコンテンツを印刷する代わりに、通常、印刷専用のユーザーインターフェイス要素のツリーを構築する方が適切です。 これにより、紙上で非インタラクティブな要素のみを使用し、紙の形状とサイズにより適した特殊なレイアウトを作成できます。 印刷専用のUserControlを作成できます。
Silverlightプロジェクトを作成し、 PrintLayout という UserControl を追加して、簡単な例を見てみましょう。
設計時間の幅と高さをほぼ紙の形に設定します。 以下に、 PrintLayout.xaml ファイルのXAMLコードを示します。
<UserControl x:Class = "PrintCustomUI.PrintLayout"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d = "http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable = "d"
d:DesignHeight = "768" d:DesignWidth = "960">
<Grid x:Name = "LayoutRoot" Background = "White">
<Grid.RowDefinitions>
<RowDefinition Height = "Auto"/>
<RowDefinition/>
<RowDefinition Height = "Auto"/>
</Grid.RowDefinitions>
<TextBlock Text = "Silverlight" HorizontalAlignment = "Center"
FontSize = "60" FontWeight = "Bold" FontFamily = "Georgia"/>
<TextBlock Grid.Row = "2" Text = "Print Testing"
HorizontalAlignment = "Center" FontFamily = "Georgia"
FontSize = "24" Margin = "0,10"/>
<Rectangle Grid.Row = "2" Height = "1" Fill = "Black"
VerticalAlignment = "Top"/>
<Ellipse Grid.Row = "1" Stroke = "Black" StrokeThickness = "10" Margin = "10">
<Ellipse.Fill>
<RadialGradientBrush
GradientOrigin = "0.2,0.2"
Center = "0.4,0.4">
<GradientStop Color = "Aqua" Offset = "0.006"/>
<GradientStop Color = "AntiqueWhite" Offset = "1"/>
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
</Grid>
</UserControl>
以下に示すのは、 MainPage.xaml ファイルのコードで、 Print ボタンのみが含まれています。
<UserControl x:Class = "PrintCustomUI.MainPage"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d = "http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable = "d"
d:DesignHeight = "300" d:DesignWidth = "400">
<Grid x:Name = "LayoutRoot" Background = "White">
<Button Content = "Print..." Height = "23" HorizontalAlignment = "Left"
Margin = "12,28,0,0" Name = "printButton"
VerticalAlignment = "Top" Width = "75"
Click = "printButton_Click"/>
</Grid>
</UserControl>
印刷ボタンの* Clickイベント*実装を次に示します。
using System;
using System.Collections.Generic;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Printing;
namespace PrintCustomUI {
public partial class MainPage : UserControl {
public MainPage() {
InitializeComponent();
}
private void printButton_Click(object sender, RoutedEventArgs e) {
PrintDocument pd = new PrintDocument();
pd.PrintPage += new EventHandler<PrintPageEventArgs>(pd_PrintPage);
pd.Print("Custom");
}
void pd_PrintPage(object sender, PrintPageEventArgs e) {
var pl = new PrintLayout();
pl.Width = e.PrintableArea.Width;
pl.Height = e.PrintableArea.Height;
e.PageVisual = pl;
}
}
}
上記のコードをコンパイルして実行すると、Webページに次の出力が表示されます。
印刷*をクリックし、 *OneNote を選択してレイアウトを印刷します。 レイアウトが印刷されていることがわかります。
使用可能なスペースがいっぱいになっていることがわかります。 理解を深めるために、上記の例を実行することをお勧めします。