[Debutant] PrintDocument() dans Silverlight
Bonjour a tous,
j'imprime une page xaml en silverlight, pour cela j'utilise cette methode
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public static void PrintData(Panel source, string documentName)
{
PrintDocument doc = new PrintDocument();
double offsetY = 0d;
double totalHeight = 0d;
Canvas canvas = new Canvas();
ReArrangePage(source);
canvas.Children.Add(_pageRoot);
doc.PrintPage += (s, e) =>
{
e.PageVisual = canvas;
if (totalHeight == 0)
{
totalHeight = _pageRoot.DesiredSize.Height;
}
Canvas.SetTop(_pageRoot, -offsetY);
offsetY += e.PrintableArea.Height;
e.HasMorePages = offsetY <= totalHeight;
};
doc.Print(documentName);
} |
Dans cette methode, la fonction ReArrangePage(source) parcourt tous les enfant du panel "source" et recréé une page avec tous les enfants agrandie a leur taille maximum, ce qui permet d'afficher toute les données de ma page "source" lors de l'impression.
Vu que du code vaut mieux qu'une explication a rallonge:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
private static void ReArrangePage(Panel parent)
{
UIElement item;
_pageRoot = new StackPanel { Orientation = Orientation.Vertical, VerticalAlignment = System.Windows.VerticalAlignment.Stretch };
foreach (UIElement ele in parent.Children)
{
if (ele is Panel)
{
switch (ele.GetType().Name)
{
case "Grid":
{
item = new Grid { Height = ele.DesiredSize.Height };
foreach (UIElement child in (ele as Panel).Children)
(item as Grid).Children.Add(AddChildren(child));
break;
}
case "StackPanel":
{
item = new StackPanel { Height = ele.DesiredSize.Height };
foreach (UIElement child in (ele as Panel).Children)
(item as StackPanel).Children.Add(AddChildren(child));
break;
}
default:
{
item = new TextBlock {Text = ele.GetType().Name + " type is not implemented yet for data printing"};
break;
}
}
_pageRoot.Children.Add(item);
}
else if (ele is ItemsControl)
{
switch (ele.GetType().Name)
{
case "ListBox":
{
item = new ListBox { Height = ele.DesiredSize.Height };
foreach (UIElement child in (ele as ListBox).Items)
(item as ListBox).Items.Add(AddChildren(child));
break;
}
default:
{
item = new TextBlock { Text = ele.GetType().Name + " type is not implemented yet for data printing" };
break;
}
}
_pageRoot.Children.Add(item);
}
else if (ele is DataGrid)
{
double height = 0;
foreach (DataGridRow r in (ele as DataGrid).GetRows())
height += r.Height;
height += (ele as DataGrid).ColumnHeaderHeight;
item = new DataGrid { Height = height, ItemsSource = (ele as DataGrid).ItemsSource };
_pageRoot.Children.Add(item);
}
else
{
switch (ele.GetType().Name)
{
case "TextBox":
item = new TextBox { Text = (ele as TextBox).Text, Width = (ele as TextBox).Width };
break;
case "RadioButton":
item = new RadioButton { Content = (ele as RadioButton).Content, IsChecked = (ele as RadioButton).IsChecked, Width = (ele as RadioButton).Width };
break;
case "CheckBox":
item = new CheckBox { Content = (ele as CheckBox).Content, IsChecked = (ele as CheckBox).IsChecked, Width = (ele as CheckBox).Width };
break;
case "TextBlock":
item = new TextBlock { Text = (ele as TextBlock).Text, Width = (ele as TextBlock).Width };
break;
case "Button":
item = new Button { Content = (ele as Button).Content, Width = (ele as Button).Width };
break;
default:
item = new TextBlock { Text = ele.GetType().Name + " type is not implemented yet for data printing" };
break;
}
_pageRoot.Children.Add(item);
}
}
} |
Le seul soucis c'est que du coup je n'arrive pas a créer un header et footer par défaut a mon printdocument. Si l'un d'entre vous a une idée, je suis preneur.
En vous remerciant.