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
|
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private const double ROWHEIGHT = 20; // taille d'une ligne a imprimer
private const double PAGEMARGIN = 30; // taille du header / footer
private int _currentIndex; // ligne actuel
private double _currentTop; // position de la premiere ligne
private double _availableSpace;
private PrintDocument _printDoc;
private formulaire _formulaire = new formulaire(); // formulaire contenant ma liste à imprimer
private void _printDoc_PrintPage(object sender, PrintPageEventArgs e)
{
Canvas pageRoot = new Canvas();
e.PageVisual = pageRoot; // assignation du visuel
this._currentTop = PAGEMARGIN; // hauteur de la premiere ligne
this._availableSpace = e.PrintableArea.Height - PAGEMARGIN * 2; // taille dispo - (header + footer)
while (this._currentIndex < this._formulaire._list.Count)
{
TextBlock text = new TextBlock { Text = "Nom: " + this._formulaire._list[this._currentIndex].Name + ". Age: " + this._formulaire._list[this._currentIndex].Age + ". Address: " + this._formulaire._list[this._currentIndex].Age };
if (ROWHEIGHT > this._availableSpace)
{
e.HasMorePages = true;
break;
}
text.SetValue(Canvas.TopProperty, this._currentTop);
text.SetValue(Canvas.LeftProperty, PAGEMARGIN);
pageRoot.Children.Add(text);
this._currentTop += ROWHEIGHT; // actualise la position de a ligne
this._availableSpace -= ROWHEIGHT; // acutalise la place restante
this._currentIndex++;
}
if (this._currentIndex >= this._formulaire._list.Count)
{
RadioButton radio = new RadioButton { Content = "test radio button", IsChecked = true, Width = 100 }; // s'ajoute sans problème mais ne s'affiche pas
radio.SetValue(Canvas.TopProperty, this._currentTop);
radio.SetValue(Canvas.LeftProperty, PAGEMARGIN);
pageRoot.Children.Add(radio);
this._currentTop += ROWHEIGHT;
this._availableSpace -= ROWHEIGHT;
e.HasMorePages = false;
}
}
private void _printDoc_EndPrint(object sender, EndPrintEventArgs e)
{
Console.WriteLine("Print ended. Result: " + e.Error + ". Page Printed:" + this._printDoc.PrintedPageCount);
}
private void _printDoc_BeginPrint(object sender, BeginPrintEventArgs e)
{
Console.WriteLine("Begin printing");
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this._currentIndex = 0;
this._printDoc = new PrintDocument();
this._printDoc.PrintPage += _printDoc_PrintPage;
this._printDoc.BeginPrint += _printDoc_BeginPrint;
this._printDoc.EndPrint += _printDoc_EndPrint;
this._printDoc.Print(@"C:\Users\ADM\Desktop\DocumentTest");
}
} |
Partager