[]
        
(Showing Draft Content)

Improve Text Rendering

Text rendering using DsPdf can be done using two main approaches:

  • Using the MeasureString/DrawString methods pair

  • Using TextLayout directly

While the first approach may be easier in simple cases, the second approach (using TextLayout) is more powerful and yields better performance. TextLayout is ideal for handling both simple and complex text-rendering tasks while offering more functionality. Additionally, the MeasureString/DrawString methods already use TextLayout to render text, so using TextLayout directly cuts down on processing work when rendering text.

Refer to the following code sample to see a demonstration of TextLayout:

public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();
            var page = doc.NewPage();
            var g = page.Graphics;
            // By default, DsPdf uses 72dpi:
            const float In = 72;
 
            // TextFormat class is used throughout all DsPdf text rendering to specify
            // font and other character formatting:
            var tf = new TextFormat() { Font = StandardFonts.Times, FontSize = 12 };            
            // A much more powerful and with better performance, way to render text
            // is to use TextLayout. (TextLayout is used anyway by DrawString/MeasureString,
            // so when you use TextLayout directly, you basically cut the work in half.)
            // A TextLayout instance represents one or more paragraphs of text, with 
            // the same paragraph formatting (character formats may be different,
            // see MultiFormattedText).
            var tl = g.CreateTextLayout();
            // To add text, use Append() or AppendLine() methods:
            tl.Append("4. First test string added to TextLayout. ", tf);
            tl.Append("Second test string added to TextLayout, continuing the same paragraph. ", tf);
            // Add a line break, effectively starting a new paragraph:
            tl.AppendLine();
            tl.Append("Third test string added to TextLayout, a new paragraph. ", tf);
            tl.Append("Fourth test string, with a different char formatting. ",
                new TextFormat(tf) { Font = StandardFonts.TimesBoldItalic, ForeColor = Color.DarkSeaGreen, });
            // Text can be added to TextLayout without explicit TextFormat:
            tl.Append("Fifth test string, using the TextLayout's default format.");
            // ...but in that case at least the Font must be specified on the
            // TextLayout's DefaultFormat, otherwise PerformLayout (below) will fail:
            tl.DefaultFormat.Font = StandardFonts.TimesItalic;
            // Specify the layout, such as max available size etc.
            // Here we only provide the max width, but many more parameters can be set:
            tl.MaxWidth = page.Size.Width - In * 2;
            // Paragraph formatting can also be set, here we set first line offset,
            // spacing between paragraphs and line spacing:
            tl.FirstLineIndent = In * 0.5f;
            tl.ParagraphSpacing = In * 0.05f;
            tl.LineSpacingScaleFactor = 0.8f;
            
            tl.PerformLayout(true);
 
            // Now we can draw it on the page:
            pt = new PointF(In, In * 4);
            g.DrawTextLayout(tl, pt);
            // TextLayout provides info about the text including the measured bounds
            // and much more. Here we draw the bounding box in orange red:
            g.DrawRectangle(new RectangleF(pt, tl.ContentRectangle.Size), Color.OrangeRed);
            
            doc.Save(stream);
            return doc.Pages.Count;
        }