帮助:任何印刷大师? (PrintPreview OK ... PrintDoc ......不!) [英] HELP: Any Printing Guru's? (PrintPreview OK...PrintDoc... NOT!)

查看:50
本文介绍了帮助:任何印刷大师? (PrintPreview OK ... PrintDoc ......不!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我坚持不懈!我现在已经搞了一个星期了。

我试图找到类似于我的例子(nada)。有很多

流式传输TXT文件......哇!


我真的被困在这里(可能是'''''''''''''''''''''''''''''''''''''''''''''''

森林东西)。但我无法让它工作。


以下是我的测试示例代码。只需要一个空白表格上的Button1。

添加PrintDocument1,PrintPreviewDialog1,PageSetupDialog1 ......并运行它。


它可以完成我想要的一切到...除了打印!我得到了多页

预览。我的格式化还可以。但所有它打印的是一个单独的空白页面,

myWeeks Total Hours:在页面上...为什么???为什么我可以预览OK ...但是

不打印?

我的测试代码(注意自动换行):


Imports System.IO

Imports System.Drawing.Printing

''********************** **************************** *******************
''打印出变量

Dim itm,rowTtl As Integer

Dim rwCntr As Integer''Print Rows Counter

Dim pageNo As Integer''Page Number

Dim PtrCtr As Integer

Dim tableFont,tableFont1 As Font

Dim X1,X2,X3 As Integer

Dim W1,W2,W3 As Integer

Dim Y As Integer

Dim lbWeekItems As Integer = 30

' '************************************************* * *******************


''**************** ********************************** **************** ***

Private Sub Button1_Click(ByVal sender As System.Object,ByVal eg As

System.EventArgs)处理Button1.Click


Dim pageWidth As Integer

PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings

PageSetupDialog1.AllowOrientation = False

PrintDocument1.DefaultPageSettings.Margins.Top = 75

PrintDocument1.DefaultPageSettings.Margins.Bottom = 50

PrintDocument1.DefaultPageSettings.Margins.Left = 50

PrintDocument1.DefaultPageSettings.Margins.Right = 50

PrintDocument1.DefaultPageSettings.Landscape = True

tableFont = New Font(" Arial",10)< br $>
tableFont1 =新字体(" Arial",9)


使用PrintDocument1.DefaultPageSettings

pageWidth = .PaperSize.Height - _

.Margins.Left - _

.Margins.Right

''以百分之一英寸的尺寸测量

结束


PtrCtr = 0

rwCntr = 0

pageNo = 0


''X'是列左边缘的坐标

''W'是列的宽度

W1 = 75

W2 = 250

W3 = 40

X1 = 50

X2 = 410

X3 = 720


尝试

PrintPreviewDialog1.Document = PrintDocument1

PrintPreviewDialog1.ShowDialog()

Catch exc As Exception

MsgBox(打印操作失败... " &安培; vbCrLf& exc.Message)

结束尝试


结束子

''************ ************************************** ************ *******

受保护的子PrintDocHeader(ByVal e As

System.Drawing.Printing.PrintPageEventArgs)

Dim str As String


Y = PrintDocument1.DefaultPageSettings.Margins.Top

str =" Time Sheet Header"

e.Graphics.DrawString (str,tableFont,Brushes.Black,X1,Y)

Y = Y + 25

e.Graphics.DrawString(" Job Information",tableFont,Brushes。黑色,X1,Y)

e.Graphics.DrawString(" Cost Code Information",tableFont,Brushes.Black,

X2,Y)

e.Graphics.DrawString(" Sat",tableFont,Brushes.Black,X3,Y)

e.Graphics.DrawString(" Sun",tableFont,Brushes.Black,X3) + 40,Y)

e.Graphics.DrawString(" Mon",tableFont,Brushes.Black,X3 + 80,Y)

e.Graphics.DrawString( "星期二",tableFont,Brushes.Black,X3 + 120,Y)

e.Graphics.DrawString(" Wed",tableFont,Brushes.Black,X3 + 160,Y)

e.Graphics.DrawString(" Thr",tableFont,Brushes.Black,X3 + 205,Y)

e.Graphics.DrawString(" Fri",tableFont,Brushes。黑色,X3 + 245,Y)

e.Graphics.DrawString(" Total",tableFont,Brushes.Black,X3 + 283,Y)

Y = Y + 20

''绘制折断线

使用PrintDocument1.DefaultPageSettings

e.Graphics.DrawLine(Pens.Black,X1,Y ,. PaperSize.Height - 50,Y)

e.Graphics.DrawLine(Pens.Black,X1,Y + 2,.PaperSize.Height - 50,Y +

2 )

结束

Y = Y + 4


结束子

''** ************************************************ ** *****************

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object,ByVal e

As System.Drawing .Printing.PrintPageEventArgs)句柄

PrintDocument1.PrintPage


而PtrCtr< lbWeekItems

Dim str As String

rwCntr = rwCntr + 1


''打印页面页眉

如果rwCntr = 1那么

PrintDocHeader(e)

''打印页码

pageNo = pageNo + 1

e.Graphics.DrawString(" Page"& pageNo,tableFont,Brushes.Black,

1000,800)

结束如果


Dim R1作为新RectangleF(X1,Y,W1,80)''Col 1作业

Dim R2作为新RectangleF(X1 + 80,Y,W2,80) ''Col 2 Job Desc

Dim R3为新RectangleF(X2,Y,W1,80)''Col 3 cCode

Dim R4为新RectangleF(X2 + 60) ,Y,W2,80)''Col 4 cCode Desc

Dim R5 As New RectangleF(X3,Y,W3,80)''Col 5 Sat

Dim R6如新的RectangleF(X3 + 40,Y,W3,80)''Col 6

Dim R7 As New RectangleF(X3 + 80,Y,W3,80)''Col 7

Dim R8 As New RectangleF(X3 + 120,Y,W3,80)''Col 8 Tue

Dim R9 As New RectangleF(X3 + 160,Y,W3,80)''Col 9

Dim R10 As New RectangleF(X3 + 205,Y,W3,80)''Col 10

Dim R11为新的RectangleF(X3 + 245,Y,W3,80)''Col 11 Fri

Dim R12为新的RectangleF(X3 + 285,Y, W3,80)''Col 12 Total

Dim R13 As New RectangleF(X1,Y + 17,1050,80)''Col 13 cCode Desc


str =" Item"

e.Graphics.DrawString(str,tableFont,Brushes.Black,R1)

e.Graphics.DrawString(str,tableFont,Brushes) .Black,R2)

e.Graphics.DrawString(str,tableFont,Brushes.Black,R3)

e.Graphics.DrawString(str,tableFont,Brushes.Black ,R4)


''周日时间

str = lbWeekItems

e.Graphics.DrawString(str,tableFont,Brushes) .Black,R5)

e.Graphics.DrawString(str,tableFont,Brushes.Black,R6)

e.Graphics.DrawString(str,tableFont,Brushes.Black ,R7)

e.Graphics.DrawString(str ,tableFont,Brushes.Black,R8)

e.Graphics.DrawString(str,tableFont,Brushes.Black,R9)

e.Graphics.DrawString(str,tableFont) ,Brushes.Black,R10)

e.Graphics.DrawString(str,tableFont,Brushes.Black,R11)

e.Graphics.DrawString(" 22", tableFont,Brushes.Black,R12)

Y = Y + 15


str ="行项目:" &安培; lbWeekItems

如果Len(str)< = 165那么

tableFont1 =新字体(Arial,9)

Else

tableFont1 =新字体(" Arial",8)

结束如果

e.Graphics.DrawString(str,tableFont1,Brushes.Black, R13)

''重置字体

tableFont1 =新字体(" Arial",9)


''Draw Break行

使用PrintDocument1.DefaultPageSettings

Y = Y + 20

e.Graphics.DrawLine(Pens.Black,X1,Y,.PaperSize .Height - 50,Y)

结束


''这就是这个页面...

如果rwCntr = 18那么

e.HasMorePages = True

PtrCtr = PtrCtr + 1

rwCntr = 0

退出Sub

结束如果

PtrCtr = PtrCtr + 1

结束时


''总计周时间(最后一页)

使用PrintDocument1.DefaultPageSettings

e.Graphics.DrawString(" Weeks Total Hours:",table Font,Brushes.Black,

875,775)

e.Graphics.DrawString(" 44",tableFont,Brushes.Black,1000,775)
结束


''没有更多页面打印

e.HasMorePages = False


结束分

''************************************* ************* *******************


任何善良的灵魂都可以发现麻烦?


问候,


布鲁斯

This is driving me NUTZ!!! I''ve been screwing around on this for a week now.
And I have tried to find examples similar to what I have (nada). Got lots of
streaming a TXT file... bah!

I am really stuck here (probably the ol'' Not seeing the tree cause of the
forest thingy). But I just can not get this to work.

The following is my Test example code. Just needs a Button1 on a blank Form.
Add in PrintDocument1, PrintPreviewDialog1, PageSetupDialog1... and run it.

It does everything that I want it to... EXCEPT print! I get the multiple page
preview. My formating is okay. But all it prints is a Single Blank page with
my "Weeks Total Hours: " on the page... Why??? Why can I preview OK... but
not print?
MY Test CODE (watch for word wrap):

Imports System.IO
Imports System.Drawing.Printing
'' ************************************************** *******************
'' Print Out Variables
Dim itm, rowTtl As Integer
Dim rwCntr As Integer '' Print Rows Counter
Dim pageNo As Integer '' Page Number
Dim PtrCtr As Integer
Dim tableFont, tableFont1 As Font
Dim X1, X2, X3 As Integer
Dim W1, W2, W3 As Integer
Dim Y As Integer
Dim lbWeekItems As Integer = 30
'' ************************************************** *******************

'' ************************************************** *******************
Private Sub Button1_Click(ByVal sender As System.Object, ByVal eg As
System.EventArgs) Handles Button1.Click

Dim pageWidth As Integer
PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings
PageSetupDialog1.AllowOrientation = False
PrintDocument1.DefaultPageSettings.Margins.Top = 75
PrintDocument1.DefaultPageSettings.Margins.Bottom = 50
PrintDocument1.DefaultPageSettings.Margins.Left = 50
PrintDocument1.DefaultPageSettings.Margins.Right = 50
PrintDocument1.DefaultPageSettings.Landscape = True

tableFont = New Font("Arial", 10)
tableFont1 = New Font("Arial", 9)

With PrintDocument1.DefaultPageSettings
pageWidth = .PaperSize.Height - _
.Margins.Left - _
.Margins.Right
'' Measured in 1/100ths of an Inch
End With

PtrCtr = 0
rwCntr = 0
pageNo = 0

'' X''s are coordinates of the left edge of the columns
'' W''s are the widths of the columns
W1 = 75
W2 = 250
W3 = 40
X1 = 50
X2 = 410
X3 = 720

Try
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
Catch exc As Exception
MsgBox("Print Operation Failed... " & vbCrLf & exc.Message)
End Try

End Sub
'' ************************************************** *******************
Protected Sub PrintDocHeader(ByVal e As
System.Drawing.Printing.PrintPageEventArgs)
Dim str As String

Y = PrintDocument1.DefaultPageSettings.Margins.Top
str = "Time Sheet Header"
e.Graphics.DrawString(str, tableFont, Brushes.Black, X1, Y)
Y = Y + 25
e.Graphics.DrawString("Job Information", tableFont, Brushes.Black, X1, Y)
e.Graphics.DrawString("Cost Code Information", tableFont, Brushes.Black,
X2, Y)
e.Graphics.DrawString("Sat", tableFont, Brushes.Black, X3, Y)
e.Graphics.DrawString("Sun", tableFont, Brushes.Black, X3 + 40, Y)
e.Graphics.DrawString("Mon", tableFont, Brushes.Black, X3 + 80, Y)
e.Graphics.DrawString("Tue", tableFont, Brushes.Black, X3 + 120, Y)
e.Graphics.DrawString("Wed", tableFont, Brushes.Black, X3 + 160, Y)
e.Graphics.DrawString("Thr", tableFont, Brushes.Black, X3 + 205, Y)
e.Graphics.DrawString("Fri", tableFont, Brushes.Black, X3 + 245, Y)
e.Graphics.DrawString("Total", tableFont, Brushes.Black, X3 + 283, Y)
Y = Y + 20
'' Draw Break Lines
With PrintDocument1.DefaultPageSettings
e.Graphics.DrawLine(Pens.Black, X1, Y, .PaperSize.Height - 50, Y)
e.Graphics.DrawLine(Pens.Black, X1, Y + 2, .PaperSize.Height - 50, Y +
2)
End With
Y = Y + 4

End Sub
'' ************************************************** *******************
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e
As System.Drawing.Printing.PrintPageEventArgs) Handles
PrintDocument1.PrintPage

While PtrCtr < lbWeekItems
Dim str As String
rwCntr = rwCntr + 1

'' Print Page Header
If rwCntr = 1 Then
PrintDocHeader(e)
'' Print Page Number
pageNo = pageNo + 1
e.Graphics.DrawString("Page " & pageNo, tableFont, Brushes.Black,
1000, 800)
End If

Dim R1 As New RectangleF(X1, Y, W1, 80) '' Col 1 Job
Dim R2 As New RectangleF(X1 + 80, Y, W2, 80) '' Col 2 Job Desc
Dim R3 As New RectangleF(X2, Y, W1, 80) '' Col 3 cCode
Dim R4 As New RectangleF(X2 + 60, Y, W2, 80) '' Col 4 cCode Desc
Dim R5 As New RectangleF(X3, Y, W3, 80) '' Col 5 Sat
Dim R6 As New RectangleF(X3 + 40, Y, W3, 80) '' Col 6
Dim R7 As New RectangleF(X3 + 80, Y, W3, 80) '' Col 7
Dim R8 As New RectangleF(X3 + 120, Y, W3, 80) '' Col 8 Tue
Dim R9 As New RectangleF(X3 + 160, Y, W3, 80) '' Col 9
Dim R10 As New RectangleF(X3 + 205, Y, W3, 80) '' Col 10
Dim R11 As New RectangleF(X3 + 245, Y, W3, 80) '' Col 11 Fri
Dim R12 As New RectangleF(X3 + 285, Y, W3, 80) '' Col 12 Total
Dim R13 As New RectangleF(X1, Y + 17, 1050, 80) '' Col 13 cCode Desc

str = "Item"
e.Graphics.DrawString(str, tableFont, Brushes.Black, R1)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R2)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R3)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R4)

'' Week Day Hours
str = lbWeekItems
e.Graphics.DrawString(str, tableFont, Brushes.Black, R5)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R6)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R7)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R8)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R9)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R10)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R11)
e.Graphics.DrawString("22", tableFont, Brushes.Black, R12)
Y = Y + 15

str = "Line Item: " & lbWeekItems
If Len(str) <= 165 Then
tableFont1 = New Font("Arial", 9)
Else
tableFont1 = New Font("Arial", 8)
End If
e.Graphics.DrawString(str, tableFont1, Brushes.Black, R13)
'' Reset Font
tableFont1 = New Font("Arial", 9)

'' Draw Break Line
With PrintDocument1.DefaultPageSettings
Y = Y + 20
e.Graphics.DrawLine(Pens.Black, X1, Y, .PaperSize.Height - 50, Y)
End With

'' That''s it for this page...
If rwCntr = 18 Then
e.HasMorePages = True
PtrCtr = PtrCtr + 1
rwCntr = 0
Exit Sub
End If
PtrCtr = PtrCtr + 1
End While

'' Total Weeks Hours (last page)
With PrintDocument1.DefaultPageSettings
e.Graphics.DrawString("Weeks Total Hours: ", tableFont, Brushes.Black,
875, 775)
e.Graphics.DrawString("44", tableFont, Brushes.Black, 1000, 775)
End With

'' No More Pages to Print
e.HasMorePages = False

End Sub
'' ************************************************** *******************

Any kind soul can spot the trouble?

Regards,

Bruce

推荐答案

我也可以预览你的代码了。我现在不在打印机上,

因此我无法测试打印输出,但我相信我知道你的问题是什么。


当你调用print时,文档会再次通过onPrintPage系列

的事件。由于您在循环点击事件中初始化值

,当您点击

打印时,这些值已经超过了他们的点数。


您可以通过添加带有

预览的第二个按钮来验证这一点而无需打印。在该按钮中,不要初始化你的弯曲者。首先点击

普通按钮,你会看到你很好的打印预览(顺便说一下,在

上使用PrintDocuments的工作很好)。然后单击第二个按钮,

,您将看到报告的问题。


解决方案是将循环初始化程序移动到打印处理中代码。


还有一件事,你可能想要在

代码的顶部打开选项严格。在打印

处理程序中有一个从整数到字符串的隐式转换。


Justin Weinberg


设计一个PrintDocument或创建.NET图形?

使用GDI + Architect节省时间。

有关更多信息,请访问 http://www.mrgsoft.com


<长码部分剪辑>
I''m able to preview your code OK also. I''m not at a printer at the moment,
so I can''t test printed output, but I believe I know what your problem is.

When you invoke "print", the document goes through the onPrintPage series
of events all over again. Since you initialized values in the click event
for your loops, these values are already past their points when you click on
print.

You can verify this without printing by adding a second button with a
preview. In that button, do not initialize your loopers. Click on the
normal button first and you''ll see your nice print preview (nice job on
working with PrintDocuments by the way). Click on the second button next,
and you''ll see the problem you are reporting.

The solution is to move your loop initializers into the printhandling code.

One more thing, you might want to turn option strict on at the top of your
code. You have an implicit cast from integer to string in your printing
handler.

Justin Weinberg

Designing a PrintDocument or creating .NET graphics?
Save time with GDI+ Architect.
For more information, visit http://www.mrgsoft.com

<Long code section snipped>


轻微修正。使用开始打印事件作为初始化程序,因为

您使用它们来确定PrintPage事件中的延续。


Private Sub PrintDocument1_BeginPrint(ByVal sender As Object,ByVal e As

System.Drawing.Printing.PrintEventArgs)处理PrintDocument1.BeginPrint

PtrCtr = 0

rwCntr = 0

pageNo = 0

End Sub

-

Justin Weinberg


设计PrintDocument或创建.NET图形?

使用GDI + Architect节省时间。

欲了解更多信息,请访问 http://www.mrgsoft.com


" Justin Weinberg" < jweinberg@_spamoff_mrgsoft.com>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...
Slight correction. Use the begin print event for your initializers since
your using them to determine continuation in your PrintPage event.

Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
PtrCtr = 0
rwCntr = 0
pageNo = 0
End Sub
--
Justin Weinberg

Designing a PrintDocument or creating .NET graphics?
Save time with GDI+ Architect.
For more information, visit http://www.mrgsoft.com

"Justin Weinberg" <jweinberg@_spamoff_mrgsoft.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
我'能够预览你的代码也行。我不是在
时刻的打印机,所以我无法测试打印输出,但我相信我知道你的问题是什么。

当你调用打印时;,该文件再次通过onPrintPage
系列事件。由于您在循环的单击事件
中初始化值,当您在打印时单击
时,这些值已经超过了它们的点。

您可以通过添加带有
预览的第二个按钮。在该按钮中,不要初始化你的弯曲者。首先点击
普通按钮,你会看到你很好的打印预览(顺便说一下,使用PrintDocuments工作很好)。然后单击第二个按钮,
您将看到报告的问题。

解决方案是将循环初始化程序移动到printhandling
代码中。还有一件事,你可能想要在
代码的顶部打开选项严格。您在打印处理程序中有一个从整数到字符串的隐式转换。

Justin Weinberg

设计PrintDocument或创建.NET图形?
保存与GDI + Architect合作的时间。
欲了解更多信息,请访问 http://www.mrgsoft.com

<长码部分剪辑>
I''m able to preview your code OK also. I''m not at a printer at the moment, so I can''t test printed output, but I believe I know what your problem is.

When you invoke "print", the document goes through the onPrintPage series of events all over again. Since you initialized values in the click event
for your loops, these values are already past their points when you click on print.

You can verify this without printing by adding a second button with a
preview. In that button, do not initialize your loopers. Click on the
normal button first and you''ll see your nice print preview (nice job on
working with PrintDocuments by the way). Click on the second button next,
and you''ll see the problem you are reporting.

The solution is to move your loop initializers into the printhandling code.
One more thing, you might want to turn option strict on at the top of your
code. You have an implicit cast from integer to string in your printing
handler.

Justin Weinberg

Designing a PrintDocument or creating .NET graphics?
Save time with GDI+ Architect.
For more information, visit http://www.mrgsoft.com

<Long code section snipped>



使用Deft Fingers,Justin Weinberg < jweinberg@_spamoff_mrgsoft.com>写道:
With Deft Fingers, "Justin Weinberg" <jweinberg@_spamoff_mrgsoft.com> wrote:
轻微修正。使用begin print事件作为初始化程序,因为
使用它们来确定PrintPage事件的延续。

Private Sub PrintDocument1_BeginPrint(ByVal sender As Object,ByVal e As
System。 Drawing.Printing.PrintEventArgs)处理PrintDocument1.BeginPrint
PtrCtr = 0
rwCntr = 0
pageNo = 0
End Sub
Slight correction. Use the begin print event for your initializers since
your using them to determine continuation in your PrintPage event.

Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
PtrCtr = 0
rwCntr = 0
pageNo = 0
End Sub



OMG !!!!是!!!那就搞定了!谢谢,谢谢,谢谢你...


该死的......我知道我亲近了......但是(相信与否)......是我第一次打印的b
。当然,我所做的就是选择最糟糕的事情来做点什么......然后一旦我拿到它,一切都在下坡!!


很多谢谢!!!


哦,Option Strict On ...谢谢。正在测试我的东西...


问候,


布鲁斯


OMG!!!! YES!!! That''s got it! THANK YOU THANK YOU THANK YOU THANK YOU...

Damn... I knew I was ''close''... but (belive it or not)... this is my first
attempt at printing. And of course, what I do it pick the worst possible
thing to do... then once I got it, everything is downhill!!

Many Thanks!!!

Oh and the Option Strict On... thanks. Just was testing out my stuff...

Regards,

Bruce


这篇关于帮助:任何印刷大师? (PrintPreview OK ... PrintDoc ......不!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