DsPdf provides the following two main approaches to render text through GcGraphics class which is a member of GrapeCity.Documents.Drawing namespace:
In addition, DsPdf offers GrapeCity.Documents.Text namespace which supports the following features to work with text:
To render text in a PDF document using DsPdf, you can either use DrawString method provided by the GcGraphics class or the TextLayout class. In case you are using the TextLayout class, you need to create a layout using Append method, and then prepare it for rendering by calling the PerformLayout method. Finally, render the text in the document by calling the DrawTextLayout method provided by the GcGraphics class.
C# |
Copy Code
|
---|---|
public void CreatePDF(Stream stream) { var doc = new GcPdfDocument(); var page = doc.NewPage(); var g = page.Graphics; const float In = 150; PointF ip = new PointF(72, 72); var tl = g.CreateTextLayout(); tl.DefaultFormat.Font = StandardFonts.Times; tl.DefaultFormat.FontSize = 12; // TextFormat class is used throughout all DsPdf text rendering to specify // font and other character formatting: var tf = new TextFormat(tl.DefaultFormat) { Font = StandardFonts.Times, FontSize = 12 }; // Render text using Append method tl.Append("Turpis non ante pulvinar et massa nibh laoreet amet volutpat laoreet " + "molestie aliquet massa ullamcorper ac nisi ante massa lobortis Massa at laoreet" + "mauris aliquamfelis feugiat et non euismod magna eget molestie euismod elit dolor " + "eget erat euismod laoreetPharetra sit mauris nibh molestie ac nunc proin felis" + " erat lorem volutpat elit mi nunc magnamauris molestie tincidunt" + " sedMassa congue nibh volutpat eget non", tf); tl.PerformLayout(true); g.DrawTextLayout(tl, ip); // Render text using DrawString method g.DrawString("1. Test string.", tf, new PointF(In, In)); // Save document doc.Save(stream); } |
For more information about rendering text using DsPdf, see DsPdf sample browser.
To set the text alignment in a PDF document, use TextAlignment property provided by the TextLayout class. This property accepts value from TextAlignment enum.
C# |
Copy Code
|
---|---|
tl.TextAlignment = TextAlignment.Trailing; |
To format text in a PDF document, use the TextFormat class. This class is used for any type of text rendering, to specify the font and other character formatting. You can use different properties, such as FontSize, FontStyle, etc. provided by the TextFormat class to apply required text format to the rendered text.
C# |
Copy Code
|
---|---|
TextFormat tf = new TextFormat()
{
Font = StandardFonts.Courier,
FontSize = 14,
FontStyle = FontStyle.Bold,
ForeColor = GrapeCity.Documents.Drawing.Color.Cyan,
Language = Language.English
};
|
To rotate text at various angles in a PDF document:
C# |
Copy Code
|
---|---|
public void CreatePDF(Stream stream) { // Rotation angle, degrees clockwise float angle = -45; var doc = new GcPdfDocument(); var g = doc.NewPage().Graphics; // Create a text layout, pick a font and font size: TextLayout tl = g.CreateTextLayout(); tl.DefaultFormat.Font = StandardFonts.Times; tl.DefaultFormat.FontSize = 24; // Add a text, and perform layout: tl.Append("Rotated text."); tl.PerformLayout(true); // Text insertion point at (1",1"): var ip = new PointF(72, 72); // Now that we have text size, create text rectangle var rect = new RectangleF(ip.X, ip.Y, tl.ContentWidth, tl.ContentHeight); // Rotate the text around its bounding rect's center: // we now have the text size, and can rotate it about its center: g.Transform = Matrix3x2.CreateRotation((float)(angle * Math.PI) / 180f, new Vector2(ip.X + tl.ContentWidth / 2, ip.Y + tl.ContentHeight / 2)); // Draw rotated text and bounding rectangle: g.DrawTextLayout(tl, ip); g.DrawRectangle(rect, Color.Black, 1); // Remove rotation and draw the bounding rectangle g.Transform = Matrix3x2.Identity; g.DrawRectangle(rect, Color.ForestGreen, 1); // Save Document doc.Save(stream); } |
DsPdf supports vertical text through FlowDirection property of the GcGraphics class which accepts value from the FlowDirection enumeration. To set the vertical text alignment, this property needs to be set to VerticalLeftToRight or VerticalRightToLeft.
Additionally, the TextFormat class of DsPdf provides you an option to rotate the sideways text in counter clockwise direction using the RotateSidewaysCounterclockwise property. Further, SidewaysInVerticalText and UprightInVerticalText property of the TextFormat class also provides options to display the text sideways or upright respectively. These properties are especially useful for rendering Latin text within the East-Asian language text.
C# |
Copy Code
|
---|---|
// Set vertical text layout using TextLayout properties tl.RotateSidewaysCounterclockwise = true; tl.FlowDirection = FlowDirection.VerticalLeftToRight; // Setup the vertical text layout for Chinese, Japanese and Korean characters TextFormat tfvertical = new TextFormat() { UprightInVerticalText = false, GlyphWidths = GlyphWidths.Default, TextRunAsCluster = false, }; tl.Append("日本語でのテスト文字列です", tfvertical); |
To render an outline text, draw the outline using the StrokePen property, and then set the Hollow property to true. And, in case of fill text, use the FillBrush property provided by the TextFormat class.
C# |
Copy Code
|
---|---|
// Outline Text TextFormat tf0 = new TextFormat() { StrokePen = Color.DarkGreen, Hollow = true, FontSize = 48, }; tl.AppendLine("Outline Text", tf0); // Filled Text TextFormat tf1 = new TextFormat() { StrokePen = Color.DarkMagenta, FillBrush = new SolidBrush(Color.Yellow), FontSize = 48, }; tl.AppendLine("Filled Text", tf1); |
There are two ways of handling the text that does not fit into the available space; one is to wrap the text and other is to trim a character or a word and append it with a character such as ellipsis. To wrap the text in a PDFdocument, use the WrapMode property provided by the TextLayout class. This class also provides the TrimmingGranularity and EllipsisCharCode properties to set the trimming options and to display a particular character at the end of the text respectively.
C# |
Copy Code
|
---|---|
// Character trimming tl.TrimmingGranularity = TrimmingGranularity.Character; tl.EllipsisCharCode = 0x007E; // Set wrap mode to character wrap tl.WrapMode = WrapMode.CharWrap; |
To render subscript and superscript text in a PDF document, use the Subscript and Superscript properties provided by the TextFormat class.
C# |
Copy Code
|
---|---|
//Apply Subscript var tf = new TextFormat() {FontSize = 18}; var tfsub = new TextFormat() {Subscript = true, FontBold = true}; var tfbold = new TextFormat() {FontBold = true, FontSize = 18}; tl.Append("The chemical formula of water is "); tl.Append("H", tfbold); tl.Append("2", tfsub); tl.Append("O.", tfbold); //Apply Superscript var tf = new TextFormat() {FontSize = 18}; var tfsup = new TextFormat() {Superscript = true, FontBold = true}; tl.Append("Example of a math equation : "); tl.Append("x", tf); tl.Append("2", tfsup); tl.Append("+", tf); tl.Append("y", tf); tl.Append("2", tfsup); |
To handle paragraph formatting, use the properties provided by TextLayout class to set the paragraph alignment and formatting.
C# |
Copy Code
|
---|---|
public void CreatePDF(Stream stream) { var doc = new GcPdfDocument(); var page = doc.NewPage(); var g = page.Graphics; // By default, DsPdf uses 72dpi: PointF ip = new PointF(72, 72); var tl = g.CreateTextLayout(); tl.MaxWidth = doc.PageSize.Width; tl.MaxHeight = doc.PageSize.Height; tl.MarginLeft = tl.MarginTop = tl.MarginRight = tl.MarginBottom = 72; var tf = new TextFormat(tl.DefaultFormat) { Font = StandardFonts.Times, FontSize = 12, }; // Render text using Append method tl.Append("Turpis non ante pulvinar et massa nibh laoreet amet volutpat laoreet " + "molestie aliquet massa ullamcorper ac nisi ante massa lobortis Massa at laoreet" + "mauris aliquamfelis feugiat et non euismod magna eget molestie euismod elit dolor" + "eget erat euismod laoreetPharetra sit mauris nibh molestie ac nunc proin felis" + "erat lorem volutpat elit mi nunc magnamauris molestie tincidunt" + "sedMassa congue nibh volutpat eget non", tf); // Set first line offset tl.FirstLineIndent = 72 / 2; // Set line spacing tl.LineSpacingScaleFactor = 1.5f; tl.PerformLayout(true); g.DrawTextLayout(tl, ip); // Save document doc.Save(stream); } |
For more information about extracting table data from PDF documents by using DsPdf, see DsPdf sample browser.