列表视图打印

时间:2010-07-23 02:07:44

标签: vb.net

我找到了一些使用谷歌进行列表视图打印的代码。我根据自己的需要修改了代码。如果列表查看多个页面,我有一些问题。它不会停止计算我的文档的“生成预览”。如果我按下取消,它会在多个页面中显示数据,但内容相同。

任何建议都会非常感激。

提前致谢

这是代码

Public Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim pd As New PrintDocument
Dim CurrRow As Integer = 0
Dim Ratio As Single = 0
Dim c As ColumnHeader
Dim g As Graphics = e.Graphics
Dim l As Integer = 0 'stores current left  
Dim iCount As Integer
Dim f As Font = lsvToPrint.Font
Dim FontBold As New System.Drawing.Font("Microsoft Sans Serif", 10, FontStyle.Bold)
Dim b As Brush = Brushes.Black
Dim currentY As Integer = 0
Dim maxY As Integer = 0
Dim gap As Integer = 5
Dim lvsi As ListViewItem.ListViewSubItem
Dim colLefts(lsvToPrint.Columns.Count) As Integer
Dim colWidths(lsvToPrint.Columns.Count) As Integer
Dim idx As Integer = 0
Dim ii As Integer
Dim lr As RectangleF
e.HasMorePages = False

'Page Settings  
Dim PSize As Integer = lsvToPrint.Items.Count
Dim PHi As Double
With pd.DefaultPageSettings
  Dim Ps As PaperSize
  PHi = PSize * 20 + 350
  Ps = New PaperSize("Cust", 800, PHi)
  .Margins.Top = 15
  .Margins.Bottom = 20
  .PaperSize = Ps
End With

Dim sfc As New StringFormat
sfc.LineAlignment = StringAlignment.Center
sfc.Alignment = StringAlignment.Center

'Title  
Dim headfont As Font
Dim X1 As Integer
Dim Y As Integer
headfont = New Font("Courier New", 16, FontStyle.Bold)
X1 = pd.DefaultPageSettings.Margins.Left
Y = pd.DefaultPageSettings.Margins.Top + 5
With pd.DefaultPageSettings
  e.Graphics.DrawLine(Pens.Black, 0, Y + 70, e.PageBounds.Width, Y + 70)
End With

'Headings  
currentY = 100
For Each c In lsvToPrint.Columns
  maxY = Math.Max(maxY, g.MeasureString(c.Text, f, c.Width).Height)
  colLefts(idx) = l
  colWidths(idx) = c.Width
  lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY) 
  If lr.Width > 0 Then g.DrawString(c.Text, FontBold, b, lr, sfc)
  l += c.Width
  idx += 1
Next
currentY += maxY + gap
g.DrawLine(Pens.Black, 0, currentY, e.PageBounds.Width, currentY)
currentY += gap
'Rows  
iCount = lsvToPrint.Items.Count - 1
For ii = CurrRow To iCount
  If (currentY + maxY + maxY) > e.PageBounds.Height Then 'jump down another line to see if this line will fit  
    CurrRow = ii - 1
    e.HasMorePages = True

    currentY += maxY + gap

    Exit For 'does next page  

  End If

  l = 0
  maxY = 0
  idx = 0

  For Each lvsi In lsvToPrint.Items(ii).SubItems
    maxY = Math.Max(maxY, g.MeasureString(lvsi.Text, f, colWidths(idx)).Height)

    lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)

    If lsvToPrint.Columns(idx).Text <> "Name" Then
      If lr.Width > 0 Then g.DrawString(lvsi.Text     , f, b, lr, sfc)
    Else
      If lr.Width > 0 Then g.DrawString(lvsi.Text, f, b, lr)
    End If
    idx += 1
  Next
  currentY += maxY + gap
Next
End Sub

1 个答案:

答案 0 :(得分:0)

确保补偿新页面的MaxY高度。在第一页上它很好,但是当它到达下一页时,CurrentY + MaxY可能已经大于页面的高度。