By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,706 Members | 1,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,706 IT Pros & Developers. It's quick & easy.

Printing / turning 90 degrees

P: n/a
How can I turn what I want to print 90 degrees using the logic below?
Please tell me the code with which to make the modification.

Many thanks,
Patrick.

using System.ComponentModel;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;

public class Printing
{
private Font printFont;
private StreamReader streamToPrint;
static string filePath;
public Printing()
{
doPrinting();
}
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line=null;
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics) ;
// Iterate over the file, printing each line.
while (count < linesPerPage && ((line=streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString (line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
// Print the file.
public void doPrinting()
{
try
{
streamToPrint = new StreamReader (filePath);
try
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
// Print the document.
pd.Print();
}
finally
{
streamToPrint.Close() ;
}
}
catch
{
}
}
public static void send()
{
string arg = "temp.txt";
filePath = arg;
new Printing();
}
}
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Patrick,
For the items you want to print rotated do the following:
System.Drawing.Drawing2D.GraphicsState svd = ev.Graphics.Save();
// then get the x,y location of the top/left of the string you want to
print
// and transform to the new locationi
ev.Graphics.TranslateTransform(x, y, MatrixOrder.Prepend);
// rotate 90 degrees clockwise -- assuming that you want text going up
:=)
ev.Graphics.RotateTransform(-90.0f);
// draw the text here using DrawString
ev.Graphics.Restore(svd); // and restore the saved state
Note that you will be back at the location you started at so you may need to
do some
measuring/repositioning for the next string to draw.

Ron Allen
"Patrick De Ridder" <00@00.00> wrote in message
news:10**************@evisp-news-01.ops.asmr-01.energis-idc.net...
How can I turn what I want to print 90 degrees using the logic below?
Please tell me the code with which to make the modification.

Many thanks,
Patrick.

using System.ComponentModel;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;

public class Printing
{
private Font printFont;
private StreamReader streamToPrint;
static string filePath;
public Printing()
{
doPrinting();
}
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line=null;
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics) ;
// Iterate over the file, printing each line.
while (count < linesPerPage && ((line=streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString (line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
// Print the file.
public void doPrinting()
{
try
{
streamToPrint = new StreamReader (filePath);
try
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
// Print the document.
pd.Print();
}
finally
{
streamToPrint.Close() ;
}
}
catch
{
}
}
public static void send()
{
string arg = "temp.txt";
filePath = arg;
new Printing();
}
}

Nov 15 '05 #2

P: n/a
Thanks Ron,
do I understand that I need to know the coordinates of the strings at zero
degrees
to be able to turn them ninety degrees? (I want to print envelopes).

Is there no simple(r) way to modify the (MSDN) code (below)?

Patrick.
"Ron Allen" <rallen@_nospam_src-us.com> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
Patrick,
For the items you want to print rotated do the following:
System.Drawing.Drawing2D.GraphicsState svd = ev.Graphics.Save();
// then get the x,y location of the top/left of the string you want to
print
// and transform to the new locationi
ev.Graphics.TranslateTransform(x, y, MatrixOrder.Prepend);
// rotate 90 degrees clockwise -- assuming that you want text going up
:=)
ev.Graphics.RotateTransform(-90.0f);
// draw the text here using DrawString
ev.Graphics.Restore(svd); // and restore the saved state
Note that you will be back at the location you started at so you may need to do some
measuring/repositioning for the next string to draw.

Ron Allen
"Patrick De Ridder" <00@00.00> wrote in message
news:10**************@evisp-news-01.ops.asmr-01.energis-idc.net...
How can I turn what I want to print 90 degrees using the logic below?
Please tell me the code with which to make the modification.

Many thanks,
Patrick.

using System.ComponentModel;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;

public class Printing
{
private Font printFont;
private StreamReader streamToPrint;
static string filePath;
public Printing()
{
doPrinting();
}
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line=null;
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics) ;
// Iterate over the file, printing each line.
while (count < linesPerPage && ((line=streamToPrint.ReadLine()) != null)) {
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString (line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
// Print the file.
public void doPrinting()
{
try
{
streamToPrint = new StreamReader (filePath);
try
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
// Print the document.
pd.Print();
}
finally
{
streamToPrint.Close() ;
}
}
catch
{
}
}
public static void send()
{
string arg = "temp.txt";
filePath = arg;
new Printing();
}
}


Nov 15 '05 #3

P: n/a
How do I integrate your code into that which I posted?
Patrick.
"Ron Allen" <rallen@_nospam_src-us.com> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
Patrick,
For the items you want to print rotated do the following:
System.Drawing.Drawing2D.GraphicsState svd = ev.Graphics.Save();
// then get the x,y location of the top/left of the string you want to
print
// and transform to the new locationi
ev.Graphics.TranslateTransform(x, y, MatrixOrder.Prepend);
// rotate 90 degrees clockwise -- assuming that you want text going up
:=)
ev.Graphics.RotateTransform(-90.0f);
// draw the text here using DrawString
ev.Graphics.Restore(svd); // and restore the saved state
Note that you will be back at the location you started at so you may need to do some
measuring/repositioning for the next string to draw.

Ron Allen
"Patrick De Ridder" <00@00.00> wrote in message
news:10**************@evisp-news-01.ops.asmr-01.energis-idc.net...
How can I turn what I want to print 90 degrees using the logic below?
Please tell me the code with which to make the modification.

Many thanks,
Patrick.

using System.ComponentModel;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;

public class Printing
{
private Font printFont;
private StreamReader streamToPrint;
static string filePath;
public Printing()
{
doPrinting();
}
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line=null;
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics) ;
// Iterate over the file, printing each line.
while (count < linesPerPage && ((line=streamToPrint.ReadLine()) != null)) {
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString (line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
// Print the file.
public void doPrinting()
{
try
{
streamToPrint = new StreamReader (filePath);
try
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
// Print the document.
pd.Print();
}
finally
{
streamToPrint.Close() ;
}
}
catch
{
}
}
public static void send()
{
string arg = "temp.txt";
filePath = arg;
new Printing();
}
}


Nov 15 '05 #4

P: n/a
Patrick,
It depends on where you want the printed line/string to be rotated. Do
you want everything rotated as in landscape printing? If so, it may just be
easier to set the PageSettings.Landscape property to true for your pages (in
OnQueryPageSettings is where I normally do this). This basically does the
rotation and the required translation for you.
You can do about the same thing by doing a Rotate(-90.0f) and then a
Translate by the width of the page (in portrait mode) to get the the
coordinate system aligned correctly. This should have your position at the
top/left of the drawing area In this case you don't need to save the state
of the Graphics objects as you will be using the translated/rotated
coordinates throughout. You would put the Translate and Rotate before all
the other drawing statements.
To do just one line/string you need to know where you are going to start
the string so you can rotate just that part and then go back to using the
standard coordinate system.

Ron Allen

"Patrick De Ridder" <wn********@all.here> wrote in message
news:10***************@evisp-news-01.ops.asmr-01.energis-idc.net...
How do I integrate your code into that which I posted?
Patrick.
"Ron Allen" <rallen@_nospam_src-us.com> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
Patrick,
For the items you want to print rotated do the following:
System.Drawing.Drawing2D.GraphicsState svd = ev.Graphics.Save();
// then get the x,y location of the top/left of the string you want to
print
// and transform to the new locationi
ev.Graphics.TranslateTransform(x, y, MatrixOrder.Prepend);
// rotate 90 degrees clockwise -- assuming that you want text going up :=)
ev.Graphics.RotateTransform(-90.0f);
// draw the text here using DrawString
ev.Graphics.Restore(svd); // and restore the saved state
Note that you will be back at the location you started at so you may
need to
do some
measuring/repositioning for the next string to draw.

Ron Allen
"Patrick De Ridder" <00@00.00> wrote in message
news:10**************@evisp-news-01.ops.asmr-01.energis-idc.net...
How can I turn what I want to print 90 degrees using the logic below?
Please tell me the code with which to make the modification.

Many thanks,
Patrick.

using System.ComponentModel;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;

public class Printing
{
private Font printFont;
private StreamReader streamToPrint;
static string filePath;
public Printing()
{
doPrinting();
}
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line=null;
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics) ;
// Iterate over the file, printing each line.
while (count < linesPerPage && ((line=streamToPrint.ReadLine()) !=

null)) {
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString (line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
// Print the file.
public void doPrinting()
{
try
{
streamToPrint = new StreamReader (filePath);
try
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
// Print the document.
pd.Print();
}
finally
{
streamToPrint.Close() ;
}
}
catch
{
}
}
public static void send()
{
string arg = "temp.txt";
filePath = arg;
new Printing();
}
}



Nov 15 '05 #5

P: n/a
Your reference to

DefaultPageSettings.Landscape

has lead me on to an example in MSDN, which I expect is going
resolve my printing query.

I need the rotation business for envelope printing in an app.

(ad interim I am having the HP printer swing the image around,
for which it only needs a couple of special characters)

Thank you.

Patrick.
"Ron Allen" <rallen@_nospam_src-us.com> wrote in message
news:uR**************@TK2MSFTNGP11.phx.gbl...
Patrick,
It depends on where you want the printed line/string to be rotated. Do you want everything rotated as in landscape printing? If so, it may just be easier to set the PageSettings.Landscape property to true for your pages (in OnQueryPageSettings is where I normally do this). This basically does the
rotation and the required translation for you.
You can do about the same thing by doing a Rotate(-90.0f) and then a
Translate by the width of the page (in portrait mode) to get the the
coordinate system aligned correctly. This should have your position at the top/left of the drawing area In this case you don't need to save the state of the Graphics objects as you will be using the translated/rotated
coordinates throughout. You would put the Translate and Rotate before all
the other drawing statements.
To do just one line/string you need to know where you are going to start the string so you can rotate just that part and then go back to using the
standard coordinate system.

Ron Allen

"Patrick De Ridder" <wn********@all.here> wrote in message
news:10***************@evisp-news-01.ops.asmr-01.energis-idc.net...
How do I integrate your code into that which I posted?
Patrick.
"Ron Allen" <rallen@_nospam_src-us.com> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
Patrick,
For the items you want to print rotated do the following:
System.Drawing.Drawing2D.GraphicsState svd = ev.Graphics.Save();
// then get the x,y location of the top/left of the string you want
to
print
// and transform to the new locationi
ev.Graphics.TranslateTransform(x, y, MatrixOrder.Prepend);
// rotate 90 degrees clockwise -- assuming that you want text
going
up :=)
ev.Graphics.RotateTransform(-90.0f);
// draw the text here using DrawString
ev.Graphics.Restore(svd); // and restore the saved state
Note that you will be back at the location you started at so you may

