LargeDocument3.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 GCTEXT = GrapeCity.Documents.Text
  10. Imports GCDRAW = GrapeCity.Documents.Drawing
  11.  
  12. '' Generates a large PDF using a single TextLayout.
  13. '' Unlike other 'large document' samples (StartEndDoc, LargeDocument2),
  14. '' this sample actually paginates the whole document as a single sequence of paragraphs,
  15. '' optimally and correctly filling each page with text. Split options such as
  16. '' keeping at least 2 lines of paragraph on each page are also supported.
  17. '' Due to the extra work, this sample takes significantly more time to complete.
  18. Public Class LargeDocument3
  19. Function CreatePDF(ByVal stream As Stream) As Integer
  20. '' Number of pages to generate:
  21. Const N = Util.LargeDocumentIterations
  22. Dim start = Util.TimeNow()
  23. Dim doc = New GcPdfDocument()
  24. '' Prep a TextLayout to hold/format the text:
  25. Dim tl = New TextLayout(72) With {
  26. .MaxWidth = doc.PageSize.Width,
  27. .MaxHeight = doc.PageSize.Height,
  28. .MarginAll = 72,
  29. .FirstLineIndent = 36
  30. }
  31. tl.DefaultFormat.Font = StandardFonts.Times
  32. tl.DefaultFormat.FontSize = 12
  33. '' Generate the document:
  34. For paraIdx = 1 To N
  35. tl.AppendLine(Util.LoremIpsum(1))
  36. Next
  37. '' Split and render TextLayout as shown in the PaginatedText sample:
  38. Dim tso = New TextSplitOptions(tl) With {
  39. .MinLinesInFirstParagraph = 2,
  40. .MinLinesInLastParagraph = 2
  41. }
  42. tl.PerformLayout(True)
  43. '' The loop splitting and rendering the layout:
  44. Dim tls = New TextLayoutSplitter(tl)
  45.  
  46. Dim tlPage = tls.Split(tso)
  47. While Not tlPage Is Nothing
  48. doc.NewPage().Graphics.DrawTextLayout(tlPage, PointF.Empty)
  49. tlPage = tls.Split(tso)
  50. End While
  51.  
  52. tl.Clear()
  53. '' Insert a title page (cannot be done if using StartDoc/EndDoc):
  54. tl.FirstLineIndent = 0
  55. Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "yumin.ttf"))
  56. Dim tf0 = New TextFormat() With {.FontSize = 24, .FontBold = True, .Font = fnt}
  57. tl.Append(String.Format("Large Document" + vbLf + "{0} Paragraphs of Lorem Ipsum" + vbLf + vbLf, N), tf0)
  58. Dim tf1 = New TextFormat(tf0) With {.FontSize = 14, .FontItalic = True}
  59. tl.Append(String.Format("Generated on {0} in {1:m\m\ s\s\ fff\m\s}.", Util.TimeNow().ToString("R"), Util.TimeNow() - start), tf1)
  60. tl.TextAlignment = TextAlignment.Center
  61. tl.PerformLayout(True)
  62. doc.Pages.Insert(0).Graphics.DrawTextLayout(tl, PointF.Empty)
  63. '' Done:
  64. doc.Save(stream)
  65. Return doc.Pages.Count
  66. End Function
  67. End Class
  68.