Click here to Skip to main content
13,344,508 members (47,639 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 15 Sep 2011

How to Compress Several Pages into One Page to Print in Silverlight

, 15 Sep 2011
Rate this:
Please Sign up or sign in to vote.
This article focuses on how to compress several pages' contents in one page and print in Silverlight.


In this article, I will introduce a method to compress several pages' contents into one page and print that page in Silverlight.


Silverlight 4 provides a print function which can be used in enterprise projects more easily. And this means that Silverilght is not just used for demonstrations and create small components for uploading files.

The print function is easy to use but there are some inconveniences, for example, we must calculate how many pages the materials have because it cannot tell us. Therefore, some users create methods to print multiple pages in Silverlight.

But I want to talk about another situation where I want to print my material in one page although it has several pages. So I need to compress the width or height to print.

Let's take an example. The following image is a simple Silverlight program. There is a list on homepage which displays a 100 columns data. It is obvious that it will be over one page if printing.


After using the code, the 100 columns data is printed in one page (see following image).


It is not very clear after being enlarged. Therefore, if your contents are not suitable for compressing, the result will be as above.

Using the code

Page definition













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

            <ListBox x:Name="lstData" Padding="50" 

              BorderBrush="Transparent" ItemsSource="{Binding}">
            <!--Define this converter to enable compress 
                      and display based on 100% by default-->
            <ScaleTransform x:Name="st" ScaleX="1" 


            <Button Content="Print" x:Name="btPrint" 

              Width="100" Click="btPrint_Click" 


Background code

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

namespace SilverlightApplication2
    public partial class MainPage : UserControl
        public MainPage()
            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;
    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);


The key to this example is that Silverlight and WPF support transformation through different translators when presenting. ScaleTransform is the easiest one which can compress based on scale. Also, there are several other translators in Silverlight.


In addition, I found a blog which shows many Silverlight skills and I want to recommend this to you:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

New Zealand New Zealand
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionNice Article Pin
loveyou99915-Sep-11 16:19
memberloveyou99915-Sep-11 16:19 
AnswerRe: Nice Article Pin
SummiG15-Sep-11 17:48
memberSummiG15-Sep-11 17:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 15 Sep 2011
Article Copyright 2011 by SummiG
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid