Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions export-multiple-charts-to-pdf/Sample/ExportCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Windows.Input;

namespace Sample
{
public class ExportCommand : ICommand
{
private Action<object> execute;

private Predicate<object> canExecute;

private event EventHandler CanExecuteChangedInternal;

public ExportCommand(Action<object> execute)
: this(execute, DefaultCanExecute)
{
}

public ExportCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
{
throw new ArgumentNullException("execute");
}

if (canExecute == null)
{
throw new ArgumentNullException("canExecute");
}

this.execute = execute;
this.canExecute = canExecute;
}

public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
this.CanExecuteChangedInternal += value;
}

remove
{
CommandManager.RequerySuggested -= value;
this.CanExecuteChangedInternal -= value;
}
}

public bool CanExecute(object parameter)
{
return this.canExecute != null && this.canExecute(parameter);
}

public void Execute(object parameter)
{
this.execute(parameter);
}

public void OnCanExecuteChanged()
{
EventHandler handler = this.CanExecuteChangedInternal;
if (handler != null)
{
//DispatcherHelper.BeginInvokeOnUIThread(() => handler.Invoke(this, EventArgs.Empty));
handler.Invoke(this, EventArgs.Empty);
}
}

public void Destroy()
{
this.canExecute = _ => false;
this.execute = _ => { return; };
}

private static bool DefaultCanExecute(object parameter)
{
return true;
}
}

}
9 changes: 5 additions & 4 deletions export-multiple-charts-to-pdf/Sample/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
<local1:ViewModel/>
</Window.DataContext>

<Grid Margin="0, 10, 0, 0">
<Grid Margin="0, 10, 0, 0" x:Name="grid">

<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="100"/>
<RowDefinition Height="60"/>
<RowDefinition Height="60"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">

Expand All @@ -33,7 +34,7 @@
</syncfusion:NumericalAxis>
</syncfusion:SfChart.SecondaryAxis>

<syncfusion:ColumnSeries
<syncfusion:ColumnSeries
Interior="#FF8BC34A"
ItemsSource="{Binding Data}"
XBindingPath="XValue"
Expand Down Expand Up @@ -126,7 +127,7 @@
</syncfusion:SfChart>

</StackPanel>

<Button x:Name="Print" Grid.Row="5" Content="Print" Width="150" Height="50" Click="Print_Click"/>
<Button Grid.Row="6" Content="Print with MVVM-compatible" Width="200" Height="50" Command="{Binding ExportButtonCommand}" CommandParameter="{x:Reference grid}"/>
</Grid>
</Window>
1 change: 1 addition & 0 deletions export-multiple-charts-to-pdf/Sample/Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="ColorConverter.cs" />
<Compile Include="ExportCommand.cs" />
<Compile Include="ViewModel.cs" />
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
Expand Down
72 changes: 71 additions & 1 deletion export-multiple-charts-to-pdf/Sample/ViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
using System;
using Microsoft.Win32;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.UI.Xaml.Charts;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace Sample
{
public class ViewModel
{
private ICommand exportButtonCommand;

public List<Model> Data { get; set; }

public List<Model> Data1 { get; set; }
Expand All @@ -15,6 +26,18 @@ public class ViewModel

public List<Model> Data4 { get; set; }

public ICommand ExportButtonCommand
{
get
{
return exportButtonCommand;
}
set
{
exportButtonCommand = value;
}
}

public ViewModel()
{
DateTime JanDateTime = new DateTime(2020, 01, 01);
Expand Down Expand Up @@ -51,6 +74,53 @@ public ViewModel()
{
Data4.Add(new Model { XValue = MayDateTime.AddDays(j), YValue = random.Next(0, 100) });
};

ExportButtonCommand = new ExportCommand(ExportChart);
}

private PdfDocument pdfDoc;
private PdfPage page1;
private int spacing = 20;
private int height = 60;
private int x = 10;

public void ExportChart(object obj)
{
var grid = obj as Grid;
if (grid != null)
{
pdfDoc = new PdfDocument();
pdfDoc.PageSettings.Size = PdfPageSize.A4;
pdfDoc.PageSettings.Margins.All = 0;
page1 = new PdfPage();
page1 = pdfDoc.Pages.Add();
float pageWidth = page1.Size.Width - spacing;
float top = 50;

for (int i = 0; i < grid.Children.Count - 2; i++)
{
var chart = (grid.Children[i] as StackPanel).Children[0] as SfChart;
if (chart == null) return;

MemoryStream outStream = new MemoryStream();
chart.Save(outStream, new JpegBitmapEncoder());
PdfBitmap pdfBitmap1 = new PdfBitmap(outStream);
page1.Graphics.DrawImage(pdfBitmap1, new RectangleF(x, top + (i * (height + spacing)), pageWidth, height));
outStream.Close();
}

SaveFileDialog dialog = new SaveFileDialog
{
Filter = "PDF document (*.pdf)|*.pdf"
};
Boolean? result = dialog.ShowDialog();
string fileName = dialog.FileName;
if ((bool)result)
{
pdfDoc.Save(fileName);
}
pdfDoc.Dispose();
}
}
}
}