CharacterFormatting.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
- Imports GCTEXT = GrapeCity.Documents.Text
- Imports GCDRAW = GrapeCity.Documents.Drawing
-
- '' Demonstrates the basics of character formatting in DsPdf.
- ''
- '' Character formatting in Document Solutions for PDF is done via GrapeCity.Documents.Text.TextFormat class.
- '' An instance of that class with the required formatting options
- '' is passed to most text rendering methods available in DsPdf (e.g. DrawString).
- '' Rendering text with different character formatting in the same paragraph
- '' is done by using TextLayout/DrawTextLayout.
- '' See also TextRendering, MultiFormattedText, ParagraphAlign,
- '' ParagraphFormatting, TextAlign.
- Public Class CharacterFormatting
- Function CreatePDF(ByVal stream As Stream) As Integer
- Dim doc = New GcPdfDocument()
- Dim page = doc.NewPage()
- Dim g = page.Graphics
- Const Inch = 72.0F, vStep = Inch / 2
- Dim ip = New PointF(Inch, Inch)
-
- '' 1. The only mandatory property that must be set on a TextFormat is Font:
- Dim tf = New TextFormat() With {.Font = StandardFonts.Times}
-
- g.DrawString("1. The only mandatory property that must always be set on a TextFormat is Font." +
- "Even FontSize is optional, and defaults to 12pts.", tf, ip)
- ip.Y += vStep * 2
-
- '' 2. Standard font properties are available:
- tf.Underline = True
- tf.Strikethrough = True
- tf.FontSize = 10
- g.DrawString("2. Standard properties are available, here we turn Underline and Strikethrough on, and set FontSize to 10.", tf, ip)
- ip.Y += vStep
-
- '' 3. TextFormat.FontStyle allows emulating bold and/or italic styles
- '' using a regular font (see also BoldItalicEmulation):
- tf.Underline = tf.Strikethrough = False
- tf.FontStyle = GCTEXT.FontStyle.BoldItalic
- tf.FontSize = 12
- g.DrawString("3. Using TextFormat.FontStyle.BoldItalic to emulate bold italic style.", tf, ip)
- ip.Y += vStep
-
- '' 4. Other properties include foreground and background colors:
- tf.FontStyle = GCTEXT.FontStyle.Regular
- tf.ForeColor = Color.DarkSlateBlue
- tf.BackColor = Color.PaleGreen
- g.DrawString("4. Using TextFormat.ForeColor and TextFormat.BackColor to colorize the text.", tf, ip)
- ip.Y += vStep
-
- '' 5. Different text formats may be mixed in the same paragraph.
- '' For that, TextLayout and GcPdfGraphics.DrawTextLayout must be used:
- Dim tl = g.CreateTextLayout()
- tl.Append("5. Different text formats can be easily mixed in the same paragraph",
- New TextFormat() With {.Font = StandardFonts.Times})
- tl.Append("when the paragraph is built with TextLayout",
- New TextFormat() With {.Font = StandardFonts.TimesBold, .BackColor = Color.PaleTurquoise})
- tl.Append("as this sample paragraph shows.",
- New TextFormat() With {.Font = StandardFonts.HelveticaBoldItalic, .ForeColor = Color.DarkOrange})
- tl.Append("Various other options are available on TextFormat, including",
- New TextFormat() With {.Font = StandardFonts.Times, .ForeColor = Color.DarkSlateBlue})
- tl.Append(" GlyphAdvanceFactor ",
- New TextFormat() With {.Font = StandardFonts.TimesBoldItalic, .Underline = True})
- tl.Append(" (spreading glyphs out ",
- New TextFormat() With {.Font = StandardFonts.Times, .GlyphAdvanceFactor = 1.5F, .ForeColor = Color.BlueViolet})
- tl.Append("or putting them closer together), ",
- New TextFormat() With {.Font = StandardFonts.Times, .GlyphAdvanceFactor = 0.8F, .ForeColor = Color.BlueViolet})
- tl.Append("TransverseOffset",
- New TextFormat() With {.Font = StandardFonts.TimesBoldItalic, .Underline = True})
- tl.Append(" (lowering the glyphs below the base line, ",
- New TextFormat() With {.Font = StandardFonts.Times, .TransverseOffset = -5, .ForeColor = Color.MediumVioletRed})
- tl.Append("or raising them above it)",
- New TextFormat() With {.Font = StandardFonts.Times, .TransverseOffset = 5, .ForeColor = Color.MediumVioletRed})
- tl.Append(" and more (for example, specific fonts' features are accessible via TextFormat.FontFeatures).",
- New TextFormat() With {.Font = StandardFonts.Times, .FontFeatures = New FontFeature() {New FontFeature(FeatureTag.clig)}})
-
- '' For this sample, we just set the max width of the text layout,
- '' in a real app you would probably set at least the MaxHeight too:
- tl.MaxWidth = page.Size.Width - Inch * 2
- tl.PerformLayout(True)
- g.DrawTextLayout(tl, ip)
- ''
- '' Done:
- doc.Save(stream)
- Return doc.Pages.Count
- End Function
- End Class
-