gpt4 book ai didi

c# - WPF 的打印对话框和打印预览对话框

转载 作者:可可西里 更新时间:2023-11-01 09:03:00 34 4
gpt4 key购买 nike

是否有 WPF 的打印对话框与 WPF 中的打印预览对话框相结合,就像 Google Chrome 或 Word 那样?

Like Google chrome does.

此时我使用 Windows 窗体的打印预览对话框。我也尝试使用它的 WPF 版本。但是 WPF 没有 PrintPreviewDialogPrintPrewiewControl。这是我的代码:

//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);
}

我也搜索过 NuGet 包,但没有找到真正好的。

最佳答案

您想要做的是从您要打印的内容(flowDocument)中创建一个 xpsDocument 并使用该 XpsDocument 预览内容,例如假设您有以下 Xaml,其中有一个要打印其内容的 flowDocument:

 <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>

flowDocument 示例来自 Wpf tutorials site

打印按钮的 Click 事件处理程序应如下所示:

 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; }

所以你在这里主要是:

  • 创建 Xps 文档并将其存储在 printPreview.xps 文件中,
  • FlowDocument 内容写入该文件,
  • XpsDocument 传递给 PrintWindow,您将在其中处理预览和打印操作,

此处 PrintWindow 的样子:

<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>

和背后的代码:

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
}
}

最终结果是这样的

enter image description here

Ps:要使用 XpsDocument,您应该添加对 System.Windows.Xps.Packaging namespace 的引用

关于c# - WPF 的打印对话框和打印预览对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29892177/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com