CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2006
    Posts
    181

    Difficulty Printing

    Everything I draw looks perfect and is aligned perfectly in the print preview but when I actually print things come out different.

    For example, if I draw a rectangle using printableArea, I get a rectangle about 1/4" around the page in the print preview but when I print the right side of the rectangle is cut off.

    After trying more stuff, I tried setting the margins to 1/2" and used MarginBounds instead of PrintableArea. Everything shows up nicely centered on the print preview but when I print everything is shifted right and down half an inch.

    I thought what you do in the print preview is supposed to be the same as the actual print. Obviously not, I don't have a problem with writing code to check if I'm doing a print preview or an actual printing and adjust the locations I calculate accordingly but I need to know what's going on the adjust my calculations. I have yet to see a pattern. Are my calculations automatically shifted by the printable area? the margin? something else?

    Can someone explain what's going on?


    Thanks,
    Scott MacMaster
    Last edited by Scott.Macmaster; November 8th, 2006 at 05:09 PM.

  2. #2
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: Difficulty Printing

    Sample
    declaration
    Code:
        Private WithEvents pdoc As New Printing.PrintDocument()
    PrintDialog button
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim dialog As New PrintDialog()
            dialog.Document = pdoc
    
            If dialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                pdoc.Print()
            End If
        End Sub
    Code:
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim ppd As New PrintPreviewDialog()
            Try
                ppd.Document = pdoc
                ppd.ShowDialog()
            Catch exp As Exception
                MessageBox.Show("Error")
            End Try
        End Sub
    PageSetupDialog button
    Code:
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Dim psd As New PageSetupDialog()
            With psd
                .Document = pdoc
                .PageSettings = pdoc.DefaultPageSettings
            End With
    
            If psd.ShowDialog = Windows.Forms.DialogResult.OK Then
                pdoc.DefaultPageSettings = psd.PageSettings
            End If
        End Sub
    PrintPage event
    Code:
        Private Sub pdoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdoc.PrintPage
    
            Dim intPrintAreaHeight, intPrintAreaWidth, marginLeft, marginTop As Int32
            With pdoc.DefaultPageSettings
                intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
                intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
    
                marginLeft = .Margins.Left ' X coordinate
                marginTop = .Margins.Top ' Y coordinate
            End With
    
            ' If the user selected Landscape mode, swap the printing area height 
            ' and width.
            If pdoc.DefaultPageSettings.Landscape Then
                Dim intTemp As Int32
                intTemp = intPrintAreaHeight
                intPrintAreaHeight = intPrintAreaWidth
                intPrintAreaWidth = intTemp
            End If
    
            Dim rectPrintingArea As New Rectangle(marginLeft, marginTop, intPrintAreaWidth, intPrintAreaHeight)
            e.Graphics.DrawRectangle(Pens.Blue, rectPrintingArea)
        End Sub

  3. #3
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Difficulty Printing

    >Can someone explain what's going on?

    The printer has a "hard margin" that the printpreview controls does not, usually it's around 1/4 inch.

  4. #4
    Join Date
    Oct 2006
    Posts
    181

    Re: Difficulty Printing

    THanks for helping, however the example code you gave did the exact same thing my code does. The print preview is aligned perfectly but the actual printout on paper isn't. I set the margin at .5" but it got shifted right and down but about 3/16" of an inch. Do you know why this is happening? Thanks.

  5. #5
    Join Date
    Oct 2006
    Posts
    181

    Re: Difficulty Printing

    Ok so printers have a "hard margin". So how do I figure out what that is and add/subtract that in my calculations? Thanks.

  6. #6
    Join Date
    Oct 2006
    Posts
    28

    Re: Difficulty Printing

    I would look for a low tech solution: print a line down the edges of your page with a known margin, then measure the actual location with a ruler, and compensate accordingly.

    Obviously this won't help if you want your code to work on several printers, but it might be worth trying a few and seeing if there is a tendency for the value to be approx 1/4 inch.

    H
    I'm using .NET Framework 3.5

    I'm planning to be spontaneous tomorrow

  7. #7
    Join Date
    Oct 2006
    Posts
    181

    Re: Difficulty Printing

    I'm working for the telmis department of a community college. I'm writing software that various college staff will use to print reports. The printers I've used so far have printing differences of almost 1/4". I really need something more exact, espacially when I'm printing on preprinted certificates and labels. My current fix is to have an alignment option in my software. That will allow college staff to adjust the printing for specific printers. Of course, I rather this be automatic and simple for them.

  8. #8
    Join Date
    Sep 2006
    Posts
    2

    Re: Difficulty Printing


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured