Skip to main content

Lesson 2 - Draw Simple Text

  • 3 minutes to read

This lesson shows you how to create a link that generates a document displaying the “Hello World!” text.

Tip

A complete sample project is available in the DevExpress Code Examples database at https://supportcenter.devexpress.com/ticket/details/e161/reporting-get-started-with-printing-library.

To add text to a document via a printing link, do the following.

  1. To create a new printing link, declare a new class (called Lesson2) derived from the Lesson1 class defined in the previous lesson. The new class will have three constants: top (determines the top position), r (specifies the rectangle) and caption (contains the “Hello World!” text string).

    This link overrides the Link.BeforeCreate protected method, to set the background color, border color, font, and line alignment for the BrickGraphics class. The overridden Link.CreateDetail method is used to draw a string whose text is predefined in the caption constant.

    using System.Drawing;
    using DevExpress.XtraPrinting;
    // ...
    
    public class Lesson2 : Lesson1 {
        internal int top = 0;
        internal Rectangle r = new Rectangle(0, 0, 150, 50);
        internal string caption = "Hello World!";
    
        public Lesson2(PrintingSystem ps) : base(ps) {}
    
        protected override void BeforeCreate() {
            base.BeforeCreate();
            if (this.PrintingSystem != null) {
                BrickGraphics g = this.PrintingSystem.Graph;
    
                // Set the background color to White.
                g.BackColor = Color.White;
    
                // Set the border color to Black.
                g.BorderColor = Color.Black;
    
                // Set the font to the default font.
                g.Font = g.DefaultFont;
    
                // Set the line alignment.
                g.StringFormat = g.StringFormat.ChangeLineAlignment(StringAlignment.Near);
            }
        }
    
        // Add a text brick without borders containing the "Hello World!" text.
        protected override void CreateDetail(BrickGraphics graph) {
            graph.DrawString(caption, Color.Black, r, BorderSide.None);
        }
    }
    
  2. Next, modify the main form’s Load event handler to pass the Printing System of the Document Viewer to the new link.

    using System;
    using System.Windows.Forms;
    using DevExpress.XtraPrinting;
    // ...
    
    private void Form1_Load(object sender, System.EventArgs e) {
        Lesson2 lesson = new Lesson2(printingSystem1);
    }
    

Launch the application and view the result.

document-viewer-ribbon-link-document-result

See Also