WPF 簡單列印
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="259" Width="282">
<Grid Margin="5" Name="grid">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Canvas Name="canvas">
<TextBlock Canvas.Top="50" Canvas.Left="20" FontSize="25" FontWeight="Bold">Hello there</TextBlock>
<Path Fill="Yellow" Stroke="Blue" Margin="5" Canvas.Left="10">
<Path.Data>
<GeometryGroup>
<RectangleGeometry Rect="0 0 100 100"></RectangleGeometry>
<EllipseGeometry Center="50 50" RadiusX="35" RadiusY="25"></EllipseGeometry>
</GeometryGroup>
</Path.Data>
</Path>
</Canvas>
<Button Name="btnClick" Grid.Row="1" Click="btnClick_Click_1">Click</Button>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Printing;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace _096基本列印
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnClick_Click_1(object sender, RoutedEventArgs e)
{
PrintDialog dialog = new PrintDialog();
var printers = new LocalPrintServer().GetPrintQueues();
//選擇一個印表機
var selectedPrinter = printers.FirstOrDefault(p => p.Name == "EPSON LQ-300K+ /II ESC/P 2");
if (selectedPrinter == null)
{
MessageBox.Show("沒有找到EPSON LQ-300K+ /II ESC/P 2印表機");
return;
}
//設定印表機
dialog.PrintQueue = selectedPrinter;
dialog.PrintVisual(canvas, "A Simple Drawing");
//if (dialog.ShowDialog()==true)
//{
// grid.Visibility = System.Windows.Visibility.Hidden;
// canvas.LayoutTransform = new ScaleTransform(5, 5);
// int pageMagin = 5;
// Size pageSize = new Size(dialog.PrintableAreaWidth - pageMagin * 2, dialog.PrintableAreaHeight - 20);
// canvas.Measure(pageSize);
// canvas.Arrange(new Rect(pageMagin,pageMagin,pageSize.Width,pageSize.Height));
// dialog.PrintVisual(canvas, "A Simple Drawing");
// canvas.LayoutTransform = null;
// grid.Visibility = System.Windows.Visibility.Visible;
//}
}
}
}