Entwickler-Ecke

WinForms - PrintDocument[] neues PrintDocument hinzufügen


Lepheus - Mo 22.12.14 18:28
Titel: PrintDocument[] neues PrintDocument hinzufügen
Hallo Zusammen,

da mir bei meinem letzten Problem so grandios geholfen wurde, nochmals vielen Dank.
Wende ich mich erneut an euch.

Habe folgenden Code im Internet gefunden um mehrere Dokumente auf einmal auszudrucken:


C#-Quelltext
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:
public class MultiPrintDocument : PrintDocument
        {
            private PrintDocument[] _documents;
            private int _docIndex;
            private PrintEventArgs _args;

            // constructors
            public MultiPrintDocument(PrintDocument document1, PrintDocument document2)
            {
                _documents = new PrintDocument[] { document1, document2 };
            }

            public MultiPrintDocument(PrintDocument[] documents)
            {
                _documents = documents;
            }

            // overidden methods
            protected override void OnBeginPrint(PrintEventArgs e)
            {
                base.OnBeginPrint(e);
                if (_documents.Length == 0)
                    e.Cancel = true;

                if (e.Cancel) return;

                _args = e;
                _docIndex = 0;  // reset current document index
                CallMethod(_documents[_docIndex], "OnBeginPrint", e);
            }

            protected override void OnQueryPageSettings(QueryPageSettingsEventArgs e)
            {
                e.PageSettings = _documents[_docIndex].DefaultPageSettings;
                CallMethod(_documents[_docIndex], "OnQueryPageSettings", e);
                base.OnQueryPageSettings(e);
            }

            protected override void OnPrintPage(PrintPageEventArgs e)
            {
                CallMethod(_documents[_docIndex], "OnPrintPage", e);
                base.OnPrintPage(e);
                if (e.Cancel) return;
                if (!e.HasMorePages)
                {
                    CallMethod(_documents[_docIndex], "OnEndPrint", _args);
                    if (_args.Cancel) return;
                    _docIndex++;  // increments the current document index

                    if (_docIndex < _documents.Length)
                    {
                        // says that it has more pages (in others documents)
                        e.HasMorePages = true;
                        CallMethod(_documents[_docIndex], "OnBeginPrint", _args);
                    }
                }
            }

            // use reflection to call protected methods of child documents
            private void CallMethod(PrintDocument document, string methodName, object args)
            {
                typeof(PrintDocument).InvokeMember(methodName,
                  BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic,
                  null, document, new object[] { args });
            }
        }


Nun möchte ich diesen gerne so erweitern, das man im Nachhinein noch ein weiteres Dokument dem MultiPrintDocument hinzufügen kann.
Habt ihr eine Idee wie ich das bewerkstelligen kann? Irgendwie muss man dem PrintDocument[] ein "normales" PrintDocument anhängen, aber wie bewerkstelligt man das man?

Vielen Dank.


Palladin007 - Mo 22.12.14 18:58

Warum vergrößerst du das Array im Konstruktor nicht einfach?

Dabei ist es doch egal, ob das nun PrintDocuments sind, oder Zahlen, das Array funktioniert immer gleich.


C# - Mo 22.12.14 19:06

Die Frage ist warum du eine eigene Klasse anlegst und nicht einfach eine Liste nimmst?
Spare dir doch die Klasse MultiPrintDocument und nimm statt dessen eine List<PrintDocument> documents;. Bei der kannst du dann beliebig Elemente hinzufügen oder entferenen. Ausdrucken kannst du dann alle mit

C#-Quelltext
1:
foreach (PrintDocument doc in documents) doc.Print();                    

Ich verstehe auch nicht, was die ganzen Events bezwecken sollen :gruebel:


Lepheus - Mo 22.12.14 19:08

:oops:

Jetzt wo ihr es sagt, habe viel zu kompliziert gedacht....

Danke