private void mitemFilePrint_Click(object sender, EventArgs e) { PrinterCE prce = null; try { // Create PrinterCE object needed for printing. prce = new PrinterCE("LICENSE KEY"); // Select font. prce.FontName = tboxInput.Font.Name; prce.FontSize = (int)(tboxInput.Font.Size * (-1)); // Connect to printer. prce.SelectPrinter(true); // Print PrintJob_Field.PrintText(tboxInput, prce); } catch (PrinterCEException) { MessageBox.Show("PrinterCE Exception", "Exception"); } finally { if (prce != null) { // Disconnect from printer. prce.ShutDown(); } prce = null; } }
//-------------------------------------------------------- //-------------------------------------------------------- public static void PrintText(TextBox textIn, PrinterCE prce) { // Define drawing coordinates // Units are pixels prce.ScaleMode = PrinterCE.MEASUREMENT_UNITS.PIXELS; // Get device resolution double cxyInch = prce.PrinterResolution; // Set margins to 1-inch. prce.PrLeftMargin = cxyInch; prce.PrTopMargin = cxyInch; prce.PrRightMargin = cxyInch; prce.PrBottomMargin = cxyInch; // Calculate page size. double cxPhysPage = prce.PrPgWidth; double cyPhysPage = prce.PrPgHeight; // Calculate line height. // For timing tests, hard-code this value to // 46 for line height for 10 Point Courier New. double cyLineHeight = prce.GetStringHeight; // Init text drawing coordinates; double xText = 0; double yText = 0; // Calculate page boundaries double yFirst = yText; double yLast = cyPhysPage; // Split input data into separate lines of text. char [] achNewLine = new char[] { '\n' }; String [] astrSplit; astrSplit = textIn.Text.Split(achNewLine); // Check for longest string in the document int i; int cchMax = 0; int cstr = astrSplit.Length; for (i = 0; i < cstr; i++) { if (astrSplit[i].Length > cchMax) { cchMax = astrSplit[i].Length; } } // Set iEnd -- trim extra carriage-return from text int iEnd = 0; int cchString = astrSplit[0].Length; char ch = astrSplit[0][cchString - 1]; if (ch == '\r') { iEnd = -1; } // Loop on available strings. for (i = 0; i < cstr; i++) { cchString = astrSplit[i].Length; if (cchString > 0) { // Draw line of text. prce.DrawText(astrSplit[i], xText, yText, cchString + iEnd); } // Advance to next line. yText += cyLineHeight; // Skip to next page (if not at end of document) if (yText >= yLast && (i + 1) < cstr) { prce.NewPage(); yText = yFirst; } } // End of page & end of document. prce.EndDoc(); } // PrintText()