Printing the whole whole printable area

A

Abhay

private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
RectangleF objR = new RectangleF();
PrintDocument pd = (PrintDocument)sender;

float fT, fB, fL, fR;

//GET THE BEST POSSIBLE TOP MARGIN

if ((float)e.MarginBounds.Top >=
pd.PrinterSettings.DefaultPageSettings.PrintableArea.Top)
{
fT = (float)e.MarginBounds.Top;
}
else
{
fT =
pd.PrinterSettings.DefaultPageSettings.PrintableArea.Top;
}

//GET THE BEST POSSIBLE LEFT MARGIN

if ((float)e.MarginBounds.Left >=
pd.PrinterSettings.DefaultPageSettings.PrintableArea.Left)
{
fL = (float)e.MarginBounds.Left;
}
else
{
fL =
pd.PrinterSettings.DefaultPageSettings.PrintableArea.Left;
}

//GET THE BEST POSSIBLE BOTTOM MARGIN
if ((float)e.MarginBounds.Bottom <=
pd.PrinterSettings.DefaultPageSettings.PrintableArea.Bottom)
{
fB = (float)e.MarginBounds.Bottom;
}
else
{
fB =
pd.PrinterSettings.DefaultPageSettings.PrintableArea.Bottom;
}


//GET THE BEST POSSIBLE RIGHT MARGIN
if ((float)e.MarginBounds.Right <=
pd.PrinterSettings.DefaultPageSettings.PrintableArea.Right)
{
fR = (float)e.MarginBounds.Right;
}
else
{
fR =
pd.PrinterSettings.DefaultPageSettings.PrintableArea.Right;
}

objR.Location = new PointF(fL, fT);

objR.Height = fB - fT;
objR.Width = fR - fL;


e.Graphics.DrawRectangle(Pens.Black, objR.Left , objR.Top ,
objR.Width , objR.Height );

}



i want to utilize the whole printable area to print a rectangle
(creating some vouchers) when i go and print i dont get the RIGHT and
BOTTOM margins the left and top are perfect

whats the reason ??
 
A

Abhay

void pd_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawRectangle(Pens.Black,
e.PageSettings.PrintableArea.X, e.PageSettings.PrintableArea.Y,
e.PageSettings.PrintableArea.Width,
e.PageSettings.PrintableArea.Height);
}


this.is exactly what i want to print and its not showing the BOTTOM
and RIGHT Margins
 
P

Peter Duniho

void pd_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawRectangle(Pens.Black,
e.PageSettings.PrintableArea.X, e.PageSettings.PrintableArea.Y,
e.PageSettings.PrintableArea.Width,
e.PageSettings.PrintableArea.Height);
}


this.is exactly what i want to print and its not showing the BOTTOM
and RIGHT Margins

What happens when you use FillRectangle instead? Or pass Width - 1 and
Height - 1 to DrawRectangle?

Note that, just as is the case in on-screen graphics, the coordinate
system is relative to a virtual grid and your rectangle's lines are drawn
just to the right and below of the intersection on this grid defined by
the coordinates of a given point. This means that the right and bottom of
your rectangle are being drawn just outside of the printable area. You
need to inset the right and bottom by a pixel to get the line into the
boundary of the printable area.

Pete
 
A

Abhay

Peter said:
What happens when you use FillRectangle instead? Or pass Width - 1 and
Height - 1 to DrawRectangle?

Note that, just as is the case in on-screen graphics, the coordinate
system is relative to a virtual grid and your rectangle's lines are drawn
just to the right and below of the intersection on this grid defined by
the coordinates of a given point. This means that the right and bottom of
your rectangle are being drawn just outside of the printable area. You
need to inset the right and bottom by a pixel to get the line into the
boundary of the printable area.

Pete

Thanks for your reply Peter but that is not the case according to me
i tried to reduce the pixel by 1 (around 15 infact) then to it dose'nt
show me the RIGHT and the BOTTOM margin

eg :- if the printable area is (Left,Top,Right,Bottom) :-

(16,16,818,1068) i have to reduce it to say (16,16,803,1053) then only
it prints out the rectangle correctly
 
P

Peter Duniho

Thanks for your reply Peter but that is not the case according to me

What I wrote certainly is true. Whether it's the cause of your problem,I
can't say. You might have other problems too. There are a variety of
issues that can cause graphics to not be drawn, whether to a printed page
or the screen, and you haven't offered enough details about your specific
problem for me to know for sure what it might be.

For example, what is the value of the OriginAtMargins property of the
PrintDocument? What are the actual margins in your second test? What
resolution is the printer set to? What print driver are you using?

But at the very least, you need to understand that if you try to draw a
rectangle that is the same exact width and height as what you believe to
be the valid area for drawing, the right and bottom edges will not be
drawn.

For the record, the following code, when I use it with the Office Document
Image Writer print driver (a convenient way to test printing code), it
prints a frame exactly the way you want (assuming the margins are within
the printable area, of course):

private void PrintPageHandler(object sender, PrintPageEventArgs ppea)
{
Rectangle rectFrame = new Rectangle(ppea.MarginBounds.Location,
new Size(ppea.MarginBounds.Width - 1, ppea.MarginBounds.Height
- 1));

using (Pen penFrame = new Pen(Color.Black, 0.0f))
{
ppea.Graphics.DrawRectangle(penFrame, rectFrame);
}

ppea.HasMorePages = false;
}

If you happen to run into a print driver that for some reason doesn't work
well with .NET and somehow having the Graphics units set to 100ths of an
inch cause some sort of rounding error, you could do something like this:

private void PrintPageHandler(object sender, PrintPageEventArgs ppea)
{
float dpiX = ppea.PageSettings.PrinterResolution.X,
dpiY = ppea.PageSettings.PrinterResolution.Y;
RectangleF rcfFrame = new RectangleF(dpiX * ppea.MarginBounds..Left
/ 100.0f,
dpiY * ppea.MarginBounds.Top / 100.0f,
dpiX * ppea.MarginBounds.Width / 100.0f - 1,
dpiY * ppea.MarginBounds.Height / 100.0f - 1);

ppea.Graphics.PageUnit = GraphicsUnit.Pixel;

using (Pen penFrame = new Pen(Color.Black, 0.0f))
{
ppea.Graphics.DrawRectangle(penFrame, rcfFrame.Left,
rcfFrame.Top, rcfFrame.Width, rcfFrame.Height);
}

ppea.HasMorePages = false;
}

I tested both versions with the Office Document Image Writer, and they
both draw a complete rectangle, while not subtracting 1 from the width and
height results in the right and bottom edge of the rectangle not being
drawn.
i tried to reduce the pixel by 1 (around 15 infact) then to it dose'nt
show me the RIGHT and the BOTTOM margin

eg :- if the printable area is (Left,Top,Right,Bottom) :-

(16,16,818,1068) i have to reduce it to say (16,16,803,1053) then only
it prints out the rectangle correctly

The fact that the amount you have to reduce the rectangle is practically
the same as the width of your margins suggests to me that you have somehow
gotten a situation where the offset of the drawn rectangle isn't what you
expect it to be. You could test this by drawing to a location left and
above what you think is the correct place (for example, use the rectangle
(0,0,818,1068) instead). In any case, I've offered about as much as I can
think of that would be useful. If that doesn't help you fix the problem,
I'm sorry for not being able to help.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top