天天看點

關于Sliverlight4 列印功能

sliverlight4  列印功能基于 PrintDocument 類

我做了個DEMO 關于列印頁面的實作

前台界面:

<UserControl x:Class="PrintPage.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="262*"/>
            <RowDefinition Height="38*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <ScrollViewer Margin="4,4.135,4,4" HorizontalScrollBarVisibility="Auto"
            VerticalScrollBarVisibility="Auto">
            <Image x:Name="imgOne" Source="image/09r.jpg"
                   Height="500" Width="500"/>
        </ScrollViewer>
        <Button x:Name="Print" Content="Print" Margin="4,5,4,0"
                Click="Print_Click" Grid.Row="1" />
    </Grid>
</UserControl>      

背景代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Printing;

namespace PrintPage
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void Print_Click(object sender, RoutedEventArgs e)
        {
            var pdoc = new PrintDocument();
            pdoc.PrintPage += (p, args) =>
            {
                args.PageVisual = imgOne;
                args.HasMorePages = false;

            };

            pdoc.EndPrint += (p, args) =>
            {
                MessageBox.Show("列印完畢");
            };

            pdoc.Print("列印改頁面");    
        }
    }
}      

  

需要注意的是 頁面列印時會觸發printPage 事件,其中利用PrintPageEventArgs 會去設定頁面中需要列印的區域(控件),是否需要列印多頁等等

關于PrintPageEventArgs 的屬性大家可以參考msdn。

同時可以定制列印完事件,觸發列印完畢後一系列的邏輯

有朋友問我sliverlight4 最大列印的範圍(長度)是多少。

苦苦尋找之後發洩一些線索:

1:Yes, its not documented though, Silverlight doesn't print for paper  sizes A3 and larger. This is due to bitmap printing which would result  in too large print jobs.

On contacting support, they said its a known issue for them with no  known schedule about when it will be resolved. Looks like it won't be so before Silverlight 5.

大緻意思是sliverlight4不能列印A3或者更大的頁面,這個BUG可能在SL5中被修正

2:

a0 paper size in pixel on  96dip is

Width 3174

Height 4492

when i select Print paper size in printer settings it didn't generate print

大緻意思是

a0紙  的長度是3174,寬度是4492,選擇A0紙的時候sliverlight就無法列印了

繼續閱讀