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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
/// <summary>
/// Method used to print a visual so that it fits the page.
/// Use the given page settings.
/// </summary>
/// <param name="elementToPrint_l">The visual to print.</param>
/// <param name="pageSettings_p">Page settings (margins, paper size, portrait/landscape, etc).</param>
/// <param name="printerSettings_p">Printer settings (printer name, etc).</param>
public static void print(FrameworkElement elementToPrint_l,
PageSettings pageSettings_p,
PrinterSettings printerSettings_p)
{
// Create the print dialog object and set options
PrintDialog printDialog_l = new PrintDialog();
printDialog_l.PageRangeSelection = PageRangeSelection.AllPages;
printDialog_l.UserPageRangeEnabled = true;
// Display the dialog. This returns true if the user presses the Print button.
bool? print_l = printDialog_l.ShowDialog();
if (print_l == true)
{
// Get selected printer capabilities.
PrintCapabilities capabilities_l =
printDialog_l.PrintQueue.GetPrintCapabilities(printDialog_l.PrintTicket);
// Compute the scale to apply to the visual in order for it to fit in the page.
double printableWidth_l;
double printableHeight_l;
if (pageSettings_p.Landscape)
{
printableWidth_l = capabilities_l.PageImageableArea.ExtentHeight - pageSettings_p.Margins.Left - pageSettings_p.Margins.Right;
printableHeight_l = capabilities_l.PageImageableArea.ExtentWidth - pageSettings_p.Margins.Top - pageSettings_p.Margins.Bottom;
}
else
{
printableWidth_l = capabilities_l.PageImageableArea.ExtentWidth - pageSettings_p.Margins.Left - pageSettings_p.Margins.Right;
printableHeight_l = capabilities_l.PageImageableArea.ExtentHeight - pageSettings_p.Margins.Top - pageSettings_p.Margins.Bottom;
}
double scale_l = Math.Min(printableWidth_l / elementToPrint_l.ActualWidth,
printableHeight_l / elementToPrint_l.ActualHeight);
//Save the printed element old attributes in order to render it back as
//it was before printing (the printing mechanism modifying the actual
//element render).
//The transformation applied to the element to print.
Transform oldTransform_l = elementToPrint_l.LayoutTransform;
//The size of the element to print.
Size oldSize_l = new Size(elementToPrint_l.ActualWidth,
elementToPrint_l.ActualHeight);
//The position of the element to print inside its parent visual.
GeneralTransform transform_l = elementToPrint_l.TransformToAncestor((Visual)elementToPrint_l.Parent);
Point topLeft_l = transform_l.Transform(new Point(0, 0));
// Transform the Visual with the computed scale factor.
TransformGroup transformGroup_l = new TransformGroup();
transformGroup_l.Children.Add(new ScaleTransform(scale_l,
scale_l));
if (pageSettings_p.Landscape)
{
transformGroup_l.Children.Add(new RotateTransform(90.0));
transformGroup_l.Children.Add(new TranslateTransform(pageSettings_p.Margins.Bottom,
pageSettings_p.Margins.Left));
}
else
{
transformGroup_l.Children.Add(new TranslateTransform(pageSettings_p.Margins.Left,
pageSettings_p.Margins.Top));
}
//elementToPrint_l.LayoutTransform = new ScaleTransform(scale_l, scale_l);
elementToPrint_l.LayoutTransform = transformGroup_l;
// Get the size of the printer page.
Size size_l;
if (pageSettings_p.Landscape)
{
size_l = new Size(printableHeight_l,
printableWidth_l);
}
else
{
size_l = new Size(printableWidth_l,
printableHeight_l);
}
// Update the layout of the visual to the printer page size.
elementToPrint_l.Measure(size_l);
if (pageSettings_p.Landscape)
{
elementToPrint_l.Arrange(new Rect(
new Point(pageSettings_p.Margins.Bottom,
pageSettings_p.Margins.Left),
size_l));
}
else
{
elementToPrint_l.Arrange(new Rect(
new Point(pageSettings_p.Margins.Left,
pageSettings_p.Margins.Top),
size_l));
}
//Print the visual.
printDialog_l.PrintVisual(elementToPrint_l, "Printing active panel...");
// Reinitialize panel's attributes.
elementToPrint_l.LayoutTransform = oldTransform_l;
elementToPrint_l.Measure(oldSize_l);
elementToPrint_l.Arrange(new Rect(topLeft_l, oldSize_l));
}
} |
Partager