Back to table of contents

Step by Step: Printing diagrams

The template of an application in which the end user can print his diagrams.

Step 1

Create new project in Microsoft Visual Studio environment. Select item New\Project from the main menu.



Select Silverlight Application, set name of the project – “Printing”, set directory to save the project to.



While creation of the project uncheck option “Host the Silverlight application in a new Web site”.



After that Visual Studio will create a new solution containing one project.

Step 2

Add and set up SharpShooter Diagrams component. It’s necessary to add SharpShooter Diagrams component to the Silverlight application to add the diagrams designer. In order to do that, you should add references to the following assemblies:

Aphalina.Core.dll

Aphalina.Diagrams.Components.dll

Aphalina.Diagrams.DesignTime.dll

Aphalina.Diagrams.DesignTime.UI.dll

Aphalina.Diagrams.dll

Aphalina.Diagrams.FlowCharts.DesignTime.dll

Aphalina.Diagrams.FlowCharts.dll

Aphalina.Diagrams.UI.Controls.dll

Aphalina.Diagrams.UI.Models.dll

Aphalina.Diagrams.UI.Ribbon.dll

Aphalina.Drawing.Connections.DesignTime.dll

Aphalina.Drawing.Connections.dll

Aphalina.Drawing.Core.dll

Aphalina.Drawing.dll

Aphalina.Drawing.UI.Controls.dll

Aphalina.Drawing.UI.Models.dll

Aphalina.Drawing.UI.Ribbon.dll

Aphalina.Framework.dll

In order to do it, right-click References of the OnSelectionChanged in the "Solution Explorer" and select “Add Reference” item in the popup menu.




You should also add references to the following assemblies:

System.Windows.Controls.dll

System.Windows.Controls.Input.dll

System.Windows.Controls.Layout.Toolkit.dll

System.Windows.Controls.Toolkit.dll



Your References should look like this:



Step 3

In order to view and change already created diagrams we need to add DiagramComponent control to the MainPage.xaml. Open it in the designer. Right-click MainPage.xaml and select View Designer in the popup menu.



Add xml namespace for the diagrams. Then add DiagramComponent and the panel with Print button. After these steps Main.Page.xaml code should look like this:

<UserControl x:Class="Printing.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"

    xmlns:diagrams="http://schemas.aphalina.com/diagrams"

    mc:Ignorable="d"

    d:DesignHeight="424" d:DesignWidth="652">

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

        <Grid>

            <Grid.ColumnDefinitions>

                <ColumnDefinition Width="*"/>

                <ColumnDefinition Width="Auto"/>

            </Grid.ColumnDefinitions>

            <diagrams:DiagramComponent x:Name="diagramComponent"/>

            <Grid Grid.Column="1" Background="White">

                <StackPanel Margin="5">

                    <Button Margin="5" Content="Print diagram" FontWeight="Bold" Click="Print_Click"/>

                </StackPanel>

            </Grid>

        </Grid>

    </Grid>

</UserControl>

Please, take a look at the following line:

<Button Margin="5" Content="Print diagram" FontWeight="Bold" Click="Print_Click"/>

 //in this line we add the Print diagram button

Step 4

You need to add ready diagrams in your project. In order to do this right-click on Serialization, select Add, then select New Folder and name it Diagrams.



It should look like this:



Then right-click on this folder, select Add -> Existing Item…



Then select .grl files, that you’ve created earlier or .grl files from the samples. Then click on Add. In this example we add Factorial.grl.



Your Solution Explorer should look like this:



Then open Properties of added diagrams.



Select BuildAction, then Resource.


Repeat these steps with other added diagrams.

Step 5

Open the MainPage.xaml.cs source code and add the following code.

    public partial class MainPage : UserControl

    {

        public MainPage()

        {

            InitializeComponent();

        }

        private void Print_Click(object sender, RoutedEventArgs e)

        {

            if (diagramComponent.Document != null)

            {

                var printWork = new PrintWork(diagramComponent.DocumentModel.DocumentName, diagramComponent.Document);

                printWork.Run();

            }

        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)

        {

            using (var stream = ResourceHelper.LoadStream(Assembly.GetExecutingAssembly(), "Diagrams/Factorial.grl"))

            {

                diagramComponent.OpenDiagram(stream, "Factorial algorithm");

            }

        }

    }

Please, take a look at the following lines:

using (var stream = ResourceHelper.LoadStream(Assembly.GetExecutingAssembly(), "Diagrams/Factorial.grl"))

            {

                diagramComponent.OpenDiagram(stream, "Factorial algorithm");

            }

 //in this lines we load the file from the resources to stream, and then open it

private void Print_Click(object sender, RoutedEventArgs e)

        {

            if (diagramComponent.Document != null)

            {

                var printWork = new PrintWork(diagramComponent.DocumentModel.DocumentName, diagramComponent.Document);

                printWork.Run();

            }

        }

//in this lines send the current document to print

Step 6

Launch application by clicking the “Start Debugging”  button on the main Visual Studio toolbar. 



The application will open in the browser.



You can print your diagrams by clicking on Print diagram button.

Add Feedback