NumberedList.vb
  1. ''
  2. '' This code is part of Document Solutions for PDF demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports GrapeCity.Documents.Pdf
  8. Imports GrapeCity.Documents.Text
  9. Imports GrapeCity.Documents.Drawing
  10.  
  11. '' Demonstrates how paragraphs of text can be rendered as a numbered list in DsPdf.
  12. '' The method of rendering pages of text in this sample is taken from the PaginatedText
  13. '' sample. See also TextRendering.
  14. Public Class NumberedList
  15. '' Encapsulate page layout constants used in the sample:
  16. Private Structure Layout
  17. Public Shared Margin As Single = 72.0F
  18. Public Shared ListOffset As Single = 24.0F
  19. End Structure
  20.  
  21. '' Utility method which pre-pends numbers to all paragraphs in a TextLayout.
  22. Private Sub AddBullets(ByVal g As GcGraphics, ByVal pt As PointF, ByVal tl As TextLayout, ByRef itemNo As Integer)
  23. Dim tlBullet = g.CreateTextLayout()
  24. tlBullet.DefaultFormat.Font = StandardFonts.Times
  25. tlBullet.DefaultFormat.FontSize = 12
  26. For Each line In tl.Lines
  27. If (line.FirstLineInParagraph) Then
  28. tlBullet.Clear()
  29. tlBullet.Append($"{itemNo})")
  30. itemNo += 1
  31. tlBullet.PerformLayout(True)
  32. g.DrawTextLayout(tlBullet, New PointF(pt.X, pt.Y + line.Position + line.LineGap))
  33. End If
  34. Next
  35. End Sub
  36.  
  37. '' Main entry point:
  38. Function CreatePDF(ByVal stream As Stream) As Integer
  39. Dim doc = New GcPdfDocument()
  40. Dim ip = New PointF(Layout.Margin, Layout.Margin)
  41.  
  42. '' Use TextLayout.MarginLeft to reserve space for list numbers/bullets:
  43. Dim tl = New TextLayout(72) With {
  44. .MaxWidth = doc.PageSize.Width - Layout.Margin * 2,
  45. .MaxHeight = doc.PageSize.Height - Layout.Margin * 2,
  46. .ParagraphSpacing = 8,
  47. .MarginLeft = Layout.ListOffset
  48. }
  49. tl.DefaultFormat.Font = StandardFonts.Times
  50.  
  51. '' Add 20 paragraphs of text that will render as a numbered list of 20 items:
  52. tl.Append(Util.LoremIpsum(20, 1, 6))
  53. '' Perform layout:
  54. tl.PerformLayout(True)
  55. '' Use split options to provide widow/orphan control:
  56. Dim tso = New TextSplitOptions(tl)
  57. tso.MinLinesInFirstParagraph = 2
  58. tso.MinLinesInLastParagraph = 2
  59. '' In a loop, split and render the text (see PaginatedText),
  60. '' and add list numbers:
  61. Dim itemNo = 1
  62. While (True)
  63. '' 'rest' will accept the text that did not fit:
  64. Dim rest As TextLayout = Nothing
  65. Dim splitResult = tl.Split(tso, rest)
  66. Dim g = doc.Pages.Add().Graphics
  67. g.DrawTextLayout(tl, ip)
  68. AddBullets(g, ip, tl, itemNo)
  69. If splitResult <> SplitResult.Split Then
  70. Exit While
  71. End If
  72. tl = rest
  73. End While
  74. ''
  75. '' Done:
  76. doc.Save(stream)
  77. Return doc.Pages.Count
  78. End Function
  79. End Class
  80.