Hyphenation.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 GrapeCity.Documents.Pdf
- Imports GrapeCity.Documents.Text
- Imports GrapeCity.Documents.Drawing
-
- '' This sample shows how to hyphenate text that contains soft hyphen characters (0x00AD).
- '' Breaks will be inserted at soft hyphen positions (if they are present in the text)
- '' if TextLayout.WrapMode Is set to WordWrap. Two properties are provided to control
- '' hyphenation:
- '' - TextLayout.SoftHyphenReplacementCharCode: specifies the character used As replacement
- '' for soft hyphen when breaking words across lines. By default this property Is 0x002D
- '' (the Unicode hyphen-minus character). Setting this property to 0 breaks words without
- '' showing any visible hyphen character. Setting it to -1 prevents breaking words at soft
- '' hyphens).
- '' - TextLayout.LinesBetweenConsecutiveHyphens: specifies the minimum number Of non-
- '' hyphenated lines between lines ending in a hyphen. By default this property Is zero.
- Public Class Hyphenation
- Function CreatePDF(ByVal stream As Stream) As Integer
- '' The online hypho-o tool
- '' was used to insert soft hyphens in the sample text from WordCharWrap:
- Dim str =
- "Lose noth­ing in your doc­u­ments! Grape­City Doc­u­ments for PDF " +
- "in­cludes text and para­graph format­ting, spe­cial char­ac­ters, " +
- "mul­tiple lan­guages, RTL sup­port, ver­tic­al and ro­tated text " +
- "on all sup­por­ted plat­forms."
- '' Replace HTML soft hyphens with Unicode ones:
- str = str.Replace("­", $"{ChrW(&HAD)}")
-
- Dim doc = New GcPdfDocument()
- Dim page = doc.NewPage()
- Dim g = page.Graphics
-
- Dim tl = g.CreateTextLayout()
- tl.Append(str)
- tl.DefaultFormat.Font = StandardFonts.Times
- tl.DefaultFormat.FontSize = 12
- tl.MaxWidth = 72 * 3
-
- '' By default 0x002D (hyphen-minus) will be used as the hyphenation character
- '' when breaking a line at a soft hyphen (0x00AD):
- tl.PerformLayout(True)
-
- Dim dy = tl.Lines(0).Height + 72 / 16
- Dim rc = New RectangleF(72, 72 + dy, tl.MaxWidth.Value, 72 * 1.4F)
-
- g.DrawString("Default hyphenation:", tl.DefaultFormat, New PointF(rc.Left, rc.Top - dy))
- g.DrawTextLayout(tl, rc.Location)
- g.DrawRectangle(rc, Color.CornflowerBlue)
-
- rc.Offset(0, 72 * 2)
- '' This will avoid hyphenating two consecutive lines:
- tl.LinesBetweenConsecutiveHyphens = 1
- '' Changing hyphenation options requires RecalculateGlyphs():
- tl.PerformLayout(True)
- g.DrawString("LinesBetweenConsecutiveHyphens: 1", tl.DefaultFormat, New PointF(rc.Left, rc.Top - dy))
- g.DrawTextLayout(tl, rc.Location)
- g.DrawRectangle(rc, Color.CornflowerBlue)
-
- rc.Offset(0, 72 * 2)
- '' Reset previous setting:
- tl.LinesBetweenConsecutiveHyphens = 0
- '' Prevent hyphenating words at all:
- tl.SoftHyphenReplacementCharCode = -1
- '' Changing hyphenation options requires RecalculateGlyphs():
- tl.PerformLayout(True)
- g.DrawString("SoftHyphenReplacementCharCode: -1", tl.DefaultFormat, New PointF(rc.Left, rc.Top - dy))
- g.DrawTextLayout(tl, rc.Location)
- g.DrawRectangle(rc, Color.CornflowerBlue)
-
- '' Done
- doc.Save(stream)
- Return doc.Pages.Count
- End Function
- End Class
-