Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Drawing / December 2003

Tip: Looking for answers? Try searching our database.

Wrong alignment print printdocument

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Frederik Vanderhaegen - 29 Dec 2003 09:07 GMT
Hi,

Does anyone knows how to fix following problem:

I've created a printpreview with the printpreview control.
This looks perfect but the printresult is different from the preview.

I tested the program with a HP Deskjet 895CXI (win xp) and a HP Laserjet 4L
(win2k) and on those two printers the aligment is never the same as the
preview.

Any help is welcome.
Thanks in advance
Frederik
Ron Allen - 29 Dec 2003 13:47 GMT
Frederik,
   If I understand your problem correctly, it is due to the 'hard margins'
on the printers.  The actual drawing 0,0 point on the printers is at the
top/left hard margin point and for print preview it is at 0,0 on the
graphics.  You can PInvoke GetDeviceCaps at the start of drawing to get the
printer margins and then call Translate on the Graphics to compensate.  If
you like I have some C# code that gets the hard margins for a printer.
   You can usualy determine if you are drawing to a printer by checking to
see if VisibleClipBounds on your page is smaller than the page size.

Ron Allen
> Hi,
>
[quoted text clipped - 10 lines]
> Thanks in advance
> Frederik
Frederik Vanderhaegen - 29 Dec 2003 15:26 GMT
Hi,

I've tried your suggestion for the use of GetDeviceCaps but still isn't the
printresult the same as the preview.
Maybe I do something wrong, hopefully you can help me, I would appreciate
it.

This is how I use the function:
   IntPtr hdc = e.Graphics.GetHdc();
   int iMarginLeft=GetDeviceCaps(hdc,112);
   e.Graphics.ReleaseHdc(hdc);
  int iMarginRight=iMarginLeft;
  pDoc.DefaultPageSettings.Margins.Left=iMarginLeft;
  pDoc.DefaultPageSettings.Margins.Right=iMarginLeft;

where pDoc is my printdocument.

Thanks in advance

Frederik

> Frederik,
>     If I understand your problem correctly, it is due to the 'hard margins'
[quoted text clipped - 22 lines]
> > Thanks in advance
> > Frederik
Ron Allen - 30 Dec 2003 19:00 GMT
Fredrick,
   The call you are using returns the left margin in device pixels.  You
need to also get the HORZRES value of pixels in width and the HORZSIZE in
centimeters and convert all of this to pixels/inch x.
For example the following C# code gets all the margins in 100ths of an inch
(the default resolution).  Note that the margins may be assymetrical
especially in ink jet printers.
 public static short DRIVERVERSION = 0;     // Device driver version
 public static short TECHNOLOGY    = 2;     // Device classification
 public static short HORZSIZE      = 4;     // Horizontal size in
millimeters
 public static short VERTSIZE      = 6;     // Vertical size in millimeters
 public static short HORZRES       = 8;     // Horizontal width in pixels
 public static short VERTRES       = 10;    // Vertical height in pixels
 public static short BITSPIXEL     = 12;    // Number of bits per pixel
 public static short PLANES        = 14;    // Number of planes
 public static short NUMBRUSHES    = 16;    // Number of brushes the device
has
 public static short NUMPENS       = 18;    // Number of pens the device
has
 public static short NUMMARKERS    = 20;    // Number of markers the device
has
 public static short NUMFONTS      = 22;    // Number of fonts the device
has
 public static short NUMCOLORS     = 24;    // Number of colors the device
supports
 public static short PDEVICESIZE   = 26;    // Size required for device
descriptor
 public static short CURVECAPS     = 28;    // Curve capabilities
 public static short LINECAPS      = 30;    // Line capabilities
 public static short POLYGONALCAPS = 32;    // Polygonal capabilities
 public static short TEXTCAPS      = 34;    // Text capabilities
 public static short CLIPCAPS      = 36;    // Clipping capabilities
 public static short RASTERCAPS    = 38;    // Bitblt capabilities
 public static short ASPECTX       = 40;    // Length of the X leg
 public static short ASPECTY       = 42;    // Length of the Y leg
 public static short ASPECTXY      = 44;    // Length of the hypotenuse
 public static short PHYSICALWIDTH   = 110; // Physical Width in device
units
 public static short PHYSICALHEIGHT  = 111; // Physical Height in device
units
 public static short PHYSICALOFFSETX = 112; // Physical Printable Area x
margin
 public static short PHYSICALOFFSETY = 113; // Physical Printable Area y
margin
 public static short SCALINGFACTORX  = 114; // Scaling factor x
 public static short SCALINGFACTORY  = 115; // Scaling factor y
 /// <summary>
 /// Return the device 'hard' margins for the passed in
 /// Device Context handle.  Return data in 1/100ths inch
 /// </summary>
 /// <param name="hDc">Input handle</param>
 /// <param name="left">output left margin in 1/100ths inch</param>
 /// <param name="top">output top margin in 1/100ths inch</param>
 /// <param name="right">output right margin in 1/100ths inch</param>
 /// <param name="bottom">output 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.ToSingle(GetDeviceCaps(hDc, PHYSICALOFFSETX));
  float offy = Convert.ToSingle(GetDeviceCaps(hDc, PHYSICALOFFSETY));;
  float resx = Convert.ToSingle(GetDeviceCaps(hDc, HORZRES));
  float resy = Convert.ToSingle(GetDeviceCaps(hDc, VERTRES));
  float hsz = Convert.ToSingle(GetDeviceCaps(hDc, HORZSIZE))/25.4f; //
screen width in inches
  float vsz = Convert.ToSingle(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);
 }

After doing this you should do   a TranslateTransform(-left, -top); to the
approprate Graphics (in the PrintPage Event handler).  This will get your
print orgin set to the top/left of the actual paper.  Note that you won't be
able to print outside the hard margins of the printer though.

   This is different than the document margins settings of the document.
As a further refinement I usually check to see if the total print width and
height of the printer is less than the vertical and horizontal extents of my
document's page and scale the graphics accordingly if true.  This lets me
print on ink jet printers with larger physical margins without changing any
code.

Ron Allen
> Hi,
>
[quoted text clipped - 46 lines]
> > > Thanks in advance
> > > Frederik
James Westgate \(Crainiate\) - 29 Dec 2003 13:55 GMT
Hi frederick,

The advice I always give with printing issues is to make sure you are using
use .net framework 1.1 and have the latest printer driver installed. Run the
test on windows XP (if possible) and see if the behaviour still exists.

Also, make sure that the printer settings arent affecting the alignment?

James

> Hi,
>
[quoted text clipped - 10 lines]
> Thanks in advance
> Frederik

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.