VerticalTextJP.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. Imports GCTEXT = GrapeCity.Documents.Text
  11. Imports GCDRAW = GrapeCity.Documents.Drawing
  12.  
  13. '' Draws vertical right-to-left Japanese text using a layout with horizontal columns.
  14. '' See also ArabicColumns, MultiLang and VerticalText.
  15. Public Class VerticalTextJP
  16.  
  17. Const text = "日本語(にほんご、にっぽんご)は、主として、日本列島で使用されてきた言語である。日本手話を母語とする者などを除いて、ほぼ全ての日本在住者は日本語を第一言語とする。日本国は法令上、公用語を明記していないが、事実上の公用語となっており、学校教育の「国語」で教えられる。使用者は、日本国内を主として約\uFF11億\uFF13千万人。日本語の文法体系や音韻体系を反映する手話として日本語対応手話がある。"
  18.  
  19. Function CreatePDF(ByVal stream As Stream) As Integer
  20. Using clouds As GCDRAW.Image = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "clouds.jpg")),
  21. firth As GCDRAW.Image = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "firth.jpg")),
  22. lavender As GCDRAW.Image = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "lavender.jpg"))
  23. Dim yumin = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "yumin.ttf"))
  24. Dim ia = New ImageAlign(ImageAlignHorz.Left, ImageAlignVert.Top, True, True, True, False, False)
  25.  
  26. Dim doc = New GcPdfDocument()
  27.  
  28. '' The TextLayout that will hold and render the text:
  29. Dim tl = New TextLayout(72)
  30. tl.FirstLineIndent = 18
  31. tl.ParagraphSpacing = 6
  32. tl.FlowDirection = FlowDirection.VerticalRightToLeft
  33. tl.TextAlignment = TextAlignment.Justified
  34. Dim tf = New TextFormat() With {.Font = yumin, .FontSize = 12}
  35. '' Repeat test text to fill a few pages:
  36. For i = 0 To 25
  37. tl.Append(text, tf)
  38. tl.AppendLine()
  39. Next
  40.  
  41. '' Layout text in 4 horizontal columns:
  42. '' (The logic/code in this sample is identical to ArabicColumns):
  43. Const NCOLS = 4
  44. Dim margin = 36.0F
  45. Dim gap = 18.0F
  46. Dim page = doc.NewPage()
  47. page.Landscape = True
  48. Dim colHeight = (page.Size.Height - margin * 2 - gap * (NCOLS - 1)) / NCOLS
  49. tl.MaxWidth = page.Size.Width
  50. tl.MaxHeight = page.Size.Height
  51. tl.MarginLeft = margin
  52. tl.MarginRight = margin
  53. tl.MarginTop = margin
  54. tl.MarginBottom = margin + (colHeight + gap) * (NCOLS - 1)
  55. '' We can specify arbitrary rectangles for the text to flow around.
  56. '' In this case, we add 3 areas to draw some images:
  57. tl.ObjectRects = New List(Of ObjectRect) From {
  58. New ObjectRect(page.Size.Width - margin - 267, margin, 267, 200),
  59. New ObjectRect(margin + 100, margin + 60, 133, 100),
  60. New ObjectRect(margin, page.Size.Height - margin - 301, 200, 301)
  61. }
  62. '' Convert object rects to image areas, adjust to provide nice looking padding:
  63. Dim rClouds = tl.ObjectRects(0).ToRectangleF()
  64. rClouds.Inflate(-4, -3)
  65. Dim rFirth = tl.ObjectRects(1).ToRectangleF()
  66. rFirth.Inflate(-4, -3)
  67. Dim rLavender = tl.ObjectRects(2).ToRectangleF()
  68. rLavender.Inflate(-4, -3)
  69. page.Graphics.DrawImage(clouds, rClouds, Nothing, ia)
  70. page.Graphics.DrawImage(firth, rFirth, Nothing, ia)
  71. page.Graphics.DrawImage(lavender, rLavender, Nothing, ia)
  72.  
  73. '' THE call: it calculates the glyphs needed to draw the text, and lays it out:
  74. tl.PerformLayout(True)
  75.  
  76. '' Loop while there is still text to render:
  77. Dim done = False
  78. While Not done
  79. For col = 1 To NCOLS
  80. Dim nextcol = If(col < NCOLS, col, 0)
  81. '' TextSplitOptions tell TextLayout.Split() how to layout the remaining text.
  82. '' In this case we advance from column to column by updating top and bottom margins:
  83. Dim tso = New TextSplitOptions(tl) With {
  84. .RestMarginTop = margin + (colHeight + gap) * nextcol,
  85. .RestMarginBottom = margin + (colHeight + gap) * (NCOLS - 1 - nextcol)
  86. }
  87. Dim rest As TextLayout = Nothing
  88. Dim split = tl.Split(tso, rest)
  89. page.Graphics.DrawTextLayout(tl, PointF.Empty)
  90. If split <> SplitResult.Split Then
  91. done = True
  92. Exit For
  93. End If
  94. tl = rest
  95. Next
  96. If Not done Then
  97. page = doc.NewPage()
  98. page.Landscape = True
  99. '' We only want to render images on the first page, so clear ObjectRect:
  100. If tl.ObjectRects IsNot Nothing Then
  101. tl.ObjectRects = Nothing
  102. '' We need to redo the layout, but no need to recalculate the glyphs:
  103. tl.PerformLayout(False)
  104. End If
  105. End If
  106. End While
  107. ''
  108. '' Done:
  109. doc.Save(stream)
  110. Return doc.Pages.Count
  111. End Using
  112. End Function
  113. End Class
  114.