Results 1 to 3 of 3

Thread: printing in C#

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2004
    Posts
    50

    printing in C#

    Hi, I need send to the printer a simple line and a bold line in C#. Why is so complicate?

    It's so simple in VB:
    VB Code:
    1. Printer.FontBold = False
    2. Printer.Print "Normal line"
    3. Printer.FontBold = True
    4. Printer.Print "Bold line"


    How can I do the same thing in C#?
    Can you give me an example on how to do this, pls?

  2. #2
    Addicted Member
    Join Date
    May 2000
    Location
    Wellington NZ
    Posts
    153

    Re: printing in C#

    I have just written this simple class that shows how to print text. Check this out...

    Code:
    public class MySimpleDocument : PrintDocument
    {
    	private string _strLine1, _strLine2;
    	public MySimpleDocument(string line1, string line2)
    	{
    		_strLine1 = line1;
    		_strLine2 = line2;
    		this.DocumentName = "My Simple Document";
    	}
    
    	protected override void OnBeginPrint(PrintEventArgs e)
    	{
    		// Add code here to initialise any information used by the document
    	}
    
    	protected override void OnPrintPage(PrintPageEventArgs e)
    	{
    		Font normalFont = new Font("Tahoma",10f);
    		Font boldFont = new Font("Tahoma",10f,FontStyle.Bold);
    		e.Graphics.DrawString(_strLine1,normalFont,Brushes.Black,10,10);
    		e.Graphics.DrawString(_strLine2,boldFont,Brushes.Black,10,30);
    
    		normalFont.Dispose();
    		boldFont.Dispose();
    	}
    }
    and then use this print it

    Code:
    MySimpleDocument doc = new MySimpleDocument(this.textBox1.Text, this.textBox2.Text);
    this.printDialog1.Document = doc;
    if(this.printDialog1.ShowDialog() == DialogResult.OK)
    {
    	doc.PrinterSettings = this.printDialog1.PrinterSettings;
    	doc.Print();
    }
    doc.Dispose();
    You dont need eyes to see, you need vision.

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2004
    Posts
    50

    Re: printing in C#

    Thanks, it was very useful your example.
    I have printed my first page in C#. Now I am wondering how can I print the second page and the rest of them, because when my 'y' coordinate it's greater than the height of the page, it's ... game over.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width