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 fnt As GCTEXT.Font
  25. Dim fpath = Path.Combine("Resources", "Fonts", "times.ttf")
  26. If File.Exists(fpath) Then
  27. fnt = GCTEXT.Font.FromFile(fpath)
  28. Else
  29. fnt = FontCollection.SystemFonts.FindFamilyName("Times New Roman")
  30. End If
  31. Dim ia = New ImageAlign(ImageAlignHorz.Left, ImageAlignVert.Top, True, True, True, False, False)
  32. Dim doc = New GcPdfDocument()
  33.  
  34. '' The TextLayout that will hold and render the text:
  35. Dim tl = New TextLayout(72) With {
  36. .FirstLineIndent = 18,
  37. .ParagraphSpacing = 6,
  38. .TextAlignment = TextAlignment.Justified,
  39. .RightToLeft = True
  40. }
  41. Dim tf = New TextFormat() With {.Font = fnt, .FontSize = 12}
  42. '' Repeat test text to fill a few pages:
  43. For i = 1 To 12
  44. tl.Append(text, tf)
  45. tl.AppendLine()
  46. Next
  47.  
  48. '' Layout text in 3 columns:
  49. '' (The logic/code in this sample is identical to JapaneseColumns
  50. Const NCOLS = 3
  51. Dim margin = 36.0F
  52. Dim gap = 18.0F
  53. Dim page = doc.NewPage()
  54. page.Landscape = True
  55. Dim colWid = (page.Size.Width - margin * 2 - gap * (NCOLS - 1)) / NCOLS
  56. tl.MaxWidth = page.Size.Width
  57. tl.MaxHeight = page.Size.Height
  58. tl.MarginTop = margin
  59. tl.MarginBottom = margin
  60. tl.MarginRight = margin
  61. tl.MarginLeft = margin + (colWid + gap) * (NCOLS - 1)
  62. '' We can specify arbitrary rectangles for the text to flow around.
  63. '' In this case, we add 3 areas to draw some images:
  64. tl.ObjectRects = New List(Of ObjectRect)() From {
  65. New ObjectRect(page.Size.Width - margin - 240, margin, 240, 240),
  66. New ObjectRect(margin + 100, margin + 60, 133, 100),
  67. New ObjectRect(margin, page.Size.Height - margin - 300, 300, 300)
  68. }
  69. '' Convert object rects to image areas, adjust to provide nice looking padding:
  70. Dim rReds = tl.ObjectRects(0).ToRectangleF()
  71. rReds.Inflate(-4, -3)
  72. Dim rFirth = tl.ObjectRects(1).ToRectangleF()
  73. rFirth.Inflate(-4, -3)
  74. Dim rPurples = tl.ObjectRects(2).ToRectangleF()
  75. rPurples.Inflate(-4, -3)
  76. page.Graphics.DrawImage(reds, rReds, Nothing, ia)
  77. page.Graphics.DrawImage(firth, rFirth, Nothing, ia)
  78. page.Graphics.DrawImage(purples, rPurples, Nothing, ia)
  79.  
  80. '' THE call: it calculates the glyphs needed to draw the text, and lays it out:
  81. tl.PerformLayout(True)
  82.  
  83. Dim done = False
  84. '' Loop while there is still text to render:
  85. While Not done
  86. For col = 1 To NCOLS
  87.  
  88. Dim nextcol = If(col < NCOLS, col, 0)
  89. '' TextSplitOptions tell TextLayout.Split() how to layout the remaining text.
  90. '' In this case we advance from column to column by updating top and bottom margins:
  91. Dim tso = New TextSplitOptions(tl) With {
  92. .RestMarginRight = margin + (colWid + gap) * nextcol,
  93. .RestMarginLeft = margin + (colWid + gap) * (NCOLS - 1 - nextcol)
  94. }
  95. Dim rest As TextLayout = Nothing
  96. Dim split = tl.Split(tso, rest)
  97. page.Graphics.DrawTextLayout(tl, PointF.Empty)
  98. If split <> SplitResult.Split Then
  99. done = True
  100. Exit For
  101. End If
  102. tl = rest
  103. Next
  104. If Not done Then
  105. page = doc.NewPage()
  106. page.Landscape = True
  107. '' We only want to render images on the first page, so clear ObjectRects:
  108. If Not tl.ObjectRects Is Nothing Then
  109. tl.ObjectRects = Nothing
  110. '' We need to redo the layout, but no need to recalculate the glyphs:
  111. tl.PerformLayout(False)
  112. End If
  113. End If
  114. End While
  115. ''
  116. '' Done:
  117. doc.Save(stream)
  118. Return doc.Pages.Count
  119. End Using
  120. End Function
  121. End Class
  122.