ArabicText.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 System.Collections.Generic
  8. Imports GrapeCity.Documents.Drawing
  9. Imports GrapeCity.Documents.Pdf
  10. Imports GrapeCity.Documents.Text
  11. Imports GCTEXT = GrapeCity.Documents.Text
  12. Imports GCDRAW = GrapeCity.Documents.Drawing
  13.  
  14. '' Renders Arabic text using a columnar layout.
  15. '' See also MultiLang and JapaneseColumns.
  16. Public Class ArabicText
  17. Const text = "العربية أكبر لغات المجموعة السامية من حيث عدد المتحدثين، وإحدى أكثر اللغات انتشارًا في العالم، يتحدثها أكثر من 422 مليون نسمة،1 ويتوزع متحدثوها في المنطقة المعروفة باسم الوطن العربي، بالإضافة إلى العديد من المناطق الأخرى المجاورة كالأحواز وتركيا وتشاد ومالي والسنغالوارتيرياوللغة العربية أهمية قصوى لدى أتباع الديانة الإسلامية، فهي لغة مصدري التشريع الأساسيين في الإسلام: القرآن، والأحاديث النبوية المروية عن النبي محمد، ولا تتم الصلاة في الإسلام (وعبادات أخرى) إلا بإتقان بعض من كلمات هذه اللغة. والعربية هي أيضًا لغة طقسية رئيسية لدى عدد من الكنائس المسيحية في العالم العربي، كما كتبت بها الكثير من أهم الأعمال الدينية والفكرية اليهودية في العصور الوسطى. وأثّر انتشار الإسلام، وتأسيسه دولًا، أرتفعت مكانة اللغة العربية، وأصبحت لغة السياسة والعلم والأدب لقرون طويلة في الأراضي التي حكمها المسلمون، وأثرت العربية، تأثيرًا مباشرًا أو غير مباشر على كثير من اللغات الأخرى في العالم الإسلامي، كالتركية والفارسية والأرديةوالالبانية واللغات الأفريقية الاخرى واللغات الأوروبية مثل الروسية والإنجليزية والفرنسية والأسبانية والايطالية والألمانية.كما انها تدرس بشكل رسمى او غير رسمى في الدول الاسلامية والدول الأفريقية المحادية للوطن العربى."
  18.  
  19. Function CreatePDF(ByVal stream As Stream) As Integer
  20.  
  21. Using reds As GCDRAW.Image = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "reds.jpg")),
  22. firth As GCDRAW.Image = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "firth.jpg")),
  23. purples As GCDRAW.Image = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "purples.jpg"))
  24. Dim times = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf"))
  25. Dim ia = New ImageAlign(ImageAlignHorz.Left, ImageAlignVert.Top, True, True, True, False, False)
  26.  
  27. Dim doc = New GcPdfDocument()
  28.  
  29. '' The TextLayout that will hold and render the text:
  30. Dim tl = New TextLayout(72) With {
  31. .FirstLineIndent = 18,
  32. .ParagraphSpacing = 6,
  33. .TextAlignment = TextAlignment.Justified,
  34. .RightToLeft = True
  35. }
  36. Dim tf = New TextFormat() With {.Font = times, .FontSize = 12}
  37. '' Repeat test text to fill a few pages:
  38. For i = 1 To 12
  39. tl.Append(text, tf)
  40. tl.AppendLine()
  41. Next
  42.  
  43. '' Layout text in 3 columns:
  44. '' (The logic/code in this sample is identical to JapaneseColumns
  45. Const NCOLS = 3
  46. Dim margin = 36.0F
  47. Dim gap = 18.0F
  48. Dim page = doc.NewPage()
  49. page.Landscape = True
  50. Dim colWid = (page.Size.Width - margin * 2 - gap * (NCOLS - 1)) / NCOLS
  51. tl.MaxWidth = page.Size.Width
  52. tl.MaxHeight = page.Size.Height
  53. tl.MarginTop = margin
  54. tl.MarginBottom = margin
  55. tl.MarginRight = margin
  56. tl.MarginLeft = margin + (colWid + gap) * (NCOLS - 1)
  57. '' We can specify arbitrary rectangles for the text to flow around.
  58. '' In this case, we add 3 areas to draw some images:
  59. tl.ObjectRects = New List(Of ObjectRect)() From {
  60. New ObjectRect(page.Size.Width - margin - 240, margin, 240, 240),
  61. New ObjectRect(margin + 100, margin + 60, 133, 100),
  62. New ObjectRect(margin, page.Size.Height - margin - 300, 300, 300)
  63. }
  64. '' Convert object rects to image areas, adjust to provide nice looking padding:
  65. Dim rReds = tl.ObjectRects(0).ToRectangleF()
  66. rReds.Inflate(-4, -3)
  67. Dim rFirth = tl.ObjectRects(1).ToRectangleF()
  68. rFirth.Inflate(-4, -3)
  69. Dim rPurples = tl.ObjectRects(2).ToRectangleF()
  70. rPurples.Inflate(-4, -3)
  71. page.Graphics.DrawImage(reds, rReds, Nothing, ia)
  72. page.Graphics.DrawImage(firth, rFirth, Nothing, ia)
  73. page.Graphics.DrawImage(purples, rPurples, Nothing, ia)
  74.  
  75. '' THE call: it calculates the glyphs needed to draw the text, and lays it out:
  76. tl.PerformLayout(True)
  77.  
  78. Dim done = False
  79. '' Loop while there is still text to render:
  80. While Not done
  81. For col = 1 To NCOLS
  82.  
  83. Dim nextcol = If(col < NCOLS, col, 0)
  84. '' TextSplitOptions tell TextLayout.Split() how to layout the remaining text.
  85. '' In this case we advance from column to column by updating top and bottom margins:
  86. Dim tso = New TextSplitOptions(tl) With {
  87. .RestMarginRight = margin + (colWid + gap) * nextcol,
  88. .RestMarginLeft = margin + (colWid + gap) * (NCOLS - 1 - nextcol)
  89. }
  90. Dim rest As TextLayout = Nothing
  91. Dim split = tl.Split(tso, rest)
  92. page.Graphics.DrawTextLayout(tl, PointF.Empty)
  93. If split <> SplitResult.Split Then
  94. done = True
  95. Exit For
  96. End If
  97. tl = rest
  98. Next
  99. If Not done Then
  100. page = doc.NewPage()
  101. page.Landscape = True
  102. '' We only want to render images on the first page, so clear ObjectRects:
  103. If Not tl.ObjectRects Is Nothing Then
  104. tl.ObjectRects = Nothing
  105. '' We need to redo the layout, but no need to recalculate the glyphs:
  106. tl.PerformLayout(False)
  107. End If
  108. End If
  109. End While
  110. ''
  111. '' Done:
  112. doc.Save(stream)
  113. Return doc.Pages.Count
  114. End Using
  115. End Function
  116. End Class
  117.