PRB: The PrintDocument Object Prints a Graphical Object with Incorrect Margins (817042)



The information in this article applies to:

  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft Windows XP Professional
  • the operating system: Microsoft Windows XP 64-Bit Edition

SYMPTOMS

When you print a graphical object, what appears on the page is shifted away from the position that you expect it to appear in. The distance that the graphical object is shifted depends on the margins that have been set on the printer.

To explain in more detail, when you use the PrintPageEventArgs.Graphics object to print a graphical object on the PrintPage event of the PrintDocument object, PrintDocument ignores the actual physical margins (that is, the printer limitations) of the selected printer. Instead, the printout is shifted according to the margin settings of the printer.

RESOLUTION

.NET Framework 1.1

To resolve this problem in version 1.1, set the following margin values before you print the document:

PrintDocument1.OriginAtMargins = True

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, click New, and then click Project.
  3. Under Project types, click Visual Basic Projects, and then click Windows Application under Templates. By default, Form1 is created.
  4. Add the following controls to Form1 from the Toolbox: Button, PrintPreviewDialog, and PrintDocument.
  5. Right-click Form1, and then click View Code.
  6. Paste the following code after the Windows Form Designer generated code:
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            e.Graphics.DrawRectangle(System.Drawing.Pens.Blue, 0, 0, 100, 100)
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
                PrintPreviewDialog1.Document = PrintDocument1
                PrintPreviewDialog1.ShowDialog()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
  7. On the Build menu, click Build Solution.
  8. Press F5 to start the application. Form1 appears.
  9. Click Button1. The Print preview form appears.
  10. Click the Print button.

    A blank page with a rectangular border is printed on your selected printer. Notice the margin of the rectangle.
For more information about the PrintDocument class, visit the following MSDN Web site:

Modification Type:MinorLast Reviewed:5/3/2006
Keywords:kbDSWGDI2003Swept kbprb kbWindowsForms kbGDIPlus kbgdipprinting kbGDI KB817042 kbAudDeveloper