Dab*_*rnl 5 printing wpf .net-4.5
说我有以下小UserControl:
<StackPanel>
<TextBlock Text="{Binding Title, StringFormat=Name: {0}}" FontWeight="Bold"/>
<Separator/>
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=. ,StringFormat=Detail: {0}}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Line Stroke="Black" HorizontalAlignment="Stretch" Margin="0,10"
X2="{Binding ActualWidth, RelativeSource={RelativeSource Self}}"
StrokeDashArray="2 2" StrokeThickness="1" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
我的应用程序将生成这些控件的集合,然后应该使用自动分页打印它们.ItemsControl变量中显示的项目数.我抨击网络,阅读关于在Pro WPF 4.5 Unleashed中打印的章节,但我仍然看不到如何实现这一点.
任何指导都非常欢迎.
编辑:欢迎任何指导.由于我在视图模型中可以获得数据,因此将数据重定向到其他地方并不困难.但是哪里?
由于我们没有您的代码并且我们不知道您的数据,因此很难理解您到底在哪里,但是这里有一个简单的示例:
<Window x:Class="WpfApplication1.PrintVisualDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Print Visual Demo" Height="350" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.9*" />
<RowDefinition Height="0.2*"/>
</Grid.RowDefinitions>
<StackPanel Name="printPanel"
Grid.Row="0"
Margin="5">
<Label Content="Sweet Baby"
HorizontalAlignment="Center"
FontSize="40"
FontFamily="Calibri">
<Label.Foreground>
<LinearGradientBrush Opacity="1"
StartPoint="0,0.5" EndPoint="1,0.5">
<LinearGradientBrush.GradientStops>
<GradientStop Color="Blue" Offset="0" />
<GradientStop Color="Red" Offset="0.5" />
<GradientStop Color="Green" Offset="1" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Label.Foreground>
</Label>
<Image Source="D:\Temp\baby.jpg"
Height="150"
Width="200">
</Image>
</StackPanel>
<Button Content="Print"
Grid.Row="1" Margin="5"
Height="40" Width="150"
HorizontalAlignment="Center"
VerticalAlignment="Center" Click="Button_Click" />
</Grid>
</Window>
private void Button_Click(object sender, RoutedEventArgs e)
{
PrintDialog pd = new PrintDialog();
if (pd.ShowDialog() != true) return;
pd.PrintVisual(printPanel, "Printing StackPanel...");
}
Run Code Online (Sandbox Code Playgroud)
PrintVisual 方法应该可以完成这项工作。
如果您想缩放视觉效果以适合页面,您可以按照以下步骤操作:
PrintDialog printDlg = new System.Windows.Controls.PrintDialog();
if (printDlg.ShowDialog() == true)
{
//get selected printer capabilities
System.Printing.PrintCapabilities capabilities = printDlg.PrintQueue.GetPrintCapabilities(printDlg.PrintTicket);
//get scale of the print wrt to screen of WPF visual
double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / this.ActualWidth, capabilities.PageImageableArea.ExtentHeight /
this.ActualHeight);
//Transform the Visual to scale
this.LayoutTransform = new ScaleTransform(scale, scale);
//get the size of the printer page
Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
//update the layout of the visual to the printer page size.
this.Measure(sz);
this.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));
//now print the visual to printer to fit on the one page.
printDlg.PrintVisual(this, "First Fit to Page WPF Print");
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,您需要做的就是重新测量并使用新尺寸重新排列特定元素,然后您可以调用 PrintVisual 方法。
我希望这对您有进一步的帮助。考虑发布更多代码或将您的项目上传到网上某处。