KeepWithNext.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. '' This sample shows how to prevent a page break between a paragraph
  12. '' and the next one when splitting a TextLayout.
  13. '' Splitting of text in this sample is similar to that in PaginatedText,
  14. '' see comments in PaginatedText for more info on text handling.
  15. Public Class KeepWithNext
  16. Function CreatePDF(ByVal stream As Stream) As Integer
  17. Const NPAR = 40
  18. Dim doc = New GcPdfDocument()
  19. Dim tl = New TextLayout(72) With
  20. {
  21. .FirstLineIndent = 72 / 2,
  22. .MaxWidth = doc.PageSize.Width,
  23. .MaxHeight = doc.PageSize.Height,
  24. .MarginAll = 72
  25. }
  26. tl.DefaultFormat.Font = StandardFonts.Times
  27. tl.DefaultFormat.FontSize = 12
  28. '' Text format for paragraphs kept together with next one:
  29. Dim tf = New TextFormat(tl.DefaultFormat) With
  30. {
  31. .FontSize = tl.DefaultFormat.FontSize + 2,
  32. .FontBold = True
  33. }
  34. '' We add a number of random 'lorem ipsum' paragraphs to this document,
  35. '' adding a 'caption' before each paragraph which is kept together
  36. '' with the following 'lorem ipsum' paragraph:
  37. For i = 0 To NPAR - 1
  38. '' 'Caption' kept together with the next paragraph:
  39. tl.Append("Caption kept together with the next paragraph. No page break after this.", tf)
  40. '' AppendParagraphBreak adds a paragraph break but prevents a page break between the two paragraphs:
  41. tl.AppendParagraphBreak()
  42. '' Random paragraph after 'caption':
  43. tl.Append(Util.LoremIpsum(1))
  44. Next
  45. tl.PerformLayout(True)
  46. '' We force all paragraph lines to stay on the same page,
  47. '' this makes it more obvious that 'caption' and following paragraph
  48. '' are kept on the same page:
  49. Dim tso = New TextSplitOptions(tl) With
  50. {
  51. .KeepParagraphLinesTogether = True
  52. }
  53. '' In a loop, split and render the text:
  54. While (True)
  55. '' 'rest' will accept the text that did not fit:
  56. Dim rest As TextLayout = Nothing
  57. Dim splitResult = tl.Split(tso, rest)
  58. doc.Pages.Add().Graphics.DrawTextLayout(tl, PointF.Empty)
  59. If (splitResult <> SplitResult.Split) Then
  60. Exit While
  61. End If
  62. tl = rest
  63. End While
  64. '' Done:
  65. doc.Save(stream)
  66. Return doc.Pages.Count
  67. End Function
  68. End Class
  69.