How to print with margins

I am making a Wordpad program. I do this function where you press this button and it prints to your default printer. I did some research and I found functional code that prints to my printer:

private void buttonPrint_Click(object sender, EventArgs e)
    {
        string print = "" + textBody.Text;

        PrintDocument p = new PrintDocument();
        p.PrintPage += delegate(object sender1, PrintPageEventArgs e1)
        {
            e1.Graphics.DrawString(print, new Font("Times New Roman", 12), new     SolidBrush(Color.Black), new RectangleF(0, 0, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));
        };
        try
        {
            p.Print();
        }
        catch (Exception ex)
        {
            throw new Exception("Exception Occured While Printing", ex);
        }
    }

      

This is currently working, but I was wondering if I could do it with fields it doesn't have now. All it does is:

<Top of Page>
<Message>

      

There are no margins on the top, sides (left, right) and bottom. How can I change my code to have fields?

+3


source to share


1 answer


On PrintDocument

you can install Margins

on the object DefaultPageSettings

:



Margins margins = new Margins(100,100,100,100);
p.DefaultPageSettings.Margins = margins;

      

+1


source







All Articles