How do I print someting in landscape orientation in C#?

Posted on 2008-01-31
Medium Priority
Last Modified: 2013-12-17
The following is the relevant code snippet from a simple C# program I am writing.  All it does it prompt the user for a file name, then a printer, and dumps a text file to it.

What I need to do is make the default orientation landscape regardless of what the driver is set to default to.  The user can click portrait if they want, but I want the landscape radio button on the print dialog to be ticked automatically.

Any suggestions?
private void printButton_Click(object sender, System.EventArgs e)
			// Allow the user to select a file.
			OpenFileDialog ofd = new OpenFileDialog();
			if( DialogResult.OK == ofd.ShowDialog(this) )
				// Allow the user to select a printer.
				PrintDialog pd  = new PrintDialog();
				pd.PrinterSettings = new PrinterSettings();
				if( DialogResult.OK == pd.ShowDialog(this) )
					string printPath = ofd.FileName;
					fileToPrint = new System.IO.StreamReader(printPath);
					printFont = new System.Drawing.Font("Courier", 12);
		private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
			float yPos = 0f;
			int count = 0;
			float leftMargin = e.MarginBounds.Left;
			float topMargin = e.MarginBounds.Top;
			string line = null;
			float linesPerPage = e.MarginBounds.Height/printFont.GetHeight(e.Graphics);
			while (count < linesPerPage)
				line = fileToPrint.ReadLine();
				if (line == null)
				yPos = topMargin + count * printFont.GetHeight(e.Graphics);
				e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
			if (line != null)
				e.HasMorePages = true;
		private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)

Open in new window

Question by:cuziyq
1 Comment
LVL 22

Accepted Solution

JimBrandley earned 1000 total points
ID: 20791975
This should do it:
pd.DefaultPageSettings.Landscape = true;


Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question