Determine if Preview or Print() in OnPagePrint Event?

  • Thread starter Thread starter Craig Scheets
  • Start date Start date
C

Craig Scheets

I have writting an application that prints fine, but I have to do a
TranslateTransform on the graphics in the OnPagePrint event to get place
output at exact X,Y (the application prints checks and must be placed
accurately).

However, when I do a print preview if I do the same translateTransform part
of the graphics is often just off the edge of the preview. Is there a way
to determine inside the OnPagePrint event if it's just a preview or the real
print function? I want to supress the TranslateTransform() if it's only a
preview.

I know I could rig something up with a global bool and set it when I do the
printPreviewDialog but I print in a seperate thread so it could be ugly and
less than perfect. This also wouldn't take into account if they clicked the
print button from within the preview window. Is there a more elegant way?

The relevant code is as follows:
private void docCheck_PrintPage(object sender, PrintPageEventArgs e)

{

//Shift the final graphics based on the printer offsets

//each "mark" from calibration sheet entered in numX & numY are equal to
3/100"

float xInchOffset = (float) xOffset *3 /100;

float yInchOffset = (float) yOffset *3 /100;

//HERE IS WHERE I WANT TO DO THE "if (!PrintPreview)" CODE TO SKIP THE NEXT
LINE

e.Graphics.TranslateTransform((float)xOffset *3,(float)yOffset *3);


Thanks,
Craig
 
Craig,

I don't think there is a way to directly do this, but I think that you
might be able to perform some workarounds.

My first guess would be to get the graphics instance, and then get the
HDC for that graphics instance. Once you have that, you would pass it to
the GetDeviceCaps API function through the P/Invoke layer. I would try to
get the value for the TECHNOLOGY capability, and see if that returns a
printer (as opposed to a display).

If that doesn't work, I would check the PrintPageEventArgs passed to
you, specifically, the PageBounds and the PageSettings properties to see if
there is a noticable discrepancy.

Hope this helps.
 
I didn't think about GetDeviceCaps, good thought... I'll see what's there.

Normally I think you'd be right about the PrintPageEventArgs too, but there
aren't any discrepancies in PageBounds because we're using ThinPrint on our
servers where this will run which is a virtual print driver. It always
returns 850x1100 for paper size, margins, pagebounds, etc on Letter Size
because it's more/less a PDF print driver with a virtual 100% printable
area. Oh Well :(

Thanks!


Nicholas Paldino said:
Craig,

I don't think there is a way to directly do this, but I think that you
might be able to perform some workarounds.

My first guess would be to get the graphics instance, and then get the
HDC for that graphics instance. Once you have that, you would pass it to
the GetDeviceCaps API function through the P/Invoke layer. I would try to
get the value for the TECHNOLOGY capability, and see if that returns a
printer (as opposed to a display).

If that doesn't work, I would check the PrintPageEventArgs passed to
you, specifically, the PageBounds and the PageSettings properties to see
if there is a noticable discrepancy.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Craig Scheets said:
I have writting an application that prints fine, but I have to do a
TranslateTransform on the graphics in the OnPagePrint event to get place
output at exact X,Y (the application prints checks and must be placed
accurately).

However, when I do a print preview if I do the same translateTransform
part of the graphics is often just off the edge of the preview. Is there
a way to determine inside the OnPagePrint event if it's just a preview or
the real print function? I want to supress the TranslateTransform() if
it's only a preview.

I know I could rig something up with a global bool and set it when I do
the printPreviewDialog but I print in a seperate thread so it could be
ugly and less than perfect. This also wouldn't take into account if they
clicked the print button from within the preview window. Is there a more
elegant way?

The relevant code is as follows:
private void docCheck_PrintPage(object sender, PrintPageEventArgs e)

{

//Shift the final graphics based on the printer offsets

//each "mark" from calibration sheet entered in numX & numY are equal to
3/100"

float xInchOffset = (float) xOffset *3 /100;

float yInchOffset = (float) yOffset *3 /100;

//HERE IS WHERE I WANT TO DO THE "if (!PrintPreview)" CODE TO SKIP THE
NEXT LINE

e.Graphics.TranslateTransform((float)xOffset *3,(float)yOffset *3);


Thanks,
Craig
 
I know this is a very old post, but I had a similar necessity and this was Google's first result for my search. I think this should solve the problem...

In the body of your OnPrintPage event handler, add this check:

Code:
 if (((PrintDocument)sender).PrintController.IsPreview)
It wasn't too difficult to find, so I imagine the original poster was still using .NET 1.1 before this property was added in .NET 2.0.

P.S.: zombie post!
 
Back
Top