标题:乱打印的问题
只看楼主
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
得分:0 
另外,如果报表的功能并不是很复杂,就用.NET自带的datareport吧,它与VB6下的报表有质的不同.也很好用的。设计好报表布局后(所见即所得),只需给报表添加数据源即可。

===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2013-05-31 20:34
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
得分:0 
两者都乱,纸张也错,第一次打印的数据在第二次打印页面还有,错误提示如下:

下面的即时窗口显示的“在 System.ArgumentException 中第一次偶然出现的“System.Drawing.dll”类型的异常”
2013-05-31 20:48
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
得分:0 
程序里也有datareport打印,你说过datareport缓冲慢,总共有8个打印按钮
2013-05-31 20:51
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
得分:0 
我就不明白ev.Graphics.Dispose()为什么第一次打印没问题第二次就出现错误那?
2013-05-31 20:55
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
得分:0 
你把所有的ev.Graphics.Dispose()替换成ev.Graphics.Clear(Color.White)再试试

===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2013-05-31 21:10
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
得分:0 
试了,全是空白纸
2013-05-31 21:27
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
得分:0 
ev.Graphics.Clear(Color.White)这是清楚颜色呀?
2013-05-31 21:32
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
得分:0 
稍等,我给你一个测试工程

===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2013-05-31 21:40
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
得分:0 
这个工程是一个新建的项目,在窗体上拉上一个TabControl,在tabpage1上放一个按钮,一个richtextbox,在tabpage2再放上放一个按钮,一个richtextbox,
当然你可以
这应当能够模拟你的项目了,好像。
将下面的代码复制到代码编辑器中
程序代码:
Imports System.Drawing.Printing
Public Class Form1
    Private printpagesettings As PageSettings
    Private stringtoprint As String
    Private printfont As New Font("宋体", 11)
    Private printfont1 As New Font("宋体", 10, FontStyle.Regular)
    Private Sub PrintDocument1_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.EndPrint
        PrintDocument1.Dispose()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Try
        printpagesettings = New PageSettings
        PrintDocument1.DefaultPageSettings = printpagesettings
        stringtoprint = RichTextBox1.Text
        PrintDialog1.Document = PrintDocument1
        PageSetupDialog1.PageSettings = printpagesettings
        PrintDialog1.PrinterSettings.Copies = 1 ''打印的份数
        If PrintDialog1.ShowDialog = DialogResult.OK Then

            PageSetupDialog1.PageSettings.Landscape = True
            If PageSetupDialog1.ShowDialog = DialogResult.OK Then
                AddHandler PrintDocument1.PrintPage, AddressOf Me.printgraphicYDxukezheng
                PrintDocument1.Print()
                PrintDocument1.Dispose()
                RemoveHandler PrintDocument1.PrintPage, AddressOf Me.printgraphicYDxukezheng
            End If
        End If
        'Catch ex As Exception
        '    MessageBox.Show("打印出现错误", ex.ToString())
        'End Try

    End Sub
    Private Sub printgraphicYDxukezheng(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim x As Integer
        Dim y As Integer
        x = 990
        y = 120
        ev.Graphics.DrawString("RichTextBox1.Text", New Font("宋体", 13, FontStyle.Regular), Brushes.Black, 450, 274)
        ev.Graphics.DrawString("建设单位TextBox.Text", New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        ev.Graphics.DrawString("D新划TextBox.Text", New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        ev.Graphics.DrawString("D位置在TextBox.Text", New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        ev.Graphics.DrawString("D该用地TextBox.Text", New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        ev.Graphics.DrawString("D总面积TextBox.Text" & "用地面积标注ComboBox1.Text", New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        ev.Graphics.DrawString("TextBox6.Text & 建设规模标注ComboBox.Text", New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        x = x - 35
        ev.Graphics.DrawString("建设用地蓝线图:" & "建设用地红线图TextBox.Text", New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        ev.Graphics.DrawString("阿图什市规划局", New Font("宋体", 16, FontStyle.Regular), Brushes.Black, 405, 700)

        ev.Graphics.DrawString("DateTimePicker1.Text", New Font("宋体", 13, FontStyle.Regular), Brushes.Black, 419, 753)


        Dim numchars As Integer
        Dim numlines As Integer
        Dim stringfopage As String
        Dim strformat As New StringFormat
        Dim rectdraw As New RectangleF(820, 420, 530, 237)
        Dim sizemeasure As New SizeF(543, 180 - printfont.GetHeight(ev.Graphics))
        strformat.Trimming = StringTrimming.Word
        ev.Graphics.MeasureString(stringtoprint, printfont, sizemeasure, strformat, numchars, numlines)
        stringfopage = stringtoprint.Substring(0, numchars)
        ev.Graphics.DrawString(stringfopage, printfont, Brushes.Black, rectdraw, strformat)
        x = Nothing
        y = Nothing
        ev.HasMorePages = False
        ev.Graphics.Dispose()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Try

        printpagesettings = New PageSettings
        PrintDocument1.DefaultPageSettings = printpagesettings
        stringtoprint = RichTextBox2.Text
        PrintDialog1.Document = PrintDocument1
        PageSetupDialog1.PageSettings = printpagesettings
        PrintDialog1.PrinterSettings.Copies = 1 ''打印的份数
        If PrintDialog1.ShowDialog = DialogResult.OK Then
            PageSetupDialog1.PageSettings.Landscape = True
            If PageSetupDialog1.ShowDialog = DialogResult.OK Then
                AddHandler PrintDocument1.PrintPage, AddressOf Me.printgraphicxcxukezheng
                PrintDocument1.Print()
                PrintDocument1.Dispose()
                RemoveHandler PrintDocument1.PrintPage, AddressOf Me.printgraphicxcxukezheng
            End If
        End If
        'Catch ex As Exception
        '    MessageBox.Show("打印出现错误", ex.ToString())
        'End Try


    End Sub
    Private Sub printgraphicxcxukezheng(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim x As Integer
        Dim y As Integer
        x = 995
        y = 110
        ev.Graphics.DrawString("RichTextBox2.Text", New Font("宋体", 13, FontStyle.Regular), Brushes.Black, 455, 274)
        ev.Graphics.DrawString("TextBox39.Text", New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 50
        ev.Graphics.DrawString("TextBox41.Text", New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 50
        ev.Graphics.DrawString("ComboBox6.Text", New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 50
        ev.Graphics.DrawString("TextBox40.Text", New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 50
        ev.Graphics.DrawString("TextBox38.Text", New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        ev.Graphics.DrawString("阿图什市规划局", New Font("宋体", 16, FontStyle.Regular), Brushes.Black, 405, 700)

        ev.Graphics.DrawString("DateTimePicker4.Text", New Font("宋体", 13, FontStyle.Regular), Brushes.Black, 419, 753)

        Dim numchars As Integer
        Dim numlines As Integer
        Dim stringfopage As String
        Dim strformat As New StringFormat
        Dim rectdraw As New RectangleF(820, 325, 530, 237)
        Dim sizemeasure As New SizeF(543, 180 - printfont.GetHeight(ev.Graphics))
        strformat.Trimming = StringTrimming.Word
        ev.Graphics.MeasureString(stringtoprint, printfont, sizemeasure, strformat, numchars, numlines)
        stringfopage = stringtoprint.Substring(0, numchars)
        ev.Graphics.DrawString(stringfopage, printfont, Brushes.Black, rectdraw, strformat)
        x = Nothing
        y = Nothing
        ev.HasMorePages = False

    End Sub

End Class
我测试了多次,而且注释掉了所有的TRY,一点儿没有重叠的地方。
现在我可以作出判断,如果你经过比对我这个项目代码之后,你仍然出现重叠,那么只有下面的一种可能:
就是你每次ev.Graphics.DrawString的时候,传递过去的string参数的内容过长,而你在之所赋值的y值过小,才会导致重叠现象的发生!



===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2013-05-31 21:58
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
得分:0 
这行是什么意思?
  Private Sub PrintDocument1_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.EndPrint
        PrintDocument1.Dispose()
    End Sub
2013-05-31 22:06



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-412490-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.116228 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved