Printing / turning 90 degrees

  • Thread starter Thread starter Patrick De Ridder
  • Start date Start date
P

Patrick De Ridder

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();
}
}
 
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
 
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.
 
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
 
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.
 
Back
Top