need
to
do some
measuring/repositioning for the next string to draw.

Ron Allen
"Patrick De Ridder" <00@00.00> wrote in message
news:10**************@evisp-news-01.ops.asmr-01.energis-idc.net...
> How can I turn what I want to print 90 degrees using the logic

below? > Please tell me the code with which to make the modification.
>
> Many thanks,
> Patrick.
>
> using System.ComponentModel;
> using System.Drawing;
> using System.Drawing.Printing;
> using System.IO;
> using System.Windows.Forms;
>
> public class Printing
> {
> private Font printFont;
> private StreamReader streamToPrint;
> static string filePath;
> public Printing()
> {
> doPrinting();
> }
> // The PrintPage event is raised for each page to be printed.
> private void pd_PrintPage(object sender, PrintPageEventArgs ev)
> {
> float linesPerPage = 0;
> float yPos = 0;
> int count = 0;
> float leftMargin = ev.MarginBounds.Left;
> float topMargin = ev.MarginBounds.Top;
> string line=null;
> // Calculate the number of lines per page.
> linesPerPage = ev.MarginBounds.Height /
> printFont.GetHeight(ev.Graphics) ;
> // Iterate over the file, printing each line.
> while (count < linesPerPage && ((line=streamToPrint.ReadLine()) !=

null))
> {
> yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
> ev.Graphics.DrawString (line, printFont, Brushes.Black,
> leftMargin, yPos, new StringFormat());
> count++;
> }
> // If more lines exist, print another page.
> if (line != null)
> ev.HasMorePages = true;
> else
> ev.HasMorePages = false;
> }
> // Print the file.
> public void doPrinting()
> {
> try
> {
> streamToPrint = new StreamReader (filePath);
> try
> {
> printFont = new Font("Arial", 10);
> PrintDocument pd = new PrintDocument();
> pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
> // Print the document.
> pd.Print();
> }
> finally
> {
> streamToPrint.Close() ;
> }
> }
> catch
> {
> }
> }
> public static void send()
> {
> string arg = "temp.txt";
> filePath = arg;
> new Printing();
> }
> }
>
>



Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.