[Erledigt] PrintDocument
-
Morgen!!
Hätte mal ne Frage zu dieser PrintDokumentklasse:
Und zwar erstell ich mir nen Ausdruck als "normale" Textdatei, die will ich dann mit PrintDocument ausdrucken (und dabei eben Schriftart, -größe usw. anpassen).
Mein Problem ist nun folgendes: Und zwar hab ich im txt-File Escape-Sequenzen drin, vor allem eben die horizontalen Tabs (\t). Nur auf dem Ausdruck über PrintDocument tauchen die Tabs nicht mehr auf, d.h. die sind da plötzlich weg (warum der Ausdruck eben besch.... aussieht).Gibt es da ne Option oder nen Trick?
Danke euch schon mal!
-
Wie druckst du denn den Text? Schreibst den Text über DrawString oder?
-
Hätt ich vielleicht mal dazu schreiben sollen!?
Genau, drucke das mit DrawString raus; ist genau das Beispiel aus der MSDN (auch die Sache mit den mehrfachen Seiten usw......).
Das Drucken klappt ja, nur eben ohne die Escape-Sequenzen in meiner txt-Datei!
-
OK, hier mal noch paar Zusatz-Infos:
Meine txt-Datei erstell ich mit nem Streamwritervoid ErstelleAusdruck(ArrayEigeneKlasse1, ArrayEigeneKlasse2){ System::IO::StreamWriter^ sw = gcnew System::IO::StreamWriter("ausdruck.txt", false); sw->WriteLine("Drehmoment:\t\tT = " + dDatenAusBerechnung + " kNm"); .... }
Wenn ich dann die txt-Datei öffne, erscheinen auch die Tabs und der Text ist "leicht" formatiert, so dass die Daten aus meiner Berechnung eben schön untereinander stehen (so wie ich das haben will).
Drucken will ich es dann so:
//ButtonKlickEreignis: ... this->mErstelleAusdruck(this->HVrechnung[i], this->HVspeichern[i], i+1); System::Drawing::Printing::PrintDocument^ pd = gcnew System::Drawing::Printing::PrintDocument; pd->PrintPage += gcnew PrintPageEventHandler( this, &Form1::pd_PrintPage ); pd->Print();
wobei ich hier noch die Methode pd_PrintPage habe, die ich aus MSDN übernommen habe:
private: void pd_PrintPage( Object^ sender, System::Drawing::Printing::PrintPageEventArgs^ ev ){ float linesPerPage = 0; float yPos = 0; int count = 0; float leftMargin = (float)ev->MarginBounds.Left; float topMargin = (float)ev->MarginBounds.Top; String^ line = nullptr; System::Drawing::Font^ printFont = gcnew System::Drawing::Font("Arial", 9); StreamReader^ streamToPrint = gcnew StreamReader("ausdruck.txt"); // Calculate the number of lines per page. linesPerPage = ev->MarginBounds.Height / printFont->GetHeight(ev->Graphics); // Print each line of the file. while (count < linesPerPage && ((line = streamToPrint->ReadLine()) != nullptr)) { yPos = topMargin + (count * printFont->GetHeight(ev->Graphics)); ev->Graphics->DrawString(line, printFont, Brushes::Black, leftMargin, yPos, gcnew StringFormat); count++; } // If more lines exist, print another page. if (!streamToPrint->EndOfStream) ev->HasMorePages = true; else ev->HasMorePages = false; streamToPrint->Close(); }
Auf dem ausdruck hab ich dann das Problem, dass die TabStopps plötzlich weg sind?!?
Wer weiß Rat? Danke
-
Hat sich erledigt!
Mann muss nur der DrawString-Methode ein weiteres Argument übergeben (stringFormat):
private: void pd_PrintPage( Object^ sender, System::Drawing::Printing::PrintPageEventArgs^ ev ){ float linesPerPage = 0; float yPos = 0; int count = 0; float leftMargin = (float)ev->MarginBounds.Left; float topMargin = (float)ev->MarginBounds.Top; String^ line = nullptr; System::Drawing::Font^ printFont = gcnew System::Drawing::Font("Arial", 9); StreamReader^ streamToPrint = gcnew StreamReader("ausdruck.txt"); StringFormat^ stringFormat = gcnew StringFormat(); array<float>^ tab = {400, 50, 25, 30}; // Festlegen der Position der TabStopps stringFormat->SetTabStops(0, tab); // An stringFormat uebergeben // Anzahl der Zeilen berechnen linesPerPage = ev->MarginBounds.Height / printFont->GetHeight(ev->Graphics); try{ // Jede einzelne Zeile des Quelltextes zeichnen/drucken while (count < linesPerPage && ((line = streamToPrint->ReadLine()) != nullptr)) { yPos = topMargin + (count * printFont->GetHeight(ev->Graphics)); ev->Graphics->DrawString(line, printFont, Brushes::Black, leftMargin, yPos, stringFormat); count++; } // Falls Seitenende erreicht, Flag setzen, dass Dokument mehrseitig ist if (!streamToPrint->EndOfStream) ev->HasMorePages = true; else ev->HasMorePages = false; } finally{ streamToPrint->Close(); } }