473,545 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Printing Document

I have a 8.5 x 11 landscape document with about 1/4 inch of space on the
left and right where there is no print.
The document displays perfect in print preview, but when I print it, about
1/2 inch on the right is not printed (leaving about 3/4 inch empty margin on
the right side of the page). The left side starts about 1/4 inch in and
prints just like I expect it to.

I tried setting the margins to 0 and OriginAtMargins = True. The print began
about an 1/8" at the left but still stoped about 3/4" on the right.

I know I can change the document so that there is no print for the last 3/4
inch on the right, but that seems like coding around the problem instead of
getting it right.

Is there anything I can do besides coding the width of the report shorter?
Nov 21 '05 #1
7 2469
This sounds like you have hit the 'hard margins' on the printer. Most
printers won't print over a whole page. Some older ink jets have wide
margin areas.
You can PInvoke GetDeviceCaps to find out the actual horizontal/vertical
hard margins on the printer. You could then scale your print area to fit all
your printed items within the actual display area of the printer. If you
search for GetHardMargins you should find some code to get printer margins
that Mr. Wagner converted to VB.
In framework 2.0 you can get this information without calling
GetDeviceCaps and use that to compensate.

Ron Allen
"DazedAndConfus ed" <Ac********@yah oo.com> wrote in message
news:Os******** ******@TK2MSFTN GP14.phx.gbl...
I have a 8.5 x 11 landscape document with about 1/4 inch of space on the
left and right where there is no print.
The document displays perfect in print preview, but when I print it, about
1/2 inch on the right is not printed (leaving about 3/4 inch empty margin
on the right side of the page). The left side starts about 1/4 inch in and
prints just like I expect it to.

I tried setting the margins to 0 and OriginAtMargins = True. The print
began about an 1/8" at the left but still stoped about 3/4" on the right.

I know I can change the document so that there is no print for the last
3/4 inch on the right, but that seems like coding around the problem
instead of getting it right.

Is there anything I can do besides coding the width of the report shorter?

Nov 21 '05 #2
Thank you for pointing me in the right direction.
"Ron Allen" <rallen@_nospam _src-us.com> wrote in message
news:uZ******** ******@TK2MSFTN GP12.phx.gbl...
This sounds like you have hit the 'hard margins' on the printer. Most
printers won't print over a whole page. Some older ink jets have wide
margin areas.
You can PInvoke GetDeviceCaps to find out the actual
horizontal/vertical hard margins on the printer. You could then scale your
print area to fit all your printed items within the actual display area of
the printer. If you search for GetHardMargins you should find some code
to get printer margins that Mr. Wagner converted to VB.
In framework 2.0 you can get this information without calling
GetDeviceCaps and use that to compensate.

Ron Allen
"DazedAndConfus ed" <Ac********@yah oo.com> wrote in message
news:Os******** ******@TK2MSFTN GP14.phx.gbl...
I have a 8.5 x 11 landscape document with about 1/4 inch of space on the
left and right where there is no print.
The document displays perfect in print preview, but when I print it,
about 1/2 inch on the right is not printed (leaving about 3/4 inch empty
margin on the right side of the page). The left side starts about 1/4
inch in and prints just like I expect it to.

I tried setting the margins to 0 and OriginAtMargins = True. The print
began about an 1/8" at the left but still stoped about 3/4" on the right.

I know I can change the document so that there is no print for the last
3/4 inch on the right, but that seems like coding around the problem
instead of getting it right.

Is there anything I can do besides coding the width of the report
shorter?


Nov 21 '05 #3
Is there a way to scale programmaticall y? A way to specify that the document
should print 95% of size. Or do you have to code alternate font sizes, x, y,
etc.?

Any good examples you can poit me to?
"Ron Allen" <rallen@_nospam _src-us.com> wrote in message
news:uZ******** ******@TK2MSFTN GP12.phx.gbl...
This sounds like you have hit the 'hard margins' on the printer. Most
printers won't print over a whole page. Some older ink jets have wide
margin areas.
You can PInvoke GetDeviceCaps to find out the actual
horizontal/vertical hard margins on the printer. You could then scale your
print area to fit all your printed items within the actual display area of
the printer. If you search for GetHardMargins you should find some code
to get printer margins that Mr. Wagner converted to VB.
In framework 2.0 you can get this information without calling
GetDeviceCaps and use that to compensate.

