wpf 使用DocumentViewer打印

  1. 声明接口  ,用于实现IDocumentRenderer接口,作用后台代码根据数据行渲染出相应的的行
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;

namespace VirtalWeldingView.PrintDocumentManage
{
    /// <summary>
    /// 通过后台代码根据数据行数渲染出相应的行
    /// </summary>
    public interface IDocumentRenderer
    {
        void Render<T>(FlowDocument doc, IList<T> list);
    }
}
    2.<span style="text-align:left">实现IDocumentRendere接口,通过TableRowGroup对象接受数据库数据集合填充TableRow和TableCell,并创建打印table格式,该格式模板为流文档xaml文件类型。</span>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows;
using DataEntity;

namespace VirtalWeldingView.PrintDocumentManage
{
    public class ClassScoreDocumentRenderer : IDocumentRenderer
    {
        public void Render<T>(System.Windows.Documents.FlowDocument doc, IList<T> list)
        {
            TableRowGroup group = doc.FindName("rowsDetails") as TableRowGroup;
            Style styleCell = doc.Resources["TableCell"] as Style;
            T entity = default(T);
            entity = Activator.CreateInstance<T>();

            foreach (var model in list)
            {
                if ((model as Student) != null)
                {
                    Student item = model as Student;
                    TableRow row = new TableRow();

                    TableCell cell = new TableCell(new Paragraph(new Run(item.Student_No)));
                    cell.Style = styleCell;
                    row.Cells.Add(cell);

                    cell = new TableCell(new Paragraph(new Run(item.Student_Name)));
                    cell.Style = styleCell;
                    row.Cells.Add(cell);

                    cell = new TableCell(new Paragraph(new Run(Convert.ToString(item.SexName))));
                    cell.Style = styleCell;
                    row.Cells.Add(cell);

                    cell = new TableCell(new Paragraph(new Run(item.TestPaperName)));
                    cell.Style = styleCell;
                    row.Cells.Add(cell);

                    cell = new TableCell(new Paragraph(new Run(Convert.ToString(item.Score))));
                    cell.Style = styleCell;
                    row.Cells.Add(cell);
                    group.Rows.Add(row);
                }
            }
        }
    }
}
自定义流文档:
<pre name="code" class="csharp"><FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              ColumnWidth="400" FontSize="14" FontFam
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值