This is a migrated thread and some comments may be shown as answers.

GridView to pdf

3 Answers 208 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Bertha
Top achievements
Rank 1
Bertha asked on 26 Sep 2018, 08:05 PM

I export the radgridview to pdf with header and footer. How to have multiple lines of header with different font size?

Telerik.WinControls.Export.GridViewPdfExport exporter = new Telerik.WinControls.Export.GridViewPdfExport(this.radGridView1);

Thanks.

3 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 28 Sep 2018, 11:31 AM
Hello, Bertha, 

GridViewPdfExport has a public API for applying a certain font for the header. You can specify left, right or middle header. Multiline header text with different fonts is not supported out of the box. However, it is possible to customize the default rendering of the header. For this purpose it is necessary to create a GridViewPdfExport and override its DrawHeader method. Note that GridViewPdfExport utilizes the powerful RadPdfProcessing library and exports RadGridView`s data natively to the PDF format. Additional information about the library and the API it offers is available in the online documentation: https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/overview

Here is the sample code snippet for showing two line header with different font: 

private void radButton1_Click(object sender, EventArgs e)
{
    string fileName = @"..\..\export" + DateTime.Now.ToLongTimeString().Replace(":", "_") + ".pdf";
    CustomGridViewPdfExport pdfExporter = new CustomGridViewPdfExport(this.radGridView1);
    pdfExporter.ShowHeaderAndFooter = true;
    pdfExporter.RunExport(fileName, new Telerik.WinControls.Export.PdfExportRenderer());
    Process.Start(fileName);
}


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();
    }
}

Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify it in a way which suits your requirement best.



I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Bertha
Top achievements
Rank 1
answered on 28 Sep 2018, 07:46 PM

Extremely helpful examples. 

I want to push the grid table to lower position so I have more room for header.  If I use the GridViewPdfExport without customize, the grid table with Header is lower than the one customize with Header.

How can I push the grid table down?

Thanks.

0
Hristo
Telerik team
answered on 01 Oct 2018, 10:38 AM
Hello Bertha,

You can push the grid down inside the exported document, by increasing the HeaderHeight property of the GridViewPdfExport object. For example, you can test like this: 
CustomGridViewPdfExport pdfExporter = new CustomGridViewPdfExport(this.radGridView1);
pdfExporter.ShowHeaderAndFooter = true;
pdfExporter.HeaderHeight = 100;

I hope this will help. Let me know if you need further assistance.

Regards,
Hristo
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
Bertha
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Bertha
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or