1 | using System.Drawing;
|
---|
2 | using System.Drawing.Drawing2D;
|
---|
3 | using System.Drawing.Printing;
|
---|
4 | using System.Windows.Forms;
|
---|
5 |
|
---|
6 | namespace Netron.Diagramming.Core {
|
---|
7 | // ----------------------------------------------------------------------
|
---|
8 | /// <summary>
|
---|
9 | /// Handles the printing of a diagram. All pages in the diagram are
|
---|
10 | /// printed. A print preview or actual print can be performed.
|
---|
11 | /// </summary>
|
---|
12 | // ----------------------------------------------------------------------
|
---|
13 | public class DiagramPrinter {
|
---|
14 | PrintDocument myDocument;
|
---|
15 | IDiagramControl myDiagram;
|
---|
16 | //PrinterSettings mySettings;
|
---|
17 | PageSettings myPageSettings;
|
---|
18 | int currentPageIndex = 0;
|
---|
19 | int numberOfPages = 1;
|
---|
20 |
|
---|
21 | // ------------------------------------------------------------------
|
---|
22 | /// <summary>
|
---|
23 | /// Constructor.
|
---|
24 | /// </summary>
|
---|
25 | /// <param name="pageSettings">PageSettings: The page setup for the
|
---|
26 | /// diagram.</param>
|
---|
27 | /// <param name="diagram">IDiagramControl: The diagram to print.
|
---|
28 | /// </param>
|
---|
29 | /// <param name="printPreviewOnly">bool: Specifies if only a print
|
---|
30 | /// preview is to be performed. If true, then a PrintPreviewDialog
|
---|
31 | /// is shown. If false, then a PrintDialog is shown and the diagram
|
---|
32 | /// is printed if the user elects to.</param>
|
---|
33 | // ------------------------------------------------------------------
|
---|
34 | public DiagramPrinter(
|
---|
35 | PageSettings pageSettings,
|
---|
36 | IDiagramControl diagram,
|
---|
37 | bool printPreviewOnly) {
|
---|
38 | this.myDiagram = diagram;
|
---|
39 | this.numberOfPages = diagram.Controller.Model.Pages.Count;
|
---|
40 | this.myPageSettings = pageSettings;
|
---|
41 | this.myDocument = new PrintDocument();
|
---|
42 | this.myDocument.DefaultPageSettings = this.myPageSettings;
|
---|
43 | this.myDocument.PrintPage +=
|
---|
44 | new PrintPageEventHandler(PrintPage);
|
---|
45 |
|
---|
46 | if (printPreviewOnly) {
|
---|
47 | this.PrintPreview();
|
---|
48 | } else {
|
---|
49 | this.Print();
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | // ------------------------------------------------------------------
|
---|
54 | /// <summary>
|
---|
55 | /// Print previews the diagram by displaying a PrintPreviewDialog.
|
---|
56 | /// The diagram, however, can be printed from the PrintPreviewDialog.
|
---|
57 | /// </summary>
|
---|
58 | // ------------------------------------------------------------------
|
---|
59 | void PrintPreview() {
|
---|
60 | PrintPreviewDialog dialog = new PrintPreviewDialog();
|
---|
61 | dialog.Document = this.myDocument;
|
---|
62 | dialog.ShowDialog();
|
---|
63 | }
|
---|
64 |
|
---|
65 | // ------------------------------------------------------------------
|
---|
66 | /// <summary>
|
---|
67 | /// Displays a PrintDialog and prints the diagram if the user elects
|
---|
68 | /// to continue.
|
---|
69 | /// </summary>
|
---|
70 | // ------------------------------------------------------------------
|
---|
71 | void Print() {
|
---|
72 | PrintDialog dialog = new PrintDialog();
|
---|
73 | dialog.Document = this.myDocument;
|
---|
74 |
|
---|
75 | if (dialog.ShowDialog() == DialogResult.OK) {
|
---|
76 | this.myDocument.Print();
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | // ------------------------------------------------------------------
|
---|
81 | /// <summary>
|
---|
82 | /// Handles the actual rendering of the diagram to the print document.
|
---|
83 | /// </summary>
|
---|
84 | /// <param name="sender">object</param>
|
---|
85 | /// <param name="e">PrintPageEventArgs</param>
|
---|
86 | // ------------------------------------------------------------------
|
---|
87 | void PrintPage(object sender, PrintPageEventArgs e) {
|
---|
88 | Graphics g = e.Graphics;
|
---|
89 | IPage page =
|
---|
90 | this.myDiagram.Controller.Model.Pages[currentPageIndex];
|
---|
91 | Bundle bundle = new Bundle(page.Entities);
|
---|
92 | Matrix m = new Matrix();
|
---|
93 | m.Translate(
|
---|
94 | -page.Bounds.Location.X,
|
---|
95 | -page.Bounds.Location.Y);
|
---|
96 | g.Transform = m;
|
---|
97 | bundle.Paint(g);
|
---|
98 | if ((currentPageIndex + 1) < this.numberOfPages) {
|
---|
99 | currentPageIndex++;
|
---|
100 | e.HasMorePages = true;
|
---|
101 | } else {
|
---|
102 | currentPageIndex = 0;
|
---|
103 | e.HasMorePages = false;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|