📄 printtableform.vb
字号:
BookItem.SubItems.Add("P. D. Ouspensky")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0472065211"
BookItem.SubItems.Add("Simulacra and Simulation")
BookItem.SubItems.Add("Jean Baudrillard")
BookItem.SubItems.Add("Sheila Faria Glaser")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0060637633"
BookItem.SubItems.Add("Basic Writings : From Being and Time (1927 to the Task of Thinking)")
BookItem.SubItems.Add("Martin Heidegger")
BookItem.SubItems.Add("David Farrell Krell")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "1566632625"
BookItem.SubItems.Add("Locke in 90 Minutes")
BookItem.SubItems.Add("Paul Strathern")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0860917045"
BookItem.SubItems.Add("Minima Moralia : Reflections from Damaged Life")
BookItem.SubItems.Add("Theodor Wiesengrund Adorno")
BookItem.SubItems.Add("E.F.Jephcott")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "1890318663"
BookItem.SubItems.Add("The Defamation of Pius XII")
BookItem.SubItems.Add("Ralph M. McInerny")
BookItem.SubItems.Add("Ralph McLnerny")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0156445085"
BookItem.SubItems.Add("In Search of the Miraculous : Fragments of an Unknown Teaching ")
BookItem.SubItems.Add("P. D. Ouspensky")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0472065211"
BookItem.SubItems.Add("Simulacra and Simulation")
BookItem.SubItems.Add("Jean Baudrillard")
BookItem.SubItems.Add("Sheila Faria Glaser")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0060637633"
BookItem.SubItems.Add("Basic Writings : From Being and Time (1927 to the Task of Thinking)")
BookItem.SubItems.Add("Martin Heidegger")
BookItem.SubItems.Add("David Farrell Krell")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "1566632625"
BookItem.SubItems.Add("Locke in 90 Minutes")
BookItem.SubItems.Add("Paul Strathern")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0860917045"
BookItem.SubItems.Add("Minima Moralia : Reflections from Damaged Life")
BookItem.SubItems.Add("Theodor Wiesengrund Adorno")
BookItem.SubItems.Add("E.F.Jephcott")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "1890318663"
BookItem.SubItems.Add("The Defamation of Pius XII")
BookItem.SubItems.Add("Ralph M. McInerny")
BookItem.SubItems.Add("Ralph McLnerny")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0156445085"
BookItem.SubItems.Add("In Search of the Miraculous : Fragments of an Unknown Teaching ")
BookItem.SubItems.Add("P. D. Ouspensky")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0472065211"
BookItem.SubItems.Add("Simulacra and Simulation")
BookItem.SubItems.Add("Jean Baudrillard")
BookItem.SubItems.Add("Sheila Faria Glaser")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0060637633"
BookItem.SubItems.Add("Basic Writings : From Being and Time (1927 to the Task of Thinking)")
BookItem.SubItems.Add("Martin Heidegger")
BookItem.SubItems.Add("David Farrell Krell")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "1566632625"
BookItem.SubItems.Add("Locke in 90 Minutes")
BookItem.SubItems.Add("Paul Strathern")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0860917045"
BookItem.SubItems.Add("Minima Moralia : Reflections from Damaged Life")
BookItem.SubItems.Add("Theodor Wiesengrund Adorno")
BookItem.SubItems.Add("E.F.Jephcott")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "1890318663"
BookItem.SubItems.Add("The Defamation of Pius XII")
BookItem.SubItems.Add("Ralph M. McInerny")
BookItem.SubItems.Add("Ralph McLnerny")
ListView1.Items.Add(BookItem)
End Sub
Dim tableFont, headerFont, titleFont As Font
Dim X1, X2, X3 As Integer
Dim W1, W2, W3 As Integer
Dim Y As Integer
Dim pageWidth, pageHeight As Integer
Dim itm As Integer
Dim pageNo As Integer
Dim lineHeight As Single
Dim ReportTitle As String
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings
If PageSetupDialog1.ShowDialog() = DialogResult.OK Then
PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
End If
tableFont = New Font("Arial", 10)
headerFont = New Font("Arial", 12, FontStyle.Bold)
titleFont = New Font("Arial", 18, FontStyle.Bold)
X1 = PrintDocument1.DefaultPageSettings.Margins.Left
With PrintDocument1.DefaultPageSettings
pageWidth = .PaperSize.Width - _
.Margins.Left - _
.Margins.Right
pageHeight = .PaperSize.Height - _
.Margins.Top - _
.Margins.Bottom
End With
X2 = X1 + 100
X3 = CInt(X2 + pageWidth * 0.6)
W1 = X2 - X1
W2 = X3 - X2
W3 = pageWidth - W1 - W2 ' X3
If PrintDocument1.DefaultPageSettings.Landscape Then
With PrintDocument1.DefaultPageSettings
pageWidth = .PaperSize.Height - _
.Margins.Left - _
.Margins.Right
pageHeight = .PaperSize.Width - _
.Margins.Top - _
.Margins.Bottom
End With
X2 = X1 + 100
X3 = CInt(X2 + pageWidth * 0.6)
W1 = X2 - X1
W2 = X3 - X2
W3 = pageWidth - W1 - W2 ' X3
End If
' X1, X2 and X3 are the X coordinates of the left edge of the three columns
' W1, W2 and W3 are the widths of the three columns
lineHeight = tableFont.GetHeight()
itm = 0
pageNo = 0
ReportTitle = "Sample Report Title"
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
' The PrintTable project demonstrates the basics of printing tabular data.
' You can enhance the code in many ways. The following few statements print
' a title on each page (to the left) and the current page number (to the right)
'
pageNo = pageNo + 1
'print report's title
printReportTitle(e.Graphics)
' Print Page Number
PrintPageNumber(e.Graphics)
' Set up table's columns
Y = PrintDocument1.DefaultPageSettings.Margins.Top + 50
e.Graphics.DrawString("ISBN", headerFont, Brushes.Black, X1, Y)
e.Graphics.DrawString("Title", headerFont, Brushes.Black, X2, Y)
e.Graphics.DrawString("Author(s)", headerFont, Brushes.Black, X3, Y)
Y = Y + 30
' this is the Y coordinate of the horizontal line below the column headers
' the vertical lines between the columns start at this coordinate
Dim YLine As Integer = Y
e.Graphics.DrawLine(Pens.Silver, PrintDocument1.DefaultPageSettings.Margins.Left, YLine, pageWidth + PrintDocument1.DefaultPageSettings.Margins.Left, YLine)
While itm < ListView1.Items.Count
Dim str As String
str = ListView1.Items(itm).Text
e.Graphics.DrawString(str, tableFont, Brushes.Black, X1, Y)
str = ListView1.Items(itm).SubItems(1).Text
Dim R As New RectangleF(X2, Y, W2, 80)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R)
Dim lines, cols As Integer
e.Graphics.MeasureString(str, tableFont, New SizeF(W2, 50), New StringFormat(), cols, lines)
Dim subitm As Integer, Yc As Integer
Yc = Y
For subitm = 2 To ListView1.Items(itm).SubItems.Count - 1
str = ListView1.Items(itm).SubItems(subitm).Text
e.Graphics.DrawString(str, tableFont, Brushes.Black, New RectangleF(X3, Yc, W3, lineHeight))
Yc = Yc + tableFont.Height + 2
Next
Y = Y + lines * tableFont.Height + 5
Y = Math.Max(Y, Yc)
With PrintDocument1.DefaultPageSettings
e.Graphics.DrawLine(Pens.Silver, .Margins.Left, Y, pageWidth + .Margins.Left, Y)
If Y > 0.95 * (pageHeight + .Margins.Top) Then
e.HasMorePages = True
DrawVerticalLines(e.Graphics, YLine, Y)
Exit Sub
End If
End With
itm = itm + 1
End While
e.HasMorePages = False
DrawVerticalLines(e.Graphics, YLine, Y)
End Sub
Sub DrawVerticalLines(ByVal G As Graphics, ByVal Y1 As Integer, ByVal Y2 As Integer)
G.DrawLine(Pens.Silver, X1, Y1, X1, Y2)
G.DrawLine(Pens.Silver, X2, Y1, X2, Y2)
G.DrawLine(Pens.Silver, X3, Y1, X3, Y2)
G.DrawLine(Pens.Silver, X3 + W3, Y1, X3 + W3, Y2)
End Sub
Sub PrintPageNumber(ByVal G As Graphics)
Dim Xt, Yt As Integer
Xt = PrintDocument1.DefaultPageSettings.Margins.Left
If PrintDocument1.DefaultPageSettings.Landscape Then
Yt = PrintDocument1.DefaultPageSettings.PaperSize.Width - PrintDocument1.DefaultPageSettings.Margins.Bottom + 10
Else
Yt = PrintDocument1.DefaultPageSettings.PaperSize.Height - PrintDocument1.DefaultPageSettings.Margins.Bottom + 10
End If
G.DrawString("Page " & pageNo, tableFont, Brushes.Black, Xt, Yt)
End Sub
Sub printReportTitle(ByVal G As Graphics)
Dim Xt, Yt As Single
Xt = PrintDocument1.DefaultPageSettings.Margins.Left
Yt = PrintDocument1.DefaultPageSettings.Margins.Top
G.DrawString(ReportTitle, titleFont, Brushes.Black, Xt, Yt)
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -