【问题标题】:Printing multiple pages from an ArrayList从 ArrayList 打印多个页面
【发布时间】:2014-03-20 14:08:23
【问题描述】:

首先让我从我不是 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

任何帮助将不胜感激!我知道我还没有为你们打下任何基础,但坦率地说,我迷路了。谢谢大家!

【问题讨论】:

  • 您应该在sub printsett 中逐行执行。它会增加Y 位置,当大于纸张高度时您应该执行e.hasmorepages=true ...
  • 任何你可以详细说明的方式。我尝试在 print 中运行 for each 循环,但它会导致打印循环。

标签: vb.net printing arraylist


【解决方案1】:

按照 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

再次感谢!

【讨论】:

  • 哈哈谢谢!我有点跳过了整个“Hello World”教程。
猜你喜欢
  • 2012-03-09
  • 1970-01-01
  • 1970-01-01
  • 2012-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-02
  • 1970-01-01
相关资源
最近更新 更多