WPF 的打印对话框和打印预览对话框 [英] Print dialog and print prewiew dialog for WPF
问题描述
是否有 WPF 的打印对话框与 WPF 中的打印预览对话框相结合,如 Google Chrome 或 Word 那样?
此时我使用 Windows 窗体中的打印预览对话框.我也尝试使用它的 WPF 版本.但是 WPF 没有 PrintPreviewDialog
或 PrintPrewiewControl
.这是我的代码:
//到我的类文件的顶部:使用 Forms = System.Windows.Forms;//在同一个类的方法中:PageSettings setting = new PageSettings();setting.Landscape = true;_document = new PrintDocument();_document.PrintPage += _document_PrintPage;_document.DefaultPageSettings = 设置;Forms.PrintPreviewDialog printDlg = new Forms.PrintPreviewDialog();printDlg.Document = _document;打印Dlg.Height = 500;打印Dlg.Width = 200;尝试{if (printDlg.ShowDialog() == Forms.DialogResult.OK){_document.Print();}}捕获 (InvalidPrinterException){MessageBox.Show("未找到打印机.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);}
我也搜索过 NuGet 包,但没有找到真正好的.
你想要做的是从你想要打印的内容中创建一个 xpsDocument
(一个 flowDocument
) 并使用该 XpsDocument
来预览内容,例如假设您有以下 Xaml,以及您想要的 flowDocument
打印其内容:
<网格><Grid.RowDefinitions><RowDefinition Height="*"/><RowDefinition Height="自动"/></Grid.RowDefinitions><FlowDocumentScrollViewer><FlowDocument x:Name="FD"><段落><Image Source="http://www.wpf-tutorial.com/images/logo.png" Width="90" Height="90" Margin="0,0,30,0"/><Run FontSize="120">WPF</Run></段落><段落>WPF,代表<Bold>Windows Presentation Foundation</Bold>,是 Microsoft 最新的 GUI 框架方法,与 .NET 框架一起使用.一些优点包括:</段落><列表><列表项><段落>它更新,因此更符合当前标准</段落></ListItem><列表项><段落>微软正在将它用于许多新应用程序,例如视觉工作室</段落></ListItem><列表项><段落>它更灵活,因此您无需编写或购买新控件即可做更多事情</段落></ListItem></列表></FlowDocument></FlowDocumentScrollViewer><Button Content="Print" Grid.Row="1" Click="Button_Click"></Button></网格>
flowDocument 示例来自
Is there a print dialog for WPF that is combinated whit a print preview dialog in WPF like Google Chrome or Word does?
At this moment I use a the print preview dialog from Windows forms. I have also try for to use the WPF version of it. But WPF has no PrintPreviewDialog
or PrintPrewiewControl
. This is my code:
//To the top of my class file:
using Forms = System.Windows.Forms;
//in a methode on the same class:
PageSettings setting = new PageSettings();
setting.Landscape = true;
_document = new PrintDocument();
_document.PrintPage += _document_PrintPage;
_document.DefaultPageSettings = setting ;
Forms.PrintPreviewDialog printDlg = new Forms.PrintPreviewDialog();
printDlg.Document = _document;
printDlg.Height = 500;
printDlg.Width = 200;
try
{
if (printDlg.ShowDialog() == Forms.DialogResult.OK)
{
_document.Print();
}
}
catch (InvalidPrinterException)
{
MessageBox.Show("No printers found.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
I've also searched for a NuGet package but nothing found really good.
What you want to do, is to create an xpsDocument
out from the content you want to print (a flowDocument
) and use that XpsDocument
to preview the content, for example let say you have the following Xaml, with a flowDocument
that you want to print its content :
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<FlowDocumentScrollViewer>
<FlowDocument x:Name="FD">
<Paragraph>
<Image Source="http://www.wpf-tutorial.com/images/logo.png" Width="90" Height="90" Margin="0,0,30,0" />
<Run FontSize="120">WPF</Run>
</Paragraph>
<Paragraph>
WPF, which stands for
<Bold>Windows Presentation Foundation</Bold> ,
is Microsoft's latest approach to a GUI framework, used with the .NET framework.
Some advantages include:
</Paragraph>
<List>
<ListItem>
<Paragraph>
It's newer and thereby more in tune with current standards
</Paragraph>
</ListItem>
<ListItem>
<Paragraph>
Microsoft is using it for a lot of new applications, e.g. Visual Studio
</Paragraph>
</ListItem>
<ListItem>
<Paragraph>
It's more flexible, so you can do more things without having to write or buy new controls
</Paragraph>
</ListItem>
</List>
</FlowDocument>
</FlowDocumentScrollViewer>
<Button Content="Print" Grid.Row="1" Click="Button_Click"></Button>
</Grid>
the flowDocument Sample is from Wpf tutorials site
the print button Click event handler should looks like this :
private void Button_Click(object sender, RoutedEventArgs e)
{
if (File.Exists("printPreview.xps"))
{
File.Delete("printPreview.xps");
}
var xpsDocument = new XpsDocument("printPreview.xps", FileAccess.ReadWrite);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
writer.Write(((IDocumentPaginatorSource)FD).DocumentPaginator);
Document = xpsDocument.GetFixedDocumentSequence();
xpsDocument.Close();
var windows = new PrintWindow(Document);
windows.ShowDialog();
}
public FixedDocumentSequence Document { get; set; }
so here you are mainly :
- Creating an Xps document and storing it in printPreview.xps file,
- Writing the
FlowDocument
content into that file, - passing the
XpsDocument
to thePrintWindow
in which you will handle the preview and the print actions,
here how the PrintWindow
looks like :
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="1.5*"/>
</Grid.ColumnDefinitions>
<StackPanel>
<Button Content="Print" Click="Button_Click"></Button>
<!--Other print operations-->
</StackPanel>
<DocumentViewer Grid.Column="1" x:Name="PreviewD">
</DocumentViewer>
</Grid>
and the code behind :
public partial class PrintWindow : Window
{
private FixedDocumentSequence _document;
public PrintWindow(FixedDocumentSequence document)
{
_document = document;
InitializeComponent();
PreviewD.Document =document;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//print directly from the Xps file
}
}
the final result looks something like this
Ps: to use XpsDocument you should add a reference to System.Windows.Xps.Packaging namespace
这篇关于WPF 的打印对话框和打印预览对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!