ArabicText.vb
'' '' This code is part of Document Solutions for PDF demos. '' Copyright (c) MESCIUS inc. All rights reserved. '' Imports System.IO Imports System.Drawing Imports System.Collections.Generic Imports GrapeCity.Documents.Drawing Imports GrapeCity.Documents.Pdf Imports GrapeCity.Documents.Text Imports GCTEXT = GrapeCity.Documents.Text Imports GCDRAW = GrapeCity.Documents.Drawing '' Renders Arabic text using a columnar layout. '' See also MultiLang and JapaneseColumns. Public Class ArabicText Const text = "العربية أكبر لغات المجموعة السامية من حيث عدد المتحدثين، وإحدى أكثر اللغات انتشارًا في العالم، يتحدثها أكثر من 422 مليون نسمة،1 ويتوزع متحدثوها في المنطقة المعروفة باسم الوطن العربي، بالإضافة إلى العديد من المناطق الأخرى المجاورة كالأحواز وتركيا وتشاد ومالي والسنغالوارتيرياوللغة العربية أهمية قصوى لدى أتباع الديانة الإسلامية، فهي لغة مصدري التشريع الأساسيين في الإسلام: القرآن، والأحاديث النبوية المروية عن النبي محمد، ولا تتم الصلاة في الإسلام (وعبادات أخرى) إلا بإتقان بعض من كلمات هذه اللغة. والعربية هي أيضًا لغة طقسية رئيسية لدى عدد من الكنائس المسيحية في العالم العربي، كما كتبت بها الكثير من أهم الأعمال الدينية والفكرية اليهودية في العصور الوسطى. وأثّر انتشار الإسلام، وتأسيسه دولًا، أرتفعت مكانة اللغة العربية، وأصبحت لغة السياسة والعلم والأدب لقرون طويلة في الأراضي التي حكمها المسلمون، وأثرت العربية، تأثيرًا مباشرًا أو غير مباشر على كثير من اللغات الأخرى في العالم الإسلامي، كالتركية والفارسية والأرديةوالالبانية واللغات الأفريقية الاخرى واللغات الأوروبية مثل الروسية والإنجليزية والفرنسية والأسبانية والايطالية والألمانية.كما انها تدرس بشكل رسمى او غير رسمى في الدول الاسلامية والدول الأفريقية المحادية للوطن العربى." Function CreatePDF(ByVal stream As Stream) As Integer Using reds As GCDRAW.Image = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "reds.jpg")), firth As GCDRAW.Image = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "firth.jpg")), purples As GCDRAW.Image = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "purples.jpg")) Dim fnt As GCTEXT.Font Dim fpath = Path.Combine("Resources", "Fonts", "times.ttf") If File.Exists(fpath) Then fnt = GCTEXT.Font.FromFile(fpath) Else fnt = FontCollection.SystemFonts.FindFamilyName("Times New Roman") End If Dim ia = New ImageAlign(ImageAlignHorz.Left, ImageAlignVert.Top, True, True, True, False, False) Dim doc = New GcPdfDocument() '' The TextLayout that will hold and render the text: Dim tl = New TextLayout(72) With { .FirstLineIndent = 18, .ParagraphSpacing = 6, .TextAlignment = TextAlignment.Justified, .RightToLeft = True } Dim tf = New TextFormat() With {.Font = fnt, .FontSize = 12} '' Repeat test text to fill a few pages: For i = 1 To 12 tl.Append(text, tf) tl.AppendLine() Next '' Layout text in 3 columns: '' (The logic/code in this sample is identical to JapaneseColumns Const NCOLS = 3 Dim margin = 36.0F Dim gap = 18.0F Dim page = doc.NewPage() page.Landscape = True Dim colWid = (page.Size.Width - margin * 2 - gap * (NCOLS - 1)) / NCOLS tl.MaxWidth = page.Size.Width tl.MaxHeight = page.Size.Height tl.MarginTop = margin tl.MarginBottom = margin tl.MarginRight = margin tl.MarginLeft = margin + (colWid + gap) * (NCOLS - 1) '' We can specify arbitrary rectangles for the text to flow around. '' In this case, we add 3 areas to draw some images: tl.ObjectRects = New List(Of ObjectRect)() From { New ObjectRect(page.Size.Width - margin - 240, margin, 240, 240), New ObjectRect(margin + 100, margin + 60, 133, 100), New ObjectRect(margin, page.Size.Height - margin - 300, 300, 300) } '' Convert object rects to image areas, adjust to provide nice looking padding: Dim rReds = tl.ObjectRects(0).ToRectangleF() rReds.Inflate(-4, -3) Dim rFirth = tl.ObjectRects(1).ToRectangleF() rFirth.Inflate(-4, -3) Dim rPurples = tl.ObjectRects(2).ToRectangleF() rPurples.Inflate(-4, -3) page.Graphics.DrawImage(reds, rReds, Nothing, ia) page.Graphics.DrawImage(firth, rFirth, Nothing, ia) page.Graphics.DrawImage(purples, rPurples, Nothing, ia) '' THE call: it calculates the glyphs needed to draw the text, and lays it out: tl.PerformLayout(True) Dim done = False '' Loop while there is still text to render: While Not done For col = 1 To NCOLS Dim nextcol = If(col < NCOLS, col, 0) '' TextSplitOptions tell TextLayout.Split() how to layout the remaining text. '' In this case we advance from column to column by updating top and bottom margins: Dim tso = New TextSplitOptions(tl) With { .RestMarginRight = margin + (colWid + gap) * nextcol, .RestMarginLeft = margin + (colWid + gap) * (NCOLS - 1 - nextcol) } Dim rest As TextLayout = Nothing Dim split = tl.Split(tso, rest) page.Graphics.DrawTextLayout(tl, PointF.Empty) If split <> SplitResult.Split Then done = True Exit For End If tl = rest Next If Not done Then page = doc.NewPage() page.Landscape = True '' We only want to render images on the first page, so clear ObjectRects: If Not tl.ObjectRects Is Nothing Then tl.ObjectRects = Nothing '' We need to redo the layout, but no need to recalculate the glyphs: tl.PerformLayout(False) End If End If End While '' '' Done: doc.Save(stream) Return doc.Pages.Count End Using End Function End Class