WPF打印,FlowDocument
工作中使用到了打印的功能,现在主要是记录一下打印功能的主要代码。
代码展示
1.调用打印代码
//调用PrintDialog 打印控件
PrintDialog printDialog = new PrintDialog();
//打印模板界面
PrintViewContent printViewContent = new PrintViewContent();
//将内容填入模板
printViewContent.RenderQaRecode(RTPlanDisplaySelected, Items);
//显示打印界面,选择打印机
if(printDialog.ShowDialog() == true)
{
//打印
printDialog.PrintDocument(printViewContent.GetDoc(), "");
}
2.模板界面(.cs)
public void RenderQaRecode(object date,List<item> items)
{
if(date is parent p)
{
//FlowDocument添加行
Paragraph paragraphPatient = new Paragraph(new Run(p.Name+ " " + p.Age));
paragraphPatient.FontSize = 35;
doc.Blocks.Add(paragraphPatient);
//声明表,行,单元格
Table t = new Table();
TableRowGroup groupDetails = new TableRowGroup();
Style styleCell = doc.Resources["BorderedCell"] as Style;
TableRow row = new TableRow();
#region 添加表头
//添加表头
TableCell cell = new TableCell(new Paragraph(new Run("项目")));
cell.Style = styleCell;
row.Cells.Add(cell);
cell = new TableCell(new Paragraph(new Run("目标值")));
cell.Style = styleCell;
row.Cells.Add(cell);
cell = new TableCell(new Paragraph(new Run("实际值")));
cell.Style = styleCell;
row.Cells.Add(cell);
cell = new TableCell(new Paragraph(new Run("结果")));
cell.Style = styleCell;
row.Cells.Add(cell);
if (items== null)
{
items= new List<item>();
}
groupDetails.Rows.Add(row);
#endregion
#region 添加表内容
foreach (var i in items)
{
TableRow rowCon = new TableRow();
cell = new TableCell(new Paragraph(new Run(i.Type)));
cell.Style = styleCell;
rowCon.Cells.Add(cell);
cell = new TableCell(new Paragraph(new Run(i.ScheduledValue.ToString())));
cell.Style = styleCell;
rowCon.Cells.Add(cell);
cell = new TableCell(new Paragraph(new Run(i.Value.ToString())));
cell.Style = styleCell;
rowCon.Cells.Add(cell);
string str = string.Empty;
if((System.Math.Abs((float)(i.ScheduledValue- i.Value)))>i.ValueLit)
{
str = "测试未通过";
}
else
{
str = "测试通过";
}
cell = new TableCell(new Paragraph(new Run(str)));
cell.Style = styleCell;
rowCon.Cells.Add(cell);
groupDetails.Rows.Add(rowCon);
}
#endregion
//将行添到表中
t.RowGroups.Add(groupDetails);
//FlowDocument 添加表
doc.Blocks.Add(t);
}
}
//获取FlowDocument的多页元素的抽象基类。我觉得可以理解为将FlowDocument分页。
public DocumentPaginator GetDoc()
{
return ((IDocumentPaginatorSource)doc).DocumentPaginator;
}
3. 模板界面(.XAML)
<Window x:Class="IORTAPP.View.PrintView.PrintViewContent"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Title="PrintView" Height="1123" Width="800">
<FlowDocument x:Name="doc" ColumnWidth="1123" FontFamily="微软雅黑 Semibold">
<FlowDocument.Resources>
<!--#region 提供样式-->
<Style TargetType="Table" x:Key="BorderedTable">
<Setter Property="CellSpacing" Value="0"></Setter>
<Setter Property="BorderThickness" Value="1"></Setter>
<Setter Property="BorderBrush" Value="#000"></Setter>
</Style>
<Style TargetType="TableCell" x:Key="BorderedCell">
<Setter Property="BorderThickness" Value="0.5"></Setter>
<Setter Property="BorderBrush" Value="#000"></Setter>
<Setter Property="Padding" Value="3"></Setter>
<Setter Property="FontSize" Value="25" />
</Style>
<!--#endregion-->
</FlowDocument.Resources>
</FlowDocument>
</Window>
关键内容简单介绍
1.PrintDialog类
在WPF 中可以通过PrintDialog 类方便的实现应用程序打印功能。
PrintDialog可以直接打印页面,也可以打印FlowDocument的内容,选择打印机以后就直接打印。
2.FlowDocument类
FlowDocument很像是WPF中的画布概念。可以布置多个控件。可以直接在XAML文件中写死,也可以在CS文件中动态添加。
FlowDocument类中包含了部分属性和控件。在代码也也有了相应的使用,这里我就不一一介绍了,但是在开发的过程中我发现了一个网上有一位写的关于FlowDocument类的文章介绍的很详细,建议大家直接看这个。链接如下
https://blog.csdn.net/qq_42068856/article/details/122050999
结束语
以上内容就是工作中的一个简单代码记录,初次解除WPF的打印,做的可能不好,希望大家能给提出意见,
此外WPF打印还可以直接打印界面,下一章会介绍