New to Telerik UI for WinForms? Download free 30-day trial

Create custom header when exporting RadGridView to PDF

Product Version Product Author
2019.2.618 RadGridView for WinForms [Dimitar Karamfilov]

Problem

You want to have multiline text when exporting RadGridView to PDF. By default the header supports only one line.

Solution

Create a custom exporter and override the DrawHeader method. This way you can manually paint in the header area.

public class CustomGridViewPdfExport : GridViewPdfExport
{
    public CustomGridViewPdfExport(Telerik.WinControls.UI.RadGridView radGridView) : base(radGridView)
    {
    }

    protected override void DrawHeader()
    {
        System.Reflection.FieldInfo fi = typeof(GridViewPdfExport).GetField("editor", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        PdfEditor editor = fi.GetValue(this) as PdfEditor;

        if (!this.ShowHeaderAndFooter)
        {
            return;
        }

        editor.SavePosition();
        editor.CreateMatrixPosition();
        editor.TranslatePosition(this.PageMargins.Left, this.PageMargins.Top);
        editor.SaveProperties();

        //specify the font for the first line
        this.HeaderFont = new System.Drawing.Font("Arial", 14f, FontStyle.Italic);
        editor.SetTextFontSize(this.HeaderFont.Size);
        editor.TrySetFont(this.HeaderFont.Name, this.HeaderFont.Style);
        //draw the first line
        editor.DrawText("First");

        editor.TranslatePosition(this.PageMargins.Left - 20, this.PageMargins.Top);
        //specify the font for the second line
        this.HeaderFont = new System.Drawing.Font("Verdana", 10f, FontStyle.Bold);
        editor.SetTextFontSize(this.HeaderFont.Size);
        editor.TrySetFont(this.HeaderFont.Name, this.HeaderFont.Style);
        //draw the second line
        editor.DrawText("Second");

        editor.RestoreProperties();
        editor.RestorePosition();
    }
}
In this article