zoukankan      html  css  js  c++  java
  • Silverlight中的打印

    Silverlight中的打印只有一个类,那就是PrintDocment这个对象来实现。

    当然实现方式也很简单,先看下实现的步骤:

    其实只需两个步骤即可完成,即绑定PrintDocument的PrintPage事件和调用Print方法。

    PrintDocument document = new PrintDocument();
    document.PrintPage += documentImage_PrintPage;
    document.Print("Image Document");


    这个就完成了一个打印,其中PrintPage事件是最为重要的,因为整个打印的工作都是在这个事件中完成的,另外该事件的参数PrintPageEventArgs构成了整个打印过程中的属性的设置;Print方法需要传递一个参数,参数为打印的文件的名称,在调用该方法的时候开始触发一系列的打印事件。

    看下PrintPageEventArgs类型的属性:

    PrintableArea:获取或设置一个Size类型的值,表示打印的范围,分别表示Height和Width,如果打印的部分超出了区域,则被截取

    PageMargins:设置打印页的Margin值

    PageVisual:设置要打印的对象,可以是一个TextBlock、Image,也可以是一个复杂的元素(Grid或者Canvas)

    HasMorePages:一个bool值,标识是否多页

    看一个简单的例子:

       private void btnPrintImage_Click(object sender, RoutedEventArgs e)
    {
    PrintDocument document = new PrintDocument();
    document.PrintPage += new EventHandler<PrintPageEventArgs>(document_PrintPage);
    document.Print("Print Image");
    }

    void document_PrintPage(object sender, PrintPageEventArgs e)
    {
    Image imagePrint = new Image();
    imagePrint.Source = img.Source;
    imagePrint.Height = e.PrintableArea.Height;
    imagePrint.Width = e.PrintableArea.Width;
    e.PageVisual = imagePrint;
    e.HasMorePages = false;
    }


     

    在打印的过程中经常会需要分页打印,没关系,看个例子:

     //当前打印的行的索引,用于遍历ListBox.Items
    private int listPrintIndex;
    private void btnPrintList_Click(object sender, RoutedEventArgs e)
    {
    //初始值为0
    listPrintIndex = 0;
    PrintDocument document = new PrintDocument();
    document.PrintPage += new EventHandler<PrintPageEventArgs>(document_PrintPage);
    document.Print("Print List");
    }

    //设置每一项之间的间距
    private int extraMargin = 50;
    void document_PrintPage(object sender, PrintPageEventArgs e)
    {
    //定义一个打印的元素
    Canvas printSurface = new Canvas();
    e.PageVisual = printSurface;
    //得到最顶端位置
    double topPosition = e.PageMargins.Top + extraMargin;

    //遍历当前的ListBox.Items
    while (listPrintIndex<lstPrint.Items.Count)
    {
    //实例化TextBlock用来存放ListItem的值
    TextBlock txt = new TextBlock();
    txt.FontSize = 30;
    //得到ListBox每一项的值
    txt.Text = lstPrint.Items[listPrintIndex].ToString();
    double measuredHeight = txt.ActualHeight;
    //如果打印的当前行高度不合适的话,则进行分页
    if (measuredHeight>(e.PrintableArea.Height- topPosition- extraMargin))
    {
    e.HasMorePages = true;
    return ;
    }

    //设置TextBlock在Canvas中的位置
    txt.SetValue(Canvas.TopProperty, topPosition);
    txt.SetValue(Canvas.LeftProperty, e.PageMargins.Left + extraMargin);
    //将TextBlock添加到打印的元素中去
    printSurface.Children.Add(txt);

    listPrintIndex++;
    //追加高度
    topPosition = topPosition + measuredHeight;
    }
    e.HasMorePages = false;
    }

    效果如下

    有时候我们需要一个功能,那就是打印预览,这个打印预览的原理就是将要打印的元素创建成一个WriteableBitmap来显示。

  • 相关阅读:
    hql语句
    eclipse部署jrebel热启动后报错java.lang.OutOfMemoryError: PermGen space问题
    jQuery冒泡事件
    oracle中使用序列生成编号
    月份-日期下拉框联动变换日期
    xml运用1
    Oracle 数据库 ORA-01034
    Spring中IOC&AOP
    使用可视化图表对 Webpack 2 的编译与打包进行统计分析
    数据驱动
  • 原文地址:https://www.cnblogs.com/ListenFly/p/2192419.html
Copyright © 2011-2022 走看看