Mega Code Archive

 
Categories / C# by API / System Drawing Printing
 

Extends PrintDocument

using System; using System.Drawing; using System.Windows.Forms; using System.Drawing.Printing; public class PrintParagraph : Form {     public PrintParagraph()     {         this.cmdPrint = new System.Windows.Forms.Button();         this.SuspendLayout();         this.cmdPrint.Location = new System.Drawing.Point(109, 122);         this.cmdPrint.Size = new System.Drawing.Size(75, 23);         this.cmdPrint.Text = "Print";         this.cmdPrint.UseVisualStyleBackColor = true;         this.cmdPrint.Click += new System.EventHandler(this.cmdPrint_Click);         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;         this.ClientSize = new System.Drawing.Size(282, 259);         this.Controls.Add(this.cmdPrint);         this.Text = "Wrapped Print";         this.ResumeLayout(false);     }     private void cmdPrint_Click(object sender, EventArgs e)     {         string text = "a paragraph";         PrintDocument doc = new ParagraphDocument(text);         doc.PrintPage += new PrintPageEventHandler(this.Doc_PrintPage);         PrintDialog dlgSettings = new PrintDialog();         dlgSettings.Document = doc;         if (dlgSettings.ShowDialog() == DialogResult.OK)         {             doc.Print();         }     }     private void Doc_PrintPage(object sender, PrintPageEventArgs e)     {         ParagraphDocument doc = (ParagraphDocument)sender;         Font font = new Font("Arial", 15);         e.Graphics.DrawString(doc.Text, font, Brushes.Black,                e.MarginBounds, StringFormat.GenericDefault);     }     [STAThread]     static void Main()     {         Application.EnableVisualStyles();         Application.SetCompatibleTextRenderingDefault(false);         Application.Run(new PrintParagraph());     }     private System.Windows.Forms.Button cmdPrint;      } public class ParagraphDocument : PrintDocument {     public string Text;     public ParagraphDocument(string text)     {         this.Text = text;     } }