vb PrintDocument不在指定的边距内打印

时间:2016-01-15 19:56:53

标签: vb.net printing margin

我正在使用以下代码进行打印,但每次打印到激光打印机时,无论我设置的边距是什么,右边距和下边距都会被切断。有人能解释一下这种情况吗?请注意,我尝试过使用PrintDoc.OriginAtMargins = True/False,但它似乎也没有用。

/代码/

Public MarginSize As Integer = 15

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    'temp = Nothing

    'With dgView
    '    For Each row In dgView.Rows
    '        temp += row.Cells(0).Value & "   " & row.Cells(1).Value & "   " & row.Cells(2).Value & "   " & row.Cells(3).Value & vbNewLine
    '    Next
    'End With

    PrintDialog.PrinterSettings = PrintDoc.PrinterSettings

    If PrintDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then

        PrintDoc.PrinterSettings = PrintDialog.PrinterSettings

        Dim PageSetup As New PageSettings

        With PageSetup
            .Margins.Left = MarginSize
            .Margins.Right = MarginSize
            .Margins.Top = MarginSize
            .Margins.Bottom = MarginSize
            .Landscape = False
        End With

        PrintDoc.DefaultPageSettings = PageSetup

    End If

    ' PrintDoc.OriginAtMargins = False

    PrintPreviewDialog.Document = PrintDoc
    PrintPreviewDialog.WindowState = FormWindowState.Maximized
    PrintPreviewDialog.PrintPreviewControl.Zoom = 1
    PrintPreviewDialog.ShowDialog()

End Sub

Private Sub PrintDoc_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDoc.PrintPage

    Static intStart As Integer
    Dim fntText As Font = txtDrawnBy.Font
    Dim txtHeight As Integer
    Dim LeftMargin As Integer = PrintDoc.DefaultPageSettings.Margins.Left
    Dim RightMargin As Integer = PrintDoc.DefaultPageSettings.PaperSize.Width - MarginSize
    Dim TopMargin As Integer = PrintDoc.DefaultPageSettings.Margins.Top
    Dim BottomMargin As Integer = PrintDoc.DefaultPageSettings.PaperSize.Height - MarginSize

    txtHeight = PrintDoc.DefaultPageSettings.PaperSize.Height - PrintDoc.DefaultPageSettings.Margins.Top - PrintDoc.DefaultPageSettings.Margins.Bottom

    Dim LinesPerPage As Integer = CInt(Math.Round(txtHeight / (fntText.Height + 0.025)))

    'Draw Rectangle for Margin
    e.Graphics.DrawRectangle(Pens.Red, e.MarginBounds)

    Dim y1 As Integer = e.PageBounds.Height.ToString / 3
    Dim y2 As Integer = e.PageBounds.Height.ToString / 3 * 2

    'Draw line 1/4 way down
    e.Graphics.DrawLine(Pens.Orange, LeftMargin, y1, RightMargin, y1)

    'Draw line 3/4 way down
    e.Graphics.DrawLine(Pens.Orange, LeftMargin, y2, RightMargin, y2)

    Dim intLineNumber As Integer
    Dim sf As New StringFormat
    Dim LineStep As Integer = 0

    For intCounter = intStart To 66

        'Print line numbers
        e.Graphics.DrawString(intLineNumber.ToString & ": ", fntText, Brushes.Black, LeftMargin, fntText.Height * intLineNumber + TopMargin)

        intLineNumber += 1
        If intLineNumber > LinesPerPage Then
            intStart = intCounter
            e.HasMorePages = True
            Exit For
        End If

    Next

End Sub

我还附上了我的结果图片。 Image of print results

0 个答案:

没有答案