[WPFでの印刷(2) (C#) ]をダウンロードする。
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="401" Width="522">
<Grid>
<Grid Name="Grid1" Margin="0,25,0,0">
<Ellipse Height="50" HorizontalAlignment="Left" Margin="65,91,0,0" Name="Ellipse1" Stroke="Black" VerticalAlignment="Top" Width="93" Fill="BlueViolet" />
<Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="225,167,0,0" Name="Label1" VerticalAlignment="Top" />
<Rectangle Height="56" HorizontalAlignment="Left" Margin="264,85,0,0" Name="Rectangle1" Stroke="Black" VerticalAlignment="Top" Width="101" Fill="Magenta" />
</Grid>
<StackPanel Height="25" HorizontalAlignment="Left" Name="StackPanel1" VerticalAlignment="Top">
<Button Content="印刷" Height="23" Name="Button1" Width="75" />
</StackPanel>
</Grid>
</Window>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Shapes;
using System.Windows.Xps;
using System.Printing;
using System.Windows.Markup;
namespace WPF_Printing2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
FixedPage page = new FixedPage();
PageContent content1 = new PageContent();
FixedDocument doc =new FixedDocument();
page.Width = 120 * 96 / 25.4;
page.Height = 70 * 96 / 25.4;
Ellipse Ellipse2 = new Ellipse();
CopyEllipse(Ellipse1, Ellipse2);
page.Children.Add(Ellipse2);
Rectangle Rectangle2 = new Rectangle();
page.Children.Add(Rectangle2);
CopyRectangle(Rectangle1, Rectangle2);
Label Label2 = new Label();
CopyLabel(Label1, Label2);
page.Children.Add(Label2);
((IAddChild)content1).AddChild(page);
doc.Pages.Add(content1);
PrintDocumentImageableArea imgArea=null;
XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(ref imgArea);
if (writer != null)
{
writer.Write(doc);
}
}
void CopyEllipse(Ellipse Ellipse1 , Ellipse Ellipse2)
{
Ellipse2.Height = Ellipse1.Height;
Ellipse2.HorizontalAlignment = Ellipse1.HorizontalAlignment;
Ellipse2.Margin = Ellipse1.Margin;
Ellipse2.Stroke = Ellipse1.Stroke;
Ellipse2.VerticalAlignment = Ellipse1.VerticalAlignment;
Ellipse2.Width = Ellipse1.Width;
Ellipse2.Fill = Ellipse1.Fill;
}
void CopyRectangle(Rectangle Rectangle1, Rectangle Rectangle2)
{
Rectangle2.Height = Rectangle1.Height;
Rectangle2.HorizontalAlignment = Rectangle1.HorizontalAlignment;
Rectangle2.Margin = Rectangle1.Margin;
Rectangle2.Stroke = Rectangle1.Stroke;
Rectangle2.VerticalAlignment = Rectangle1.VerticalAlignment;
Rectangle2.Width = Rectangle1.Width;
Rectangle2.Fill = Rectangle1.Fill;
}
void CopyLabel(Label Label, Label Label2)
{
Label2.Content = Label1.Content;
Label2.Height = Label1.Height;
Label2.HorizontalAlignment = Label1.HorizontalAlignment;
Label2.Margin = Label1.Margin;
Label2.VerticalAlignment = Label1.VerticalAlignment;
}
}
}