Ron Allen
"DazedAndConfus ed" <Ac********@yah oo.com> wrote in message
news:Os******** ******@TK2MSFTN GP14.phx.gbl...
I have a 8.5 x 11 landscape document with about 1/4 inch of space on the
left and right where there is no print.
The document displays perfect in print preview, but when I print it,
about 1/2 inch on the right is not printed (leaving about 3/4 inch empty
margin on the right side of the page). The left side starts about 1/4
inch in and prints just like I expect it to.

I tried setting the margins to 0 and OriginAtMargins = True. The print
began about an 1/8" at the left but still stoped about 3/4" on the right.

I know I can change the document so that there is no print for the last
3/4 inch on the right, but that seems like coding around the problem
instead of getting it right.

Is there anything I can do besides coding the width of the report
shorter?


Nov 21 '05 #4
Just call the Graphics.Transf orm.Scale method on the Graphics being used
and specify the x and y scaling factors and everything will be done for you
as far as shrinking the document.

Ron Allen
"DazedAndConfus ed" <Ac********@yah oo.com> wrote in message
news:eU******** ******@tk2msftn gp13.phx.gbl...
Is there a way to scale programmaticall y? A way to specify that the
document should print 95% of size. Or do you have to code alternate font
sizes, x, y, etc.?

Any good examples you can poit me to?
"Ron Allen" <rallen@_nospam _src-us.com> wrote in message
news:uZ******** ******@TK2MSFTN GP12.phx.gbl...
This sounds like you have hit the 'hard margins' on the printer. Most
printers won't print over a whole page. Some older ink jets have wide
margin areas.
You can PInvoke GetDeviceCaps to find out the actual
horizontal/vertical hard margins on the printer. You could then scale
your print area to fit all your printed items within the actual display
area of the printer. If you search for GetHardMargins you should find
some code to get printer margins that Mr. Wagner converted to VB.
In framework 2.0 you can get this information without calling
GetDeviceCaps and use that to compensate.

Ron Allen
"DazedAndConfus ed" <Ac********@yah oo.com> wrote in message
news:Os******** ******@TK2MSFTN GP14.phx.gbl...
I have a 8.5 x 11 landscape document with about 1/4 inch of space on the
left and right where there is no print.
The document displays perfect in print preview, but when I print it,
about 1/2 inch on the right is not printed (leaving about 3/4 inch empty
margin on the right side of the page). The left side starts about 1/4
inch in and prints just like I expect it to.

I tried setting the margins to 0 and OriginAtMargins = True. The print
began about an 1/8" at the left but still stoped about 3/4" on the
right.

I know I can change the document so that there is no print for the last
3/4 inch on the right, but that seems like coding around the problem
instead of getting it right.

Is there anything I can do besides coding the width of the report
shorter?



Nov 21 '05 #5
Thank you.

I was able to scale the document using:

e.Graphics.Scal eTransform(0.95 F, 0.95F)

In order to scale using Transform.Scale method I had to:

Dim myMatrix As New System.Drawing. Drawing2D.Matri x
myMatrix.Scale( 0.95F, 0.95F)
e.Graphics.Tran sform = myMatrix

Is this the correct way of using Transform.Scale or am I missing the concept
somewhere?

Also I would really like to be able to use Mr. Wagners class for getting the
hard margins but keep getting a System.InvalidO perationExcepti on - The
object is currently in use elsewhere and just can't figure it out on my own.

In stepping through the debugger I actually see the
System.InvalidO perationExcepti on error first comming up in the Locals window
on the Graphics object at:
Dim hDC As IntPtr = Graphics.GetHdc ()

hDC is populated (with the printer handle I assume)

But no ErrorException is thrown until m.Left = CInt(ox * 100 /
Graphics.DpiX); then I get the "The object is currently in use elsewhere"
error.

Please help me to understand??!!!

Here is the code:

Imports System.Drawing. Printing

''' <summary>
''' Provides information about a device.
''' </summary>
Public Class Device
Private Declare Function GetDeviceCaps Lib "gdi32.dll" ( _
ByVal hdc As IntPtr, _
ByVal nIndex As Int32 _
) As Int32

Private Const PHYSICALOFFSETX As Int32 = 112 ' In device units.
Private Const PHYSICALOFFSETY As Int32 = 113 ' In device units.
Private Const HORZRES As Int32 = 8 ' In pixels/dots.
Private Const VERTRES As Int32 = 10 ' In pixels/dots.

''' <summary>
''' Gets a device's physical margins.
''' </summary>
''' <param name="Graphics" >The device's <c>Graphics</c> object.</param>
''' <returns>
''' The device's physical margins in 0.001 inch units.
''' </returns>
Public Shared Function GetPhysicalMarg ins( _
ByVal Graphics As Graphics _
) As Margins
Dim hDC As IntPtr
Try
hDC = Graphics.GetHdc () <========== Locals window shows
Graphics as System.InvalidO perationExcepti on, but no error is thrown

