How to adjust Graphics for a Printer

Hi again,

I created a PrintPreview that was absolutely correct, and if the output was to print I created an adjustment to handle the printer's HardMargins.  Now the PrintPreview and printed output's margins match.  This is the code that I added to the PrintDocument.PrintPage eventhandler.  PrtDoc is my instance of PrintDocument.

Private Sub PrtDoc_PrintPage(ByVal sender As Object, ByVal e As _
     
System.Drawing.Printing.PrintPageEventArgs) _
      Handles
PrtDoc.PrintPage
  
'get the coordinates of the printable area inside the margins
   Dim PrtW As
Int32 = e.MarginBounds.Width
  
Dim PrtH As
Int32 = e.MarginBounds.Height
  
Dim PrtX As
Int32 = e.MarginBounds.X
  
Dim PrtY As
Int32 = e.MarginBounds.Y
  
If Not PrtDoc.PrintController.IsPreview
Then
     
'adjust coordinates for Printer HardMargins
      PrtX -= PrtDoc.DefaultPageSettings.HardMarginX
      PrtY -= PrtDoc.DefaultPageSettings.HardMarginY
      If
PrtDoc.DefaultPageSettings.Landscape _
        
And fromPrintPreview
Then
        
'adjust for landscape
         PrtY += 850 - e.PageBounds.Height
      End
If
   End
If

After the code gets the coordinates of the printable area, it adjusts the left and top margins (PrtX & PrtY) if the output is not a preview.  Since the printer shifts the image to the right and down, the code subtracts these values from the margins.  

 

A curious thing happens when I print in landscape.  It is especially noticeable if the page is smaller than the normal 8 1/2" x 11" sheet.  The narrow paper is loaded on the right side of the input paper tray.  If the PaperSize and Landscape have been set in the PrinterDialog, the printer prints the output correctly on the paper.  However, if the PaperSize and Landscape have been set programmatically and either viewed through the PrintPreview dialog or printed directly, the printer prints the output incorrectly on the left side of the printer.  For my HP printer the code fixes this errant behavior.  A Boolean variable, fromPrintPreview, is set to True if the PageSize and landscape have been set programmatically. The adjustment for landscape is made by adjusting the top margin (PrtY).  I initally thought I shoud be adjusting the left margin, but this is Landscape top margin is on the left. The adjustment adds 850 and subtracts the page height.  I guess that this would also occur with narrow sheets of paper printed in Portrait.

 

I know that this solution works for my HP printer, but will not work for all printers.  Some printers load narrow paper in the middle of the input paper tray.   I unsuccessfully looked for some variable analogous to the HardMargins for the narrow paper problem.

 

 

If someone has a solution for this problem, I would greatly appreciate hearing from you, thanks.




Answer this question

How to adjust Graphics for a Printer

  • Vlince

    Hi,

    It might be an idea to think of the paper and the print position for all printers in terms of the centre line of the printer.

    However some printers can handle A4 others A3 and printers up to A0 exist.

    However for the majority of printers on the market they are only capable of printing up to A4.

    Hence the reason for the software known as a printer_driver.

    To adapt your routine for all printers therefore may be difficult if not impossible.

    Some printers will work from the centre-line as you say, some from one edge. This will depend on whether or not the input tray has one or two paper size adjusters that move.

    How about a program that could squeeze 3 cd sized labels out of a plain A4 adhesive sheet

    1 in top left, 1 in bottom left and one in centre right position

    Regards,

    S_DS



  • How to adjust Graphics for a Printer