65.9K
CodeProject 正在变化。 阅读更多。
Home

如何在 Silverlight 中将多个页面压缩到一页打印

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.83/5 (3投票s)

2011 年 9 月 15 日

CPOL

1分钟阅读

viewsIcon

18214

本文重点介绍如何在 Silverlight 中将多页内容压缩到一页并进行打印。

引言

在本文中,我将介绍一种将多页内容压缩到一页并在 Silverlight 中打印该页面的方法。

背景

Silverlight 4 提供了一个打印功能,可以更轻松地在企业项目中应用。这意味着 Silverlight 不仅仅用于演示和创建小型组件以供上传文件。

打印功能易于使用,但存在一些不便之处,例如,它无法告知我们材料有多少页,因此我们需要自行计算。因此,一些用户创建了在 Silverlight 中打印多页的方法。

但我想讨论另一种情况,即我希望将我的材料在一页中打印出来,即使它有多个页面。因此,我需要压缩宽度或高度进行打印。

举个例子。下图是一个简单的 Silverlight 程序。主页上有一个列表,显示 100 列数据。显然,打印时会超出单页。

2.gif

使用代码后,100 列数据被打印在一页中(见下图)。

1.gif

放大后不是很清晰。因此,如果您的内容不适合压缩,结果将如上所示。

使用代码

页面定义

<UserControl
    xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
    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"
    xmlns:System="clr-namespace:System;assembly=mscorlib"
    x:Class="SilverlightApplication2.MainPage"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400"
    xmlns:c="clr-namespace:SilverlightApplication2">

   <Grid x:Name="LayoutRoot" Background="White">

   <ScrollViewer>
        <StackPanel>
            <ListBox x:Name="lstData" Padding="50" 
              BorderBrush="Transparent" ItemsSource="{Binding}">
            <!--Define this converter to enable compress 
                      and display based on 100% by default-->
            <ListBox.RenderTransform>
            <ScaleTransform x:Name="st" ScaleX="1" 
              ScaleY="1"></ScaleTransform>
            </ListBox.RenderTransform>

           <ListBox>
            <Button Content="Print" x:Name="btPrint" 
              Width="100" Click="btPrint_Click" 
              HorizontalAlignment="Left"></Button>
        <StackPanel>
   </ScrollViewer>
</Grid>
</UserControl>

后台代码

using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Printing;

namespace SilverlightApplication2
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            //Prepare 100 Column Testing Data
            this.DataContext = Enumerable.Range(1, 100).Select(i => new Data()
            {
                ID = i,
                Name = "Steve Paul Jobs"
            });
        }
        private void btPrint_Click(object sender, RoutedEventArgs e)
        {
            //Actual Height of List
            var ah = lstData.ActualHeight;
            //Actual Width of List
            var aw = lstData.ActualWidth;
            //Create a New Print Document
            var doc = new PrintDocument();
            //Do Print Event
            doc.PrintPage += (o, a) =>
            {
                //Select Printer and Provide Print Height
                var h = a.PrintableArea.Height;
                //Select Printer and Provide Print Width
                var w = a.PrintableArea.Width;
                //Compress Width if not Enough
                if(aw > w) st.ScaleX = w / aw;
                //Compress Height if not Enough
                if(ah > h) st.ScaleY = h / ah;
                //Set Print Content
                a.PageVisual = lstData;
            };
            //Do Event after Printing
            doc.EndPrint += (o, a) =>
            {
                //Compress List to 100% again
                st.ScaleX = 1;
                st.ScaleY = 1;
            };
            //Print 
            doc.Print(null);
        }
    }
    class Data
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public override string ToString()
        {
            return string.Format("ID={0:000},Name={1}", ID, Name);
        }
    }
}

结论

这个示例的关键在于 Silverlight 和 WPF 在呈现时支持通过不同的转换器进行变换。ScaleTransform 是最简单的一种,可以基于缩放进行压缩。此外,Silverlight 中还有其他几种转换器。

加法

此外,我发现了一个博客,展示了许多 Silverlight 技巧,我想推荐给您:http://janewdaisy.wordpress.com/category/silverligh/

© . All rights reserved.