Catch ex As InvalidOperatio nException

End Try
Dim m As Margins = GetPhysicalMarg ins(hDC, Graphics)
Graphics.Releas eHdc(hDC)
Return m
End Function

''' <summary>
''' Gets a device's physical margins.
''' </summary>
''' <param name="hDC">Hand le to the device context.</param>
''' <returns>
''' The device's physical margins in 0.001 inch units.
''' </returns>
Public Shared Function GetPhysicalMarg ins( _
ByVal hDC As IntPtr _
) As Margins
Dim g As Graphics = Graphics.FromHd c(hDC)
Dim m As Margins = GetPhysicalMarg ins(hDC, g)
g.Dispose()
Return m
End Function

''' <summary>
''' Gets a device's physical margins.
''' </summary>
''' <param name="hDC">Hand le to the device context.</param>
''' <param name="Graphics" >The device's <c>Graphics</c> object.</param>
''' <returns>
''' The device's physical margins in 0.001 inch units.
''' </returns>
Private Shared Function GetPhysicalMarg ins( _
ByVal hDC As IntPtr, _
ByVal Graphics As Graphics _
) As Margins
Dim m As New Margins
Dim ox As Int32 = GetDeviceCaps(h DC, PHYSICALOFFSETX )
Dim oy As Int32 = GetDeviceCaps(h DC, PHYSICALOFFSETY )
With m
.Left = CInt(ox * 100 / Graphics.DpiX) <==========
System.InvalidO perationExcepti on - The object is currently in use
elsewhere - ERROR IS THROWN HERE
.Top = CInt(oy * 100 / Graphics.DpiY)
.Right = _
CInt( _
(ox + GetDeviceCaps(h DC, HORZRES)) * 100 / _
Graphics.DpiX _
)
.Bottom = _
CInt( _
(oy + GetDeviceCaps(h DC, VERTRES)) * 100 / _
Graphics.DpiY _
)
End With
Return m
End Function
End Class

Public Class Form1
Inherits System.Windows. Forms.Form
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim p As New PrinterSettings
For Each PrinterName As String In PrinterSettings .InstalledPrint ers
p.PrinterName = PrinterName
Dim g As Graphics = p.CreateMeasure mentGraphics()
MsgBox( _
PrinterName & ControlChars.Ne wLine & _
Device.GetPhysi calMargins(g).T oString() & "[0.01 inch]" _
)
g.Dispose()
Next PrinterName
End Sub
End Class

"Ron Allen" <rallen@_nospam _src-us.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Just call the Graphics.Transf orm.Scale method on the Graphics being
used and specify the x and y scaling factors and everything will be done
for you as far as shrinking the document.

Ron Allen
"DazedAndConfus ed" <Ac********@yah oo.com> wrote in message
news:eU******** ******@tk2msftn gp13.phx.gbl...
Is there a way to scale programmaticall y? A way to specify that the
document should print 95% of size. Or do you have to code alternate font
sizes, x, y, etc.?

Any good examples you can poit me to?
"Ron Allen" <rallen@_nospam _src-us.com> wrote in message
news:uZ******** ******@TK2MSFTN GP12.phx.gbl...
This sounds like you have hit the 'hard margins' on the printer.
Most printers won't print over a whole page. Some older ink jets have
wide margin areas.
You can PInvoke GetDeviceCaps to find out the actual
horizontal/vertical hard margins on the printer. You could then scale
your print area to fit all your printed items within the actual display
area of the printer. If you search for GetHardMargins you should find
some code to get printer margins that Mr. Wagner converted to VB.
In framework 2.0 you can get this information without calling
GetDeviceCaps and use that to compensate.

Ron Allen
"DazedAndConfus ed" <Ac********@yah oo.com> wrote in message
news:Os******** ******@TK2MSFTN GP14.phx.gbl...
I have a 8.5 x 11 landscape document with about 1/4 inch of space on the
left and right where there is no print.
The document displays perfect in print preview, but when I print it,
about 1/2 inch on the right is not printed (leaving about 3/4 inch
empty margin on the right side of the page). The left side starts about
1/4 inch in and prints just like I expect it to.

I tried setting the margins to 0 and OriginAtMargins = True. The print
began about an 1/8" at the left but still stoped about 3/4" on the
right.

I know I can change the document so that there is no print for the last
3/4 inch on the right, but that seems like coding around the problem
instead of getting it right.

Is there anything I can do besides coding the width of the report
shorter?



Nov 21 '05 #6
You need to get the DPIX returned by GetDeviceCaps not by the Graphics.
The HORZRES and VERTRES as defined here are the call values to use to
get the corresponding resolutions as you are doing after your division.
Just get the x and y resolutions first which gives you the # of pixels
across/down of the printer and divide these into the returned size which is
returned in mm. I convert the mm to inches before dividing to get the
actual DPI values
The original I wrote in (C#) is below Herfred did the conversion to VB
quite some time ago.
You don't need to pass the Graphics to anything to compute the hard
margins. All you need is the hDc from the Graphics. Note that I usually
just release the hDc as soon as I get it as it doesn't normally change
during the code I call afterwards.

Also you can just scale using Graphics.Transf orm.Scale(0.95, 0.95) no
need to setup a matrix.

--------------------original
C# -----------------------------------------------
// the other two constants
public static short HORZSIZE = 4; // Horizontal size in
millimeters
public static short VERTSIZE = 6; // Vertical size in millimeters

/// <summary>
/// Return the device 'hard' margins for the passed in
/// Device Context handle. Return data in 1/100ths inch
/// </summary>
/// <param name="hDc">Inpu t handle</param>
/// <param name="left">out put left margin in 1/100ths inch</param>
/// <param name="top">outp ut top margin in 1/100ths inch</param>
/// <param name="right">ou tput right margin in 1/100ths inch</param>
/// <param name="bottom">o utput bottom margin in 1/100ths inch</param>
public static void GetHardMargins( int hDc, ref float left, ref float top,
ref float right, ref float bottom)
{
float offx = Convert.ToSingl e(GetDeviceCaps (hDc, PHYSICALOFFSETX ));
float offy = Convert.ToSingl e(GetDeviceCaps (hDc, PHYSICALOFFSETY ));;
float resx = Convert.ToSingl e(GetDeviceCaps (hDc, HORZRES));
float resy = Convert.ToSingl e(GetDeviceCaps (hDc, VERTRES));
float hsz = Convert.ToSingl e(GetDeviceCaps (hDc, HORZSIZE))/25.4f; //
screen width in inches
float vsz = Convert.ToSingl e(GetDeviceCaps (hDc, VERTSIZE))/25.4f; //
screen height in inches
float ppix = resx/hsz;
float ppiy = resy/vsz;
left = (offx/ppix) * 100.0f;
top = (offy/ppix) * 100.0f;
bottom = top + (vsz * 100.0f);
right = left + (hsz * 100.0f);
}

// code that call this
IntPtr hDc = ev.Graphics.Get Hdc();
ev.Graphics.Rel easeHdc(hDc);
float left = 0.0f, right = 0.0f, top = 0.0f, bottom = 0.0f;
PrintUtils.GetH ardMargins(hDc. ToInt32(), ref left, ref top, ref right,
ref bottom);

This works even though I release the device context immediately. Don't
assume that the DPIX/Y returned from the Graphics will be identical to these
as they are rounded (although close enough for most).
--------------------------end C#
code------------------------------------------
"DazedAndConfus ed" <Ac********@yah oo.com> wrote in message
news:e1******** ******@tk2msftn gp13.phx.gbl...
Thank you.

I was able to scale the document using:

e.Graphics.Scal eTransform(0.95 F, 0.95F)

In order to scale using Transform.Scale method I had to:

Dim myMatrix As New System.Drawing. Drawing2D.Matri x
myMatrix.Scale( 0.95F, 0.95F)
e.Graphics.Tran sform = myMatrix

Is this the correct way of using Transform.Scale or am I missing the
concept somewhere?

Also I would really like to be able to use Mr. Wagners class for getting
the hard margins but keep getting a System.InvalidO perationExcepti on - The
object is currently in use elsewhere and just can't figure it out on my
own.

In stepping through the debugger I actually see the
System.InvalidO perationExcepti on error first comming up in the Locals
window on the Graphics object at:
Dim hDC As IntPtr = Graphics.GetHdc ()

hDC is populated (with the printer handle I assume)

But no ErrorException is thrown until m.Left = CInt(ox * 100 /
Graphics.DpiX); then I get the "The object is currently in use elsewhere"
error.

Please help me to understand??!!!

Here is the code:

Imports System.Drawing. Printing

''' <summary>
''' Provides information about a device.
''' </summary>
Public Class Device
Private Declare Function GetDeviceCaps Lib "gdi32.dll" ( _
ByVal hdc As IntPtr, _
ByVal nIndex As Int32 _
) As Int32

Private Const PHYSICALOFFSETX As Int32 = 112 ' In device units.
Private Const PHYSICALOFFSETY As Int32 = 113 ' In device units.
Private Const HORZRES As Int32 = 8 ' In pixels/dots.
Private Const VERTRES As Int32 = 10 ' In pixels/dots.

''' <summary>
''' Gets a device's physical margins.
''' </summary>
''' <param name="Graphics" >The device's <c>Graphics</c> object.</param>
''' <returns>
''' The device's physical margins in 0.001 inch units.
''' </returns>
Public Shared Function GetPhysicalMarg ins( _
ByVal Graphics As Graphics _
) As Margins
Dim hDC As IntPtr
Try
hDC = Graphics.GetHdc () <========== Locals window shows
Graphics as System.InvalidO perationExcepti on, but no error is thrown

Catch ex As InvalidOperatio nException

End Try
Dim m As Margins = GetPhysicalMarg ins(hDC, Graphics)
Graphics.Releas eHdc(hDC)
Return m
End Function

''' <summary>
''' Gets a device's physical margins.
''' </summary>
''' <param name="hDC">Hand le to the device context.</param>
''' <returns>
''' The device's physical margins in 0.001 inch units.
''' </returns>
Public Shared Function GetPhysicalMarg ins( _
ByVal hDC As IntPtr _
) As Margins
Dim g As Graphics = Graphics.FromHd c(hDC)
Dim m As Margins = GetPhysicalMarg ins(hDC, g)
g.Dispose()
Return m
End Function

''' <summary>
''' Gets a device's physical margins.
''' </summary>
''' <param name="hDC">Hand le to the device context.</param>
''' <param name="Graphics" >The device's <c>Graphics</c> object.</param>
''' <returns>
''' The device's physical margins in 0.001 inch units.
''' </returns>
Private Shared Function GetPhysicalMarg ins( _
ByVal hDC As IntPtr, _
ByVal Graphics As Graphics _
) As Margins
Dim m As New Margins
Dim ox As Int32 = GetDeviceCaps(h DC, PHYSICALOFFSETX )
Dim oy As Int32 = GetDeviceCaps(h DC, PHYSICALOFFSETY )
With m
.Left = CInt(ox * 100 / Graphics.DpiX) <==========
System.InvalidO perationExcepti on - The object is currently in use
elsewhere - ERROR IS THROWN HERE
.Top = CInt(oy * 100 / Graphics.DpiY)
.Right = _
CInt( _
(ox + GetDeviceCaps(h DC, HORZRES)) * 100 / _
Graphics.DpiX _
)
.Bottom = _
CInt( _
(oy + GetDeviceCaps(h DC, VERTRES)) * 100 / _
Graphics.DpiY _
)
End With
Return m
End Function
End Class

Public Class Form1
Inherits System.Windows. Forms.Form
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim p As New PrinterSettings
For Each PrinterName As String In PrinterSettings .InstalledPrint ers
p.PrinterName = PrinterName
Dim g As Graphics = p.CreateMeasure mentGraphics()
MsgBox( _
PrinterName & ControlChars.Ne wLine & _
Device.GetPhysi calMargins(g).T oString() & "[0.01 inch]" _
)
g.Dispose()
Next PrinterName
End Sub
End Class

"Ron Allen" <rallen@_nospam _src-us.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Just call the Graphics.Transf orm.Scale method on the Graphics being
used and specify the x and y scaling factors and everything will be done
for you as far as shrinking the document.

Ron Allen
"DazedAndConfus ed" <Ac********@yah oo.com> wrote in message
news:eU******** ******@tk2msftn gp13.phx.gbl...
Is there a way to scale programmaticall y? A way to specify that the
document should print 95% of size. Or do you have to code alternate font
sizes, x, y, etc.?

Any good examples you can poit me to?
"Ron Allen" <rallen@_nospam _src-us.com> wrote in message
news:uZ******** ******@TK2MSFTN GP12.phx.gbl...
This sounds like you have hit the 'hard margins' on the printer.
Most printers won't print over a whole page. Some older ink jets have
wide margin areas.
You can PInvoke GetDeviceCaps to find out the actual
horizontal/vertical hard margins on the printer. You could then scale
your print area to fit all your printed items within the actual display
area of the printer. If you search for GetHardMargins you should
find some code to get printer margins that Mr. Wagner converted to VB.
In framework 2.0 you can get this information without calling
GetDeviceCaps and use that to compensate.

Ron Allen
"DazedAndConfus ed" <Ac********@yah oo.com> wrote in message
news:Os******** ******@TK2MSFTN GP14.phx.gbl...
>I have a 8.5 x 11 landscape document with about 1/4 inch of space on
>the left and right where there is no print.
> The document displays perfect in print preview, but when I print it,
> about 1/2 inch on the right is not printed (leaving about 3/4 inch
> empty margin on the right side of the page). The left side starts
> about 1/4 inch in and prints just like I expect it to.
>
> I tried setting the margins to 0 and OriginAtMargins = True. The print
> began about an 1/8" at the left but still stoped about 3/4" on the
> right.
>
> I know I can change the document so that there is no print for the
> last 3/4 inch on the right, but that seems like coding around the
> problem instead of getting it right.
>
> Is there anything I can do besides coding the width of the report
> shorter?
>



Nov 21 '05 #7
Thank for your help and especially your patients.

"Ron Allen" <rallen@_nospam _src-us.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
You need to get the DPIX returned by GetDeviceCaps not by the Graphics.
The HORZRES and VERTRES as defined here are the call values to use to
get the corresponding resolutions as you are doing after your division.
Just get the x and y resolutions first which gives you the # of pixels
across/down of the printer and divide these into the returned size which
is returned in mm. I convert the mm to inches before dividing to get the
actual DPI values
The original I wrote in (C#) is below Herfred did the conversion to VB
quite some time ago.
You don't need to pass the Graphics to anything to compute the hard
margins. All you need is the hDc from the Graphics. Note that I usually
just release the hDc as soon as I get it as it doesn't normally change
during the code I call afterwards.

Also you can just scale using Graphics.Transf orm.Scale(0.95, 0.95) no
need to setup a matrix.

--------------------original
C# -----------------------------------------------
// the other two constants
public static short HORZSIZE = 4; // Horizontal size in
millimeters
public static short VERTSIZE = 6; // Vertical size in
millimeters

/// <summary>
/// Return the device 'hard' margins for the passed in
/// Device Context handle. Return data in 1/100ths inch
/// </summary>
/// <param name="hDc">Inpu t handle</param>
/// <param name="left">out put left margin in 1/100ths inch</param>
/// <param name="top">outp ut top margin in 1/100ths inch</param>
/// <param name="right">ou tput right margin in 1/100ths inch</param>
/// <param name="bottom">o utput bottom margin in 1/100ths inch</param>
public static void GetHardMargins( int hDc, ref float left, ref float top,
ref float right, ref float bottom)
{
float offx = Convert.ToSingl e(GetDeviceCaps (hDc, PHYSICALOFFSETX ));
float offy = Convert.ToSingl e(GetDeviceCaps (hDc, PHYSICALOFFSETY ));;
float resx = Convert.ToSingl e(GetDeviceCaps (hDc, HORZRES));
float resy = Convert.ToSingl e(GetDeviceCaps (hDc, VERTRES));
float hsz = Convert.ToSingl e(GetDeviceCaps (hDc, HORZSIZE))/25.4f; //
screen width in inches
float vsz = Convert.ToSingl e(GetDeviceCaps (hDc, VERTSIZE))/25.4f; //
screen height in inches
float ppix = resx/hsz;
float ppiy = resy/vsz;
left = (offx/ppix) * 100.0f;
top = (offy/ppix) * 100.0f;
bottom = top + (vsz * 100.0f);
right = left + (hsz * 100.0f);
}

// code that call this
IntPtr hDc = ev.Graphics.Get Hdc();
ev.Graphics.Rel easeHdc(hDc);
float left = 0.0f, right = 0.0f, top = 0.0f, bottom = 0.0f;
PrintUtils.GetH ardMargins(hDc. ToInt32(), ref left, ref top, ref right,
ref bottom);

This works even though I release the device context immediately. Don't
assume that the DPIX/Y returned from the Graphics will be identical to
these as they are rounded (although close enough for most).
--------------------------end C#
code------------------------------------------
"DazedAndConfus ed" <Ac********@yah oo.com> wrote in message
news:e1******** ******@tk2msftn gp13.phx.gbl...
Thank you.

I was able to scale the document using:

e.Graphics.Scal eTransform(0.95 F, 0.95F)

In order to scale using Transform.Scale method I had to:

Dim myMatrix As New System.Drawing. Drawing2D.Matri x
myMatrix.Scale( 0.95F, 0.95F)
e.Graphics.Tran sform = myMatrix

Is this the correct way of using Transform.Scale or am I missing the
concept somewhere?

Also I would really like to be able to use Mr. Wagners class for getting
the hard margins but keep getting a System.InvalidO perationExcepti on -
The object is currently in use elsewhere and just can't figure it out on
my own.

In stepping through the debugger I actually see the
System.InvalidO perationExcepti on error first comming up in the Locals
window on the Graphics object at:
Dim hDC As IntPtr = Graphics.GetHdc ()

hDC is populated (with the printer handle I assume)

But no ErrorException is thrown until m.Left = CInt(ox * 100 /
Graphics.DpiX); then I get the "The object is currently in use elsewhere"
error.

Please help me to understand??!!!

Here is the code:

Imports System.Drawing. Printing

''' <summary>
''' Provides information about a device.
''' </summary>
Public Class Device
Private Declare Function GetDeviceCaps Lib "gdi32.dll" ( _
ByVal hdc As IntPtr, _
ByVal nIndex As Int32 _
) As Int32

Private Const PHYSICALOFFSETX As Int32 = 112 ' In device units.
Private Const PHYSICALOFFSETY As Int32 = 113 ' In device units.
Private Const HORZRES As Int32 = 8 ' In pixels/dots.
Private Const VERTRES As Int32 = 10 ' In pixels/dots.

''' <summary>
''' Gets a device's physical margins.
''' </summary>
''' <param name="Graphics" >The device's <c>Graphics</c>
object.</param>
''' <returns>
''' The device's physical margins in 0.001 inch units.
''' </returns>
Public Shared Function GetPhysicalMarg ins( _
ByVal Graphics As Graphics _
) As Margins
Dim hDC As IntPtr
Try
hDC = Graphics.GetHdc () <========== Locals window shows
Graphics as System.InvalidO perationExcepti on, but no error is thrown

Catch ex As InvalidOperatio nException

End Try
Dim m As Margins = GetPhysicalMarg ins(hDC, Graphics)
Graphics.Releas eHdc(hDC)
Return m
End Function

''' <summary>
''' Gets a device's physical margins.
''' </summary>
''' <param name="hDC">Hand le to the device context.</param>
''' <returns>
''' The device's physical margins in 0.001 inch units.
''' </returns>
Public Shared Function GetPhysicalMarg ins( _
ByVal hDC As IntPtr _
) As Margins
Dim g As Graphics = Graphics.FromHd c(hDC)
Dim m As Margins = GetPhysicalMarg ins(hDC, g)
g.Dispose()
Return m
End Function

''' <summary>
''' Gets a device's physical margins.
''' </summary>
''' <param name="hDC">Hand le to the device context.</param>
''' <param name="Graphics" >The device's <c>Graphics</c>
object.</param>
''' <returns>
''' The device's physical margins in 0.001 inch units.
''' </returns>
Private Shared Function GetPhysicalMarg ins( _
ByVal hDC As IntPtr, _
ByVal Graphics As Graphics _
) As Margins
Dim m As New Margins
Dim ox As Int32 = GetDeviceCaps(h DC, PHYSICALOFFSETX )
Dim oy As Int32 = GetDeviceCaps(h DC, PHYSICALOFFSETY )
With m
.Left = CInt(ox * 100 / Graphics.DpiX) <==========
System.InvalidO perationExcepti on - The object is currently in use
elsewhere - ERROR IS THROWN HERE
.Top = CInt(oy * 100 / Graphics.DpiY)
.Right = _
CInt( _
(ox + GetDeviceCaps(h DC, HORZRES)) * 100 / _
Graphics.DpiX _
)
.Bottom = _
CInt( _
(oy + GetDeviceCaps(h DC, VERTRES)) * 100 / _
Graphics.DpiY _
)
End With
Return m
End Function
End Class

Public Class Form1
Inherits System.Windows. Forms.Form
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim p As New PrinterSettings
For Each PrinterName As String In PrinterSettings .InstalledPrint ers
p.PrinterName = PrinterName
Dim g As Graphics = p.CreateMeasure mentGraphics()
MsgBox( _
PrinterName & ControlChars.Ne wLine & _
Device.GetPhysi calMargins(g).T oString() & "[0.01 inch]" _
)
g.Dispose()
Next PrinterName
End Sub
End Class

"Ron Allen" <rallen@_nospam _src-us.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Just call the Graphics.Transf orm.Scale method on the Graphics being
used and specify the x and y scaling factors and everything will be done
for you as far as shrinking the document.

Ron Allen
"DazedAndConfus ed" <Ac********@yah oo.com> wrote in message
news:eU******** ******@tk2msftn gp13.phx.gbl...
Is there a way to scale programmaticall y? A way to specify that the
document should print 95% of size. Or do you have to code alternate
font sizes, x, y, etc.?

Any good examples you can poit me to?
"Ron Allen" <rallen@_nospam _src-us.com> wrote in message
news:uZ******** ******@TK2MSFTN GP12.phx.gbl...
> This sounds like you have hit the 'hard margins' on the printer.
> Most printers won't print over a whole page. Some older ink jets have
> wide margin areas.
> You can PInvoke GetDeviceCaps to find out the actual
> horizontal/vertical hard margins on the printer. You could then scale
> your print area to fit all your printed items within the actual
> display area of the printer. If you search for GetHardMargins you
> should find some code to get printer margins that Mr. Wagner converted
> to VB.
> In framework 2.0 you can get this information without calling
> GetDeviceCaps and use that to compensate.
>
> Ron Allen
> "DazedAndConfus ed" <Ac********@yah oo.com> wrote in message
> news:Os******** ******@TK2MSFTN GP14.phx.gbl...
>>I have a 8.5 x 11 landscape document with about 1/4 inch of space on
>>the left and right where there is no print.
>> The document displays perfect in print preview, but when I print it,
>> about 1/2 inch on the right is not printed (leaving about 3/4 inch
>> empty margin on the right side of the page). The left side starts
>> about 1/4 inch in and prints just like I expect it to.
>>
>> I tried setting the margins to 0 and OriginAtMargins = True. The
>> print began about an 1/8" at the left but still stoped about 3/4" on
>> the right.
>>
>> I know I can change the document so that there is no print for the
>> last 3/4 inch on the right, but that seems like coding around the
>> problem instead of getting it right.
>>
>> Is there anything I can do besides coding the width of the report
>> shorter?
>>
>
>



Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3319
by: Stefania Scott | last post by:
I am trying to print a word document from Access. The code I've written works well in my computer but does not in the one were it is needed. Here the piece of code: 'doc path strObjectPath = "P:\2004worksheets\IIS_WS.doc" Set oWord = New Word.Application oWord.Documents.Add (strObjectPath) oWord.PrintOut
7
8419
by: ap | last post by:
Hi, It is a quick question... Is that impossible to print the page(Web Form .aspx file) in client side using GDI+ like PrintDocument pd = new PrintDocument(); pd.print(); As I know it is a server side printing... and is that possible to make it to client side printing or it must be printed using javascript??
4
1892
by: Al Jones | last post by:
Well, hopefully that title get someones attention. In this old gwbasic program I've converted I *have* sucessuflly converted it to print to a file and the output is acceptable. From there I have printPreview and (what is it) PrinDocument working. All of the references I've found (to include several books) show that method of printing ......
1
5690
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out of a Microsoft book, "Visual Basic,Net Step by Step" in Chapter 18. All but the bottom two subroutines will open a text file, and then allow me...
3
3702
by: John Peterson | last post by:
Hello all! I'm at my wits end trying to search for what I assumed to be a relatively straightforward task. I have a Web application written in C#, and I have a button on the form that I want to print the current contents of the browser without bringing up the print dialog. At first I thought it was a simple matter to have the button's...
1
2070
by: eskildb | last post by:
First, please be gently. I am fairly new to the programming world (1.5 years with some expermentation prior to). I have been working on a project that has to print HTML pages with graphics in a unattended automated fashion. I have a webbrowser that is created with code but not seen. I found the below code on the internet. It creates a...
1
2879
by: eskildb | last post by:
First, please be gently. I am fairly new to the programming world (1.5 years with some expermentation prior to). I have been working on a project that has to print HTML pages with graphics in a unattended automated fashion. I have a webbrowser that is created with code but not seen. I found the below code on the internet. It creates a...
8
2397
by: Rick Lederman | last post by:
I am using a PrintDocument and PrintDialog to print. The first time that I print it works, but when I try to print a second time without exiting the entire program I get an "InvalidPrinterException was unhandled" error. The error happens in the third line below. The details of the error are below my signature. Help? ...
2
8310
by: deepakfordotnet | last post by:
Hi, First of all let me confess that I could not get the solution to the same problem from an earlier post Printing :by Mr.Richard MSL (dated September 24th 2006) working. (Replied by Mr.Walter Wang - September 27th 2006) My problem is to Print a text from a RichTextBox with all options like - AllowMultiplePages enabled plus PrintPreview and...
18
11268
by: Brett | last post by:
I have an ASP.NET page that displays work orders in a GridView. In that GridView is a checkbox column. When the user clicks a "Print" button, I create a report, using the .NET Framework printing classes, for each of the checked rows in the GridView. This works fine in the Visual Studio 2005 development environment on localhost. But, when I...
0
7499
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7689
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
5359
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.