2010-07-23 49 views
0

我发现了一些使用谷歌listview打印的代码。我修改了我的需求的代码库。如果列表查看多个页面,我遇到一些问题。它不会停止计算我的文档的“生成预览”。如果我按取消,则显示的数据是多页但内容相同。listview printing

任何建议将不胜感激。

在此先感谢

下面是代码

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 

回答

0

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