首先让我开始,我不是vb.net开发人员。事实上,我从未接受过VB艺术的培训。话虽这么说,我正在开发一个非常简单的应用程序,它接受一个csv文件并将一个列解析为一个数组列表。现在我需要获取该数组列表并打印数组列表中每个项目的单个页面(无预览)。因此,数组列表中的每个项目都有自己的页面。
到目前为止我所拥有的。我确定我没有看到因为我无法弄清楚如何把它变成多页。
Private Sub Print()
Dim PrintPreviewSelected As Boolean = False
'Set the doc to print
Dim pDoc As New PrintDocument
pDoc.PrintController = New StandardPrintController 'turns off the printing page x of y dialog
Try
Using sr As New StreamReader(file)
defPrinter = sr.ReadToEnd()
End Using
Catch e As Exception
End Try
If defPrinter = "" Then
If Me.PrintDialog1.ShowDialog() = DialogResult.OK Then
pDoc.PrinterSettings.PrinterName = Me.PrintDialog1.PrinterSettings.PrinterName
End If
Else
pDoc.PrinterSettings.PrinterName = defPrinter
End If
pDoc.DefaultPageSettings.Landscape = True
pDoc.DefaultPageSettings.Margins = New Margins(40, 10, 10, 10)
pDoc.OriginAtMargins = True
AddHandler pDoc.PrintPage, AddressOf PrintSett
If PrintPreviewSelected Then
PrintPreviewDialog1.Document = pDoc
PrintPreviewDialog1.UseAntiAlias = True
PrintPreviewDialog1.WindowState = FormWindowState.Maximized
PrintPreviewDialog1.ShowDialog()
Else
If txtFile.Text <> "" Then
pDoc.Print()
Else
MessageBox.Show("You must select a file first", "Select a file.")
End If
End If
RemoveHandler pDoc.PrintPage, AddressOf PrintSett
End Sub
Private Sub PrintSett(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim fnt10 As Font = New Font("Courier New", 34, FontStyle.Regular)
e.Graphics.DrawString("", fnt10, Brushes.Black, 318, 412)
End Sub
任何帮助将不胜感激!我知道我还没有为你们工作奠定任何基础,但坦率地说,我的失败了。谢谢你们!
答案 0 :(得分:2)
根据matzone的建议,我能够弄明白。
Dim PageNumber As Integer = 1
Dim morePage As String
Private Sub PrintSett(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim ReportFont As New Font("Arial", 45, FontStyle.Regular)
Dim VerticalPrintLocationSingle As Single = 412
Dim HorizontalPrintLocationSingle As Single = e.MarginBounds.Left
Dim TextString As String
Dim sngCenterPage As Single
If customerList.Count > (PageNumber) Then
If customerList.Item(PageNumber) IsNot "" Then
TextString = customerList.Item(PageNumber)
Console.WriteLine(customerList.Item(PageNumber))
sngCenterPage = Convert.ToSingle(e.PageBounds.Width / 2 - e.Graphics.MeasureString(customerList.Item(PageNumber), ReportFont).Width / 2)
PageNumber += 1
morePage = True
End If
Else
morePage = False
customerList.Clear()
End If
e.Graphics.DrawString(TextString, ReportFont, Brushes.Black, sngCenterPage, VerticalPrintLocationSingle)
e.HasMorePages = morePage
End Sub
再次感谢!