all groups > dotnet drawing api > june 2005 >
dotnet drawing api :
How to print in full page without margins
I set the DefaultPageSettings.Margins.Left/Right/Top/Bottom to 0, it doesn't seem to work for the Left margin for two printers (HP and Brother).
Most printers can't print all the way to the physical margins. You are undoubtedly seeing the physical offset for the hard margins for your page. You can PInvoke GetDeviceCaps to get the exact physical margins for the printer drawing on a page and you could then scale the drawing so that all your information will fit or just offset things if you don't need the actual whole page size. There are some printers that can print all the way to the margins for larger paper but they tend to be very expensive. Ron Allen [quoted text, click to view] "zhaounknown" <zhaounknown@discussions.microsoft.com> wrote in message news:AB95B24B-D809-4833-B4E7-AC6219F114D4@microsoft.com... >I set the DefaultPageSettings.Margins.Left/Right/Top/Bottom to 0, it >doesn't > seem to work for the Left margin for two printers (HP and Brother). > > Am I missing any other properties that should be set? Thanks a lot.
Hi Ron, I found a great set of codes and article that you post in the other forum for getting the printer's hard margin throught GetDeviceCaps. I was able to retrieve the correct hard margins through GetDeviceCaps but I am unable to tell the printer to use that hard margins to draw maximum printable area. I have the following code for setting the margins. IntPtr hDc = new IntPtr(); hDc = e.Graphics.GetHdc(); PrintUtil.GetHardMargins(hDc, ref leftHardMargin, ref topHardMargin, ref rightHardMargin, ref bottomHardMargin); e.Graphics.ReleaseHdc(hDc); leftMargin = Convert.ToInt32(leftHardMargin); topMargin = Convert.ToInt32(topHardMargin); rightMargin = Convert.ToInt32(rightHardMargin); bottomMargin = Convert.ToInt32(bottomHardMargin); this.DefaultPageSettings.Margins = new Margins(leftMargin,leftMargin,topMargin,topMargin); this.OriginAtMargins = true; Rectangle myRect = new Rectangle(leftMargin, topMargin, rightMargin, bottomMargin); e.Graphics.DrawRectangle(Pens.Black, myRect ); The printer cuts of the right side of the rectangle box. And the printer's left offeset seems to be wider than the printer's actual printable area which I tested with left and right margin set to zero. So I can see where the printer start printing on the page. I have tried so many ways of setting printers margins but none seems to work. Furthermore, my way of setting margins have different effects on different printers. Please help me figure out what is making this weird behavior. Thanks in advance [quoted text, click to view] "Ron Allen" wrote: > Most printers can't print all the way to the physical margins. You are > undoubtedly seeing the physical offset for the hard margins for your page. > You can PInvoke GetDeviceCaps to get the exact physical margins for the > printer drawing on a page and you could then scale the drawing so that all > your information will fit or just offset things if you don't need the actual > whole page size. > There are some printers that can print all the way to the margins for > larger paper but they tend to be very expensive. > > Ron Allen > "zhaounknown" <zhaounknown@discussions.microsoft.com> wrote in message > news:AB95B24B-D809-4833-B4E7-AC6219F114D4@microsoft.com... > >I set the DefaultPageSettings.Margins.Left/Right/Top/Bottom to 0, it > >doesn't > > seem to work for the Left margin for two printers (HP and Brother). > > > > Am I missing any other properties that should be set? Thanks a lot. > >
Judge, You may be seeing the way the rectangle is being drawn. Try drawing the rectangle with the left and bottom 1 less than the generated amounts. I've never tried the OriginAtMargins offset though. I just normally to a TranslateTransform by the negative of the left and top margins and then draw as normal. When I have output that may overflow the printer's hard margins I test the scaling necessary to fit into the drawn area and do a scale on the drawing as well. You might want to try doing the TranslateTransform on the Graphics to see how that works as well. Ron Allen [quoted text, click to view] "Judge" <Judge@discussions.microsoft.com> wrote in message news:F5E99071-EE20-4D6D-9A08-D8CD08A656DD@microsoft.com... > Hi Ron, > I found a great set of codes and article that you post in the other forum > for getting the printer's hard margin throught GetDeviceCaps. > > I was able to retrieve the correct hard margins through GetDeviceCaps but > I > am unable to tell the printer to use that hard margins to draw maximum > printable area. I have the following code for setting the margins. > > IntPtr hDc = new IntPtr(); > hDc = e.Graphics.GetHdc(); > PrintUtil.GetHardMargins(hDc, ref leftHardMargin, ref topHardMargin, ref > rightHardMargin, ref bottomHardMargin); > e.Graphics.ReleaseHdc(hDc); > > > > leftMargin = Convert.ToInt32(leftHardMargin); > topMargin = Convert.ToInt32(topHardMargin); > rightMargin = Convert.ToInt32(rightHardMargin); > bottomMargin = Convert.ToInt32(bottomHardMargin); > > this.DefaultPageSettings.Margins = new > Margins(leftMargin,leftMargin,topMargin,topMargin); > > this.OriginAtMargins = true; > Rectangle myRect = new Rectangle(leftMargin, topMargin, rightMargin, > bottomMargin); > > > e.Graphics.DrawRectangle(Pens.Black, myRect ); > > The printer cuts of the right side of the rectangle box. And the printer's > left offeset seems to be wider than the printer's actual printable area > which > I tested with left and right margin set to zero. So I can see where the > printer start printing on the page. > > I have tried so many ways of setting printers margins but none seems to > work. > > Furthermore, my way of setting margins have different effects on different > printers. > > Please help me figure out what is making this weird behavior. > > Thanks in advance > > > "Ron Allen" wrote: > >> Most printers can't print all the way to the physical margins. You >> are >> undoubtedly seeing the physical offset for the hard margins for your >> page. >> You can PInvoke GetDeviceCaps to get the exact physical margins for the >> printer drawing on a page and you could then scale the drawing so that >> all >> your information will fit or just offset things if you don't need the >> actual >> whole page size. >> There are some printers that can print all the way to the margins for >> larger paper but they tend to be very expensive. >> >> Ron Allen >> "zhaounknown" <zhaounknown@discussions.microsoft.com> wrote in message >> news:AB95B24B-D809-4833-B4E7-AC6219F114D4@microsoft.com... >> >I set the DefaultPageSettings.Margins.Left/Right/Top/Bottom to 0, it >> >doesn't >> > seem to work for the Left margin for two printers (HP and Brother). >> > >> > Am I missing any other properties that should be set? Thanks a lot. >> >> >>
how big is the difference between the size you get from code and the actual printed one? [quoted text, click to view] Judge wrote: > Hi Ron, > I found a great set of codes and article that you post in the other forum > for getting the printer's hard margin throught GetDeviceCaps. > > I was able to retrieve the correct hard margins through GetDeviceCaps but I > am unable to tell the printer to use that hard margins to draw maximum > printable area. I have the following code for setting the margins. > > IntPtr hDc = new IntPtr(); > hDc = e.Graphics.GetHdc(); > PrintUtil.GetHardMargins(hDc, ref leftHardMargin, ref topHardMargin, ref > rightHardMargin, ref bottomHardMargin); > e.Graphics.ReleaseHdc(hDc); > > > > leftMargin = Convert.ToInt32(leftHardMargin); > topMargin = Convert.ToInt32(topHardMargin); > rightMargin = Convert.ToInt32(rightHardMargin); > bottomMargin = Convert.ToInt32(bottomHardMargin); > > this.DefaultPageSettings.Margins = new > Margins(leftMargin,leftMargin,topMargin,topMargin); > > this.OriginAtMargins = true; > Rectangle myRect = new Rectangle(leftMargin, topMargin, rightMargin, > bottomMargin); > > > e.Graphics.DrawRectangle(Pens.Black, myRect ); > > The printer cuts of the right side of the rectangle box. And the printer's > left offeset seems to be wider than the printer's actual printable area which > I tested with left and right margin set to zero. So I can see where the > printer start printing on the page. > > I have tried so many ways of setting printers margins but none seems to work. > > Furthermore, my way of setting margins have different effects on different > printers. > > Please help me figure out what is making this weird behavior. > > Thanks in advance > > > "Ron Allen" wrote: > > >> Most printers can't print all the way to the physical margins. You are >>undoubtedly seeing the physical offset for the hard margins for your page. >>You can PInvoke GetDeviceCaps to get the exact physical margins for the >>printer drawing on a page and you could then scale the drawing so that all >>your information will fit or just offset things if you don't need the actual >>whole page size. >> There are some printers that can print all the way to the margins for >>larger paper but they tend to be very expensive. >> >>Ron Allen >>"zhaounknown" <zhaounknown@discussions.microsoft.com> wrote in message >>news:AB95B24B-D809-4833-B4E7-AC6219F114D4@microsoft.com... >> >>>I set the DefaultPageSettings.Margins.Left/Right/Top/Bottom to 0, it >>>doesn't >>>seem to work for the Left margin for two printers (HP and Brother). >>> >>>Am I missing any other properties that should be set? Thanks a lot. >> >>
Hi Ron, I tried that and it still not working. What happens is that the hard margin returns .25 the printpreview shows exactly how the whole rectangle is drawn around the hard margin. i.e., .25 from the left, .25 from the right etc. Its nicely drawn on the printpreview. But somehow when I do actual printing the printer adds .25 to the left of the hard margin. i.e. it starts drawing at ..50 double the size of the original hard margin. To fix that I set the origins at margin to true. Since the graphics object return now is located within the margin I start drawing the rectangle from x:0, y:0 position. This fix the actual printing but the printpreview now shows me it started drawing from the 0, 0 position. No hard margin is taken into acount. So I tested with different printer and found out that different printer has different result again. Back to same old problem. The rectangle may sifted or cut off on the right side etc. Have you or anyone ever tried to print the rectangle of the hardmargin (printable area). If so could you share information how this can be achieved. I am using .net 1.1 and C# Thanks Ron. Your inputs are greatly appreciated. Ron, is it ok for me to do email exchange with you? If any one from Microsoft reading this (hoping they visit forums), I think its about time they provide a solution to this (if they have any) in their MSDN or publish an article on how to do this exactly. So many people have encounter problem with .net printing API behaving so differently on different computer across different forums. This solution should offer technique for printing the rectangle of a printable area hard margin that works with different printer. Judge [quoted text, click to view] "Ron Allen" wrote: > Judge, > You may be seeing the way the rectangle is being drawn. Try drawing the > rectangle with the left and bottom 1 less than the generated amounts. I've > never tried the OriginAtMargins offset though. I just normally to a > TranslateTransform by the negative of the left and top margins and then draw > as normal. When I have output that may overflow the printer's hard margins > I test the scaling necessary to fit into the drawn area and do a scale on > the drawing as well. > You might want to try doing the TranslateTransform on the Graphics to > see how that works as well. > > Ron Allen > "Judge" <Judge@discussions.microsoft.com> wrote in message > news:F5E99071-EE20-4D6D-9A08-D8CD08A656DD@microsoft.com... > > Hi Ron, > > I found a great set of codes and article that you post in the other forum > > for getting the printer's hard margin throught GetDeviceCaps. > > > > I was able to retrieve the correct hard margins through GetDeviceCaps but > > I > > am unable to tell the printer to use that hard margins to draw maximum > > printable area. I have the following code for setting the margins. > > > > IntPtr hDc = new IntPtr(); > > hDc = e.Graphics.GetHdc(); > > PrintUtil.GetHardMargins(hDc, ref leftHardMargin, ref topHardMargin, ref > > rightHardMargin, ref bottomHardMargin); > > e.Graphics.ReleaseHdc(hDc); > > > > > > > > leftMargin = Convert.ToInt32(leftHardMargin); > > topMargin = Convert.ToInt32(topHardMargin); > > rightMargin = Convert.ToInt32(rightHardMargin); > > bottomMargin = Convert.ToInt32(bottomHardMargin); > > > > this.DefaultPageSettings.Margins = new > > Margins(leftMargin,leftMargin,topMargin,topMargin); > > > > this.OriginAtMargins = true; > > Rectangle myRect = new Rectangle(leftMargin, topMargin, rightMargin, > > bottomMargin); > > > > > > e.Graphics.DrawRectangle(Pens.Black, myRect ); > > > > The printer cuts of the right side of the rectangle box. And the printer's > > left offeset seems to be wider than the printer's actual printable area > > which > > I tested with left and right margin set to zero. So I can see where the > > printer start printing on the page. > > > > I have tried so many ways of setting printers margins but none seems to > > work. > > > > Furthermore, my way of setting margins have different effects on different > > printers. > > > > Please help me figure out what is making this weird behavior. > > > > Thanks in advance > > > > > > "Ron Allen" wrote: > > > >> Most printers can't print all the way to the physical margins. You > >> are > >> undoubtedly seeing the physical offset for the hard margins for your > >> page. > >> You can PInvoke GetDeviceCaps to get the exact physical margins for the > >> printer drawing on a page and you could then scale the drawing so that > >> all > >> your information will fit or just offset things if you don't need the > >> actual > >> whole page size. > >> There are some printers that can print all the way to the margins for > >> larger paper but they tend to be very expensive. > >> > >> Ron Allen > >> "zhaounknown" <zhaounknown@discussions.microsoft.com> wrote in message > >> news:AB95B24B-D809-4833-B4E7-AC6219F114D4@microsoft.com... > >> >I set the DefaultPageSettings.Margins.Left/Right/Top/Bottom to 0, it > >> >doesn't > >> > seem to work for the Left margin for two printers (HP and Brother). > >> > > >> > Am I missing any other properties that should be set? Thanks a lot. > >> > >> > >> > >
SharpCoderMP. Depends on the printer. It returns correct but the printer won't obey that margin. Thats the main problem coz it behave differently on different printers. [quoted text, click to view] "SharpCoderMP" wrote: > how big is the difference between the size you get from code and the > actual printed one? > > Judge wrote: > > Hi Ron, > > I found a great set of codes and article that you post in the other forum > > for getting the printer's hard margin throught GetDeviceCaps. > > > > I was able to retrieve the correct hard margins through GetDeviceCaps but I > > am unable to tell the printer to use that hard margins to draw maximum > > printable area. I have the following code for setting the margins. > > > > IntPtr hDc = new IntPtr(); > > hDc = e.Graphics.GetHdc(); > > PrintUtil.GetHardMargins(hDc, ref leftHardMargin, ref topHardMargin, ref > > rightHardMargin, ref bottomHardMargin); > > e.Graphics.ReleaseHdc(hDc); > > > > > > > > leftMargin = Convert.ToInt32(leftHardMargin); > > topMargin = Convert.ToInt32(topHardMargin); > > rightMargin = Convert.ToInt32(rightHardMargin); > > bottomMargin = Convert.ToInt32(bottomHardMargin); > > > > this.DefaultPageSettings.Margins = new > > Margins(leftMargin,leftMargin,topMargin,topMargin); > > > > this.OriginAtMargins = true; > > Rectangle myRect = new Rectangle(leftMargin, topMargin, rightMargin, > > bottomMargin); > > > > > > e.Graphics.DrawRectangle(Pens.Black, myRect ); > > > > The printer cuts of the right side of the rectangle box. And the printer's > > left offeset seems to be wider than the printer's actual printable area which > > I tested with left and right margin set to zero. So I can see where the > > printer start printing on the page. > > > > I have tried so many ways of setting printers margins but none seems to work. > > > > Furthermore, my way of setting margins have different effects on different > > printers. > > > > Please help me figure out what is making this weird behavior. > > > > Thanks in advance > > > > > > "Ron Allen" wrote: > > > > > >> Most printers can't print all the way to the physical margins. You are > >>undoubtedly seeing the physical offset for the hard margins for your page. > >>You can PInvoke GetDeviceCaps to get the exact physical margins for the > >>printer drawing on a page and you could then scale the drawing so that all > >>your information will fit or just offset things if you don't need the actual > >>whole page size. > >> There are some printers that can print all the way to the margins for > >>larger paper but they tend to be very expensive. > >> > >>Ron Allen > >>"zhaounknown" <zhaounknown@discussions.microsoft.com> wrote in message > >>news:AB95B24B-D809-4833-B4E7-AC6219F114D4@microsoft.com... > >> > >>>I set the DefaultPageSettings.Margins.Left/Right/Top/Bottom to 0, it > >>>doesn't > >>>seem to work for the Left margin for two printers (HP and Brother). > >>> > >>>Am I missing any other properties that should be set? Thanks a lot. > >> > >> > >>
Judge, What you want to do is only apply the hard margin offsets when printing to the printer but not do this when in PrintPreview. I usually check the VisibleClipBounds to see if it is smaller than the page and apply hard margin offets if this is true. Otherwise I don't do a translate. Note that I don't draw using OriginAtMargins = true, I just use the actual page coordinates after taking account of the hard margins. A sample from one of my working reports is: -------------------------------------------------------------------------------------------------------- IntPtr hDc = ev.Graphics.GetHdc(); ev.Graphics.ReleaseHdc(hDc); // don't tie the hDC up any longer than required float left = 0.0f, right = 0.0f, top = 0.0f, bottom = 0.0f; PrintUtils.GetHardMargins(hDc.ToInt32(), ref left, ref top, ref right, ref bottom); if (ev.Graphics.VisibleClipBounds.Height < 1100.0f) // the displayed page height is for printer { ev.Graphics.TranslateTransform(-left, -top); // adjust to take hard margins into account } else { m_color = true; // printing to screen -- use color and set a TextRenderingHint ev.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; } -------------------------------------------------------------------------------------------------------------- After the TranslateTransform I check to see if the margins are within the desired page width and height (Note that GetHardMargins on a PrintPreview returns large #s for right and bottom) and then call Graphics.Transform.Scale to get the document to fit within the printed page. I think that you will be better off using the TranslateTransform rather than the OriginAtMargins to do the drawing. Also note that since your drawing is specifying left/top of the point being drawn if you draw all the way to the margins on the right/bottom your actual line pixels will be just outside the drawing area. You will need to subtract your line width from the right/bottom of the rectangle to get the whole thing shown. Ron Allen [quoted text, click to view] "Judge" <Judge@discussions.microsoft.com> wrote in message news:1C1B65FC-6B87-45A9-AFFD-FCB12F56564C@microsoft.com... > Hi Ron, > I tried that and it still not working. What happens is that the hard > margin > returns .25 the printpreview shows exactly how the whole rectangle is > drawn > around the hard margin. i.e., .25 from the left, .25 from the right etc. > Its > nicely drawn on the printpreview. But somehow when I do actual printing > the > printer adds .25 to the left of the hard margin. i.e. it starts drawing at > .50 double the size of the original hard margin. > > To fix that I set the origins at margin to true. Since the graphics object > return now is located within the margin I start drawing the rectangle > from > x:0, y:0 position. This fix the actual printing but the printpreview now > shows me it started drawing from the 0, 0 position. No hard margin is > taken > into acount. > > So I tested with different printer and found out that different printer > has > different result again. Back to same old problem. The rectangle may sifted > or > cut off on the right side etc. > > Have you or anyone ever tried to print the rectangle of the hardmargin > (printable area). If so could you share information how this can be > achieved. > > I am using .net 1.1 and C# > > Thanks Ron. Your inputs are greatly appreciated. > > Ron, is it ok for me to do email exchange with you? > > If any one from Microsoft reading this (hoping they visit forums), I think > its about time they provide a solution to this (if they have any) in their > MSDN or publish an article on how to do this exactly. So many people have > encounter problem with .net printing API behaving so differently on > different > computer across different forums. This solution should offer technique for > printing the rectangle of a printable area hard margin that works with > different printer. > > Judge > > "Ron Allen" wrote: > >> Judge, >> You may be seeing the way the rectangle is being drawn. Try drawing >> the >> rectangle with the left and bottom 1 less than the generated amounts. >> I've >> never tried the OriginAtMargins offset though. I just normally to a >> TranslateTransform by the negative of the left and top margins and then >> draw >> as normal. When I have output that may overflow the printer's hard >> margins >> I test the scaling necessary to fit into the drawn area and do a scale on >> the drawing as well. >> You might want to try doing the TranslateTransform on the Graphics to >> see how that works as well. >> >> Ron Allen >> "Judge" <Judge@discussions.microsoft.com> wrote in message >> news:F5E99071-EE20-4D6D-9A08-D8CD08A656DD@microsoft.com... >> > Hi Ron, >> > I found a great set of codes and article that you post in the other >> > forum >> > for getting the printer's hard margin throught GetDeviceCaps. >> > >> > I was able to retrieve the correct hard margins through GetDeviceCaps >> > but >> > I >> > am unable to tell the printer to use that hard margins to draw maximum >> > printable area. I have the following code for setting the margins. >> > >> > IntPtr hDc = new IntPtr(); >> > hDc = e.Graphics.GetHdc(); >> > PrintUtil.GetHardMargins(hDc, ref leftHardMargin, ref topHardMargin, >> > ref >> > rightHardMargin, ref bottomHardMargin); >> > e.Graphics.ReleaseHdc(hDc); >> > >> > >> > >> > leftMargin = Convert.ToInt32(leftHardMargin); >> > topMargin = Convert.ToInt32(topHardMargin); >> > rightMargin = Convert.ToInt32(rightHardMargin); >> > bottomMargin = Convert.ToInt32(bottomHardMargin); >> > >> > this.DefaultPageSettings.Margins = new >> > Margins(leftMargin,leftMargin,topMargin,topMargin); >> > >> > this.OriginAtMargins = true; >> > Rectangle myRect = new Rectangle(leftMargin, topMargin, rightMargin, >> > bottomMargin); >> > >> > >> > e.Graphics.DrawRectangle(Pens.Black, myRect ); >> > >> > The printer cuts of the right side of the rectangle box. And the >> > printer's >> > left offeset seems to be wider than the printer's actual printable area >> > which >> > I tested with left and right margin set to zero. So I can see where the >> > printer start printing on the page. >> > >> > I have tried so many ways of setting printers margins but none seems to >> > work. >> > >> > Furthermore, my way of setting margins have different effects on >> > different >> > printers. >> > >> > Please help me figure out what is making this weird behavior. >> > >> > Thanks in advance >> > >> > >> > "Ron Allen" wrote: >> > >> >> Most printers can't print all the way to the physical margins. >> >> You >> >> are >> >> undoubtedly seeing the physical offset for the hard margins for your >> >> page.
Thanks Ron, I am trying it right now and will post the result here how it goes. Thanks, [quoted text, click to view] "Ron Allen" wrote: > Judge, > What you want to do is only apply the hard margin offsets when printing > to the printer but not do this when in PrintPreview. > I usually check the VisibleClipBounds to see if it is smaller than the > page and apply hard margin offets if this is true. Otherwise I don't do a > translate. Note that I don't draw using OriginAtMargins = true, I just use > the actual page coordinates after taking account of the hard margins. A > sample from one of my working reports is: > -------------------------------------------------------------------------------------------------------- > IntPtr hDc = ev.Graphics.GetHdc(); > ev.Graphics.ReleaseHdc(hDc); // don't tie the hDC up any longer than > required > float left = 0.0f, right = 0.0f, top = 0.0f, bottom = 0.0f; > PrintUtils.GetHardMargins(hDc.ToInt32(), ref left, ref top, ref right, > ref bottom); > if (ev.Graphics.VisibleClipBounds.Height < 1100.0f) // the displayed > page height is for printer > { > ev.Graphics.TranslateTransform(-left, -top); // adjust to take hard > margins into account > } > else > { > m_color = true; // printing to screen -- use color and set a > TextRenderingHint > ev.Graphics.TextRenderingHint = > System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; > } > -------------------------------------------------------------------------------------------------------------- > After the TranslateTransform I check to see if the margins are within the > desired page width and height (Note that GetHardMargins on a PrintPreview > returns large #s for right and bottom) and then call > Graphics.Transform.Scale to get the document to fit within the printed page. > I think that you will be better off using the TranslateTransform rather > than the OriginAtMargins to do the drawing. Also note that since your > drawing is specifying left/top of the point being drawn if you draw all the > way to the margins on the right/bottom your actual line pixels will be just > outside the drawing area. You will need to subtract your line width from > the right/bottom of the rectangle to get the whole thing shown. > > Ron Allen > > "Judge" <Judge@discussions.microsoft.com> wrote in message > news:1C1B65FC-6B87-45A9-AFFD-FCB12F56564C@microsoft.com... > > Hi Ron, > > I tried that and it still not working. What happens is that the hard > > margin > > returns .25 the printpreview shows exactly how the whole rectangle is > > drawn > > around the hard margin. i.e., .25 from the left, .25 from the right etc. > > Its > > nicely drawn on the printpreview. But somehow when I do actual printing > > the > > printer adds .25 to the left of the hard margin. i.e. it starts drawing at > > .50 double the size of the original hard margin. > > > > To fix that I set the origins at margin to true. Since the graphics object > > return now is located within the margin I start drawing the rectangle > > from > > x:0, y:0 position. This fix the actual printing but the printpreview now > > shows me it started drawing from the 0, 0 position. No hard margin is > > taken > > into acount. > > > > So I tested with different printer and found out that different printer > > has > > different result again. Back to same old problem. The rectangle may sifted > > or > > cut off on the right side etc. > > > > Have you or anyone ever tried to print the rectangle of the hardmargin > > (printable area). If so could you share information how this can be > > achieved. > > > > I am using .net 1.1 and C# > > > > Thanks Ron. Your inputs are greatly appreciated. > > > > Ron, is it ok for me to do email exchange with you? > > > > If any one from Microsoft reading this (hoping they visit forums), I think > > its about time they provide a solution to this (if they have any) in their > > MSDN or publish an article on how to do this exactly. So many people have > > encounter problem with .net printing API behaving so differently on > > different > > computer across different forums. This solution should offer technique for > > printing the rectangle of a printable area hard margin that works with > > different printer. > > > > Judge > > > > "Ron Allen" wrote: > > > >> Judge, > >> You may be seeing the way the rectangle is being drawn. Try drawing > >> the > >> rectangle with the left and bottom 1 less than the generated amounts. > >> I've > >> never tried the OriginAtMargins offset though. I just normally to a > >> TranslateTransform by the negative of the left and top margins and then > >> draw > >> as normal. When I have output that may overflow the printer's hard > >> margins > >> I test the scaling necessary to fit into the drawn area and do a scale on > >> the drawing as well. > >> You might want to try doing the TranslateTransform on the Graphics to > >> see how that works as well. > >> > >> Ron Allen > >> "Judge" <Judge@discussions.microsoft.com> wrote in message > >> news:F5E99071-EE20-4D6D-9A08-D8CD08A656DD@microsoft.com... > >> > Hi Ron, > >> > I found a great set of codes and article that you post in the other > >> > forum > >> > for getting the printer's hard margin throught GetDeviceCaps. > >> > > >> > I was able to retrieve the correct hard margins through GetDeviceCaps > >> > but > >> > I > >> > am unable to tell the printer to use that hard margins to draw maximum > >> > printable area. I have the following code for setting the margins. > >> > > >> > IntPtr hDc = new IntPtr(); > >> > hDc = e.Graphics.GetHdc(); > >> > PrintUtil.GetHardMargins(hDc, ref leftHardMargin, ref topHardMargin, > >> > ref > >> > rightHardMargin, ref bottomHardMargin); > >> > e.Graphics.ReleaseHdc(hDc); > >> > > >> > > >> > > >> > leftMargin = Convert.ToInt32(leftHardMargin); > >> > topMargin = Convert.ToInt32(topHardMargin); > >> > rightMargin = Convert.ToInt32(rightHardMargin); > >> > bottomMargin = Convert.ToInt32(bottomHardMargin); > >> > > >> > this.DefaultPageSettings.Margins = new > >> > Margins(leftMargin,leftMargin,topMargin,topMargin); > >> > > >> > this.OriginAtMargins = true; > >> > Rectangle myRect = new Rectangle(leftMargin, topMargin, rightMargin, > >> > bottomMargin); > >> > > >> > > >> > e.Graphics.DrawRectangle(Pens.Black, myRect ); > >> > > >> > The printer cuts of the right side of the rectangle box. And the > >> > printer's > >> > left offeset seems to be wider than the printer's actual printable area > >> > which > >> > I tested with left and right margin set to zero. So I can see where the > >> > printer start printing on the page. > >> > > >> > I have tried so many ways of setting printers margins but none seems to > >> > work. > >> >
Ron, I tried what you have suggested here. But I still have the same problem. The right side gets cut off in the actual printing and the the left margin is twice the width of what the actual hard margin is. However in print preview everthing looks find. I notice that for actual printing, the .NET graphics some how locates the 2 times the hardleftmargin for x, 2 times the topHardmargin for y. This happens when you create a rectangle new Ractangle(leftHardMargin, topHardMargin, width, height); Whatever I specified in the rectangle's lefthardmargin is added to the DefaultPageSettings.Margins.Left. Similarly, whatever I specified in the rectangle's lefthardmargin is added to the DefaultPageSettings.Margins.Top. In my case the actual printing becomes twice the size because I set the same lefthardmargin to the DefaultPageSettings.Margins.Left and my Rectangle.Left. Similarly, for the top setting. However, PrintPreview shows correctly. i.e. what we thought would show. But the actual printing somehow mess that up and keep adding the Rectangle's left and top with DefaultPageSettings left and top. Thats why so many people have trouble finding a solution. Some say if I print the rectangle at 0,0 position the printout fine but print preview becomes a problem. So to work around with the problem I did the following: note: m_Preview is set before I show preview dialog. this.DefaultPageSettings.Margins= new Margins(leftHardMargin,(e.PageBounds.Width - rightHardMargin),topHardMargin, (e.PageBounds.Height - bottomHardMargin)); if (!this.m_Preview) { // if this is not print preview if (topHardMargin < 25) // i would like at least o.25 top margin { topHardMargin = 25 - topMargin; // since the weird api keeps adding up the top margin with the DefaultPageSettings.Margins.Top } if (leftHardMargin >= 25) { leftHardMargin = 25 - leftMargin; // i would like at least o.25 top margin // since the weird api keeps adding up the left margin with the DefaultPageSettings.Margins.Top } } else { if (topHardMargin < 25) { topHardMargin = 25; } } m_Preview = false; // reset to default. Rectangle checkRect = new Rectangle(leftMargin, topMargin, e.PageSettings.Bounds.Width - (25 * 2) - 1, e.PageSettings.Bounds.Height - (25 * 2) - 1); // i would like my margins to be at least .25 inch After that I tested on two different computers and printers and it works like a charm. I tested the above code on Lexmark and samsaung. They both printed and previewed fine. I am not sure if my solution is a correct solution but this is the only way I could work around with that .NET printing api. It sucks big time but have to put up with it anyways. If I can spend more time I would have rewrote it in java. Thanks for you input. Judge [quoted text, click to view] "Ron Allen" wrote: > Judge, > What you want to do is only apply the hard margin offsets when printing > to the printer but not do this when in PrintPreview. > I usually check the VisibleClipBounds to see if it is smaller than the > page and apply hard margin offets if this is true. Otherwise I don't do a > translate. Note that I don't draw using OriginAtMargins = true, I just use > the actual page coordinates after taking account of the hard margins. A > sample from one of my working reports is: > -------------------------------------------------------------------------------------------------------- > IntPtr hDc = ev.Graphics.GetHdc(); > ev.Graphics.ReleaseHdc(hDc); // don't tie the hDC up any longer than > required > float left = 0.0f, right = 0.0f, top = 0.0f, bottom = 0.0f; > PrintUtils.GetHardMargins(hDc.ToInt32(), ref left, ref top, ref right, > ref bottom); > if (ev.Graphics.VisibleClipBounds.Height < 1100.0f) // the displayed > page height is for printer > { > ev.Graphics.TranslateTransform(-left, -top); // adjust to take hard > margins into account > } > else > { > m_color = true; // printing to screen -- use color and set a > TextRenderingHint > ev.Graphics.TextRenderingHint = > System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; > } > -------------------------------------------------------------------------------------------------------------- > After the TranslateTransform I check to see if the margins are within the > desired page width and height (Note that GetHardMargins on a PrintPreview > returns large #s for right and bottom) and then call > Graphics.Transform.Scale to get the document to fit within the printed page. > I think that you will be better off using the TranslateTransform rather > than the OriginAtMargins to do the drawing. Also note that since your > drawing is specifying left/top of the point being drawn if you draw all the > way to the margins on the right/bottom your actual line pixels will be just > outside the drawing area. You will need to subtract your line width from > the right/bottom of the rectangle to get the whole thing shown. > > Ron Allen > > "Judge" <Judge@discussions.microsoft.com> wrote in message > news:1C1B65FC-6B87-45A9-AFFD-FCB12F56564C@microsoft.com... > > Hi Ron, > > I tried that and it still not working. What happens is that the hard > > margin > > returns .25 the printpreview shows exactly how the whole rectangle is > > drawn > > around the hard margin. i.e., .25 from the left, .25 from the right etc. > > Its > > nicely drawn on the printpreview. But somehow when I do actual printing > > the > > printer adds .25 to the left of the hard margin. i.e. it starts drawing at > > .50 double the size of the original hard margin. > > > > To fix that I set the origins at margin to true. Since the graphics object > > return now is located within the margin I start drawing the rectangle > > from > > x:0, y:0 position. This fix the actual printing but the printpreview now > > shows me it started drawing from the 0, 0 position. No hard margin is > > taken > > into acount. > > > > So I tested with different printer and found out that different printer > > has > > different result again. Back to same old problem. The rectangle may sifted > > or > > cut off on the right side etc. > > > > Have you or anyone ever tried to print the rectangle of the hardmargin > > (printable area). If so could you share information how this can be > > achieved. > > > > I am using .net 1.1 and C# > > > > Thanks Ron. Your inputs are greatly appreciated. > > > > Ron, is it ok for me to do email exchange with you? > > > > If any one from Microsoft reading this (hoping they visit forums), I think > > its about time they provide a solution to this (if they have any) in their > > MSDN or publish an article on how to do this exactly. So many people have
Typo correction on previous post: Similarly, whatever I specified in the rectangle's tophardmargin is added to the DefaultPageSettings.Margins.Top. [quoted text, click to view] "Judge" wrote: > Ron, > > I tried what you have suggested here. But I still have the same problem. The > right side gets cut off in the actual printing and the the left margin is > twice the width of what the actual hard margin is. However in print preview > everthing looks find. > > I notice that for actual printing, the .NET graphics some how locates the 2 > times the hardleftmargin for x, 2 times the topHardmargin for y. > > This happens when you create a rectangle > new Ractangle(leftHardMargin, topHardMargin, width, height); > > Whatever I specified in the rectangle's lefthardmargin is added to the > DefaultPageSettings.Margins.Left. Similarly, whatever I specified in the > rectangle's lefthardmargin is added to the DefaultPageSettings.Margins.Top. > > In my case the actual printing becomes twice the size because I set the same > lefthardmargin to the > DefaultPageSettings.Margins.Left and my Rectangle.Left. Similarly, for the > top setting. > > However, PrintPreview shows correctly. i.e. what we thought would show. But > the actual printing somehow mess that up and keep adding the Rectangle's left > and top with DefaultPageSettings left and top. Thats why so many people have > trouble finding a solution. Some say if I print the rectangle at 0,0 position > the printout fine but print preview becomes a problem. > > So to work around with the problem I did the following: > note: m_Preview is set before I show preview dialog. > > this.DefaultPageSettings.Margins= new > Margins(leftHardMargin,(e.PageBounds.Width - rightHardMargin),topHardMargin, > (e.PageBounds.Height - bottomHardMargin)); > > if (!this.m_Preview) > { // if this is not print preview > if (topHardMargin < 25) // i would like at least o.25 top margin > { > topHardMargin = 25 - topMargin; > // since the weird api keeps adding up the top margin with the > DefaultPageSettings.Margins.Top > } > if (leftHardMargin >= 25) > { > leftHardMargin = 25 - leftMargin; // i would like at least > o.25 top margin > // since the weird api keeps adding up the left margin with the > DefaultPageSettings.Margins.Top > > } > } > else { > if (topHardMargin < 25) > { > topHardMargin = 25; > > } > } > m_Preview = false; // reset to default. > > Rectangle checkRect = new Rectangle(leftMargin, topMargin, > e.PageSettings.Bounds.Width - (25 * 2) - 1, e.PageSettings.Bounds.Height - > (25 * 2) - 1); // i would like my margins to be at least .25 inch > > After that I tested on two different computers and printers and it works > like a charm. I tested the above code on Lexmark and samsaung. They both > printed and previewed fine. > > I am not sure if my solution is a correct solution but this is the only way > I could work around with that .NET printing api. It sucks big time but have > to put up with it anyways. If I can spend more time I would have rewrote it > in java. > > Thanks for you input. > Judge > > > "Ron Allen" wrote: > > > Judge, > > What you want to do is only apply the hard margin offsets when printing > > to the printer but not do this when in PrintPreview. > > I usually check the VisibleClipBounds to see if it is smaller than the > > page and apply hard margin offets if this is true. Otherwise I don't do a > > translate. Note that I don't draw using OriginAtMargins = true, I just use > > the actual page coordinates after taking account of the hard margins. A > > sample from one of my working reports is: > > -------------------------------------------------------------------------------------------------------- > > IntPtr hDc = ev.Graphics.GetHdc(); > > ev.Graphics.ReleaseHdc(hDc); // don't tie the hDC up any longer than > > required > > float left = 0.0f, right = 0.0f, top = 0.0f, bottom = 0.0f; > > PrintUtils.GetHardMargins(hDc.ToInt32(), ref left, ref top, ref right, > > ref bottom); > > if (ev.Graphics.VisibleClipBounds.Height < 1100.0f) // the displayed > > page height is for printer > > { > > ev.Graphics.TranslateTransform(-left, -top); // adjust to take hard > > margins into account > > } > > else > > { > > m_color = true; // printing to screen -- use color and set a > > TextRenderingHint > > ev.Graphics.TextRenderingHint = > > System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; > > } > > -------------------------------------------------------------------------------------------------------------- > > After the TranslateTransform I check to see if the margins are within the > > desired page width and height (Note that GetHardMargins on a PrintPreview > > returns large #s for right and bottom) and then call > > Graphics.Transform.Scale to get the document to fit within the printed page. > > I think that you will be better off using the TranslateTransform rather > > than the OriginAtMargins to do the drawing. Also note that since your > > drawing is specifying left/top of the point being drawn if you draw all the > > way to the margins on the right/bottom your actual line pixels will be just > > outside the drawing area. You will need to subtract your line width from > > the right/bottom of the rectangle to get the whole thing shown. > > > > Ron Allen > > > > "Judge" <Judge@discussions.microsoft.com> wrote in message > > news:1C1B65FC-6B87-45A9-AFFD-FCB12F56564C@microsoft.com... > > > Hi Ron, > > > I tried that and it still not working. What happens is that the hard > > > margin > > > returns .25 the printpreview shows exactly how the whole rectangle is > > > drawn > > > around the hard margin. i.e., .25 from the left, .25 from the right etc. > > > Its > > > nicely drawn on the printpreview. But somehow when I do actual printing > > > the > > > printer adds .25 to the left of the hard margin. i.e. it starts drawing at > > > .50 double the size of the original hard margin. > > > > > > To fix that I set the origins at margin to true. Since the graphics object > > > return now is located within the margin I start drawing the rectangle > > > from > > > x:0, y:0 position. This fix the actual printing but the printpreview now > > > shows me it started drawing from the 0, 0 position. No hard margin is > > > taken > > > into acount. > > > > > > So I tested with different printer and found out that different printer > > > has > > > different result again. Back to same old problem. The rectangle may sifted > > > or > > > cut off on the right side etc. > > > > > > Have you or anyone ever tried to print the rectangle of the hardmargin
Don't see what you're looking for? Try a search.
|
|
|