【问题标题】:Programmatically insert page break in vb.net以编程方式在 vb.net 中插入分页符
【发布时间】:2012-07-26 13:44:02
【问题描述】:

所以我试图在直接从我的 VB.NET 程序打印时强制分页。我基本上是使用 MSDN 中的这段代码来打印我的文档:

Private Sub printDocument1_PrintPage(ByVal sender As Object, _
    ByVal e As PrintPageEventArgs)

    Dim charactersOnPage As Integer = 0
    Dim linesPerPage As Integer = 0

    ' Sets the value of charactersOnPage to the number of characters 
    ' of stringToPrint that will fit within the bounds of the page.
    e.Graphics.MeasureString(stringToPrint, Me.Font, e.MarginBounds.Size, _
        StringFormat.GenericTypographic, charactersOnPage, linesPerPage)

    ' Draws the string within the bounds of the page
    e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black, _
    e.MarginBounds, StringFormat.GenericTypographic)

   ' Remove the portion of the string that has been printed.
    stringToPrint = stringToPrint.Substring(charactersOnPage)

    ' Check to see if more pages are to be printed.
    e.HasMorePages = stringToPrint.Length > 0

End Sub

这使它可以很好地打印,但我想在特定位置放置分页符。我试过 e.HasMorePages = true,但我不明白这如何让我在特定位置中断。假设我的 stringToPrint 长度为 5000 个字符,我想在 1000 个字符后开始新页面,然后在接下来的 2500 个字符后重新开始。我该怎么做?

此外,将 linesOnPage 和 charactersOnPage 更改为其他值似乎根本没有任何改变。

编辑:我想有关我的程序所做的更多信息会有所帮助。基本上它所做的是程序将创建大约 4 页完整的数据,并将其打印到 .txt 文件中。现在,我想打印整个 .txt 文件。我知道如何做到这一点的唯一方法是打印一个字符串,所以我让它逐行读取整个 .txt 文件并将其全部存储为一个字符串(即 stringToPrint)。现在,使用上面的代码,我打印 stringToPrint。

【问题讨论】:

    标签: vb.net printing formatting


    【解决方案1】:

    您当前的代码基于打印到矩形:e.MarginBounds

    为了测试,我使用了这段代码:

    Private pageNum As Integer
    
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
                              Handles Button1.Click
      Dim sb As New StringBuilder
      For i As Integer = 1 To 100
        sb.AppendLine(i.ToString & ") This is a line.")
      Next
      stringToPrint = sb.ToString
    
      pageNum = 0
      Using pvw As New PrintPreviewDialog()
        pvw.Document = printDocument1
        pvw.ShowDialog()
      End Using
    End Sub
    

    然后我将您的例程更改为在第一页上使用较小的矩形:

    Private Sub printDocument1_PrintPage(ByVal sender As Object, _
                                         ByVal e As PrintPageEventArgs) _
                                         Handles printDocument1.PrintPage
      Dim charactersOnPage As Integer = 0
      Dim linesPerPage As Integer = 0
    
      pageNum += 1
    
      Dim printSize As Size = e.MarginBounds.Size
      If pageNum = 1 Then
        printSize = New Size(printSize.Width, printSize.Height / 2)
      End If
      e.Graphics.MeasureString(stringToPrint, Me.Font, printSize, _
          StringFormat.GenericTypographic, charactersOnPage, linesPerPage)
    
      ' Draws the string within the bounds of the page
      e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black, _
        New Rectangle(e.MarginBounds.Location, printSize), _
        StringFormat.GenericTypographic)
    
      ' Remove the portion of the string that has been printed.
      stringToPrint = stringToPrint.Substring(charactersOnPage)
    
      ' Check to see if more pages are to be printed.
      e.HasMorePages = stringToPrint.Length > 0
    End Sub
    

    如果您需要根据字符控制打印,则必须放弃打印文本块的代码并逐行打印所有内容。

    【讨论】:

    • 太好了,非常感谢。我会更多地摆弄这个,但它肯定是有效的。唯一的问题是我的程序打印了很多必须完美格式化的表格,因此更改大小和边距等内容确实会影响它。只是好奇,您将如何将其更改为基于字符而不是框大小进行打印?
    • @IncurableHam 我不确定我是否看到您如何在像stringToPrint 这样的单个字符串变量中拥有表。如果没有看到更多代码很难回答,但我猜有一个List<of Stuff> 来打印你可以迭代循环的地方,然后根据它确定你的分页符。
    • 这是我之前进行实际打印的代码:e.Graphics.DrawString(stringtoPrint, printFont, Brushes.Black, _ 75, 0, StringFormat.GenericTypographic) 基本上我需要这些边距( 75, 0) 以便正确打印。如何使用新的 Rectangle 将这些添加到 e.Graphics.Drawstring?
    • @IncurableHam 类似New Rectangle(New Point(e.MarginBounds.Left + 75, e.MarginBounds.Top), printSize)。没测试过。
    • @LarsTech 太棒了,谢谢!使用该代码给了我一些其他尝试的想法,最后这奏效了:If page = 1 Then printsize = New Size(printsize.Width + 75, printsize.Height / 2.5) 得到我想要的。我意识到在 vb.net 中打印似乎很困难,但如果你真的理解它,它会给你很大的控制空间。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多