RadPrintDocument - Print from RadGridView and RadRichTextEditor in a single page

1 Answer 202 Views
GridView
Arun
Top achievements
Rank 2
Iron
Arun asked on 29 Jun 2021, 04:59 AM

Hi

Please Help

Language: VB.Net (VS2017)

Telerik Version: 2017.3.1017

I have a RadGridView and two RadRichTextEditors in a form. In the printout, is it possible to display the texts from RadRichTextEditors just after the RadGridView printed? I mean, it should print on Report Footer. I don't have Telerik Reports License, so using RadPrintDocument. I am able to print Customized Headers & Footers with GridView data. Sample document attached.

Thanks in advance.

Arun

1 Answer, 1 is accepted

Sort by
0
Accepted
Todor
Telerik team
answered on 01 Jul 2021, 09:17 AM

Hi, Arun,

First, you can export the grid using GridViewPdfExport class. Below the RadGridView rows, you need to export some additional data - the content of two different RadRichTextEditor controls. To achieve this you can subscribe to the CellPaint event of GridViewSpreadExport and when the last cell of the grid is painted you can add this additional information. The code sample below illustrates how to calculate the available space between the last row of the grid and the footer and how to manually draw the content of the two RadRichTextEditor controls.

GridViewPdfExport export;
private void radButtonExport_Click(object sender, EventArgs e)
{
    this.export = new GridViewPdfExport(this.radGridView1);
    this.export.ExportVisualSettings = false;
    this.export.CellPaint += this.Export_CellPaint;
    this.export.ShowHeaderAndFooter = true;
    this.export.MiddleHeader = "Header";
    this.export.MiddleFooter = "Footer";

    PdfExportRenderer renderer = new PdfExportRenderer();
    this.export.RunExport(@"../../Exported/ExportedFile.pdf", renderer);
}

private void Export_CellPaint(object sender, ExportCellPaintEventArgs e)
{
    if (e.Cell.RowIndex == this.radGridView1.RowCount - 1 &&
        e.Cell.ColumnIndex == this.radGridView1.ColumnCount - 1)
    {
        // Draw the content of the two RichTextEditors after the Last cell is painted

        // Step 1 is to get available rectangle between the exported rows and the footer
        Padding pageMarginsPx = GridExportUtils.ConvertMmToDipPadding(this.export.PageMargins);
        SizeF pageSizePx = GridExportUtils.ConvertMmToDipSize(this.export.PageSize);

        int richTextEditorMargin = 10;
        RectangleF availableRect = new RectangleF();
        availableRect.X = pageMarginsPx.Left;
        availableRect.Y = e.Rectangle.Bottom + richTextEditorMargin;
        availableRect.Width = pageSizePx.Width - pageMarginsPx.Horizontal;
        availableRect.Height = pageSizePx.Height - availableRect.Y - (float)this.export.FooterHeight - pageMarginsPx.Bottom;

        // Step 2 is to split the rectangle into left and right parts, for the two RichTextEditors contents.
        float rectWidth = (availableRect.Width - richTextEditorMargin) / 2;
        RectangleF leftRect = new RectangleF(availableRect.Location, availableRect.Size);
        leftRect.Width = rectWidth;

        RectangleF rightRect = new RectangleF(availableRect.Location, availableRect.Size);
        rightRect.X = leftRect.Right + richTextEditorMargin;
        rightRect.Width = rectWidth;

        // Step 3 is to draw the content manually using the FixedContentEditor
        // Left RichTextEditor Rectangle
        e.Editor.CreateMatrixPosition();
        e.Editor.TranslatePosition(leftRect.X, leftRect.Y);
        e.Editor.DrawRectangle(Point.Empty, new PointF(leftRect.Width, leftRect.Height));

        // Left RichTextEditor Content
        FixedContentEditor fixedContentEditor = e.Editor as FixedContentEditor;
        fixedContentEditor.SaveGraphicProperties();
        fixedContentEditor.GraphicProperties.FillColor = new RgbColor(0, 0, 0);
        fixedContentEditor.DrawText(this.radRichTextEditor1.Text, new System.Windows.Size(leftRect.Width, leftRect.Height));
        fixedContentEditor.RestoreGraphicProperties();

        // Right RichTextEditor Rectangle
        e.Editor.CreateMatrixPosition();
        e.Editor.TranslatePosition(rightRect.X, rightRect.Y);
        e.Editor.DrawRectangle(Point.Empty, new PointF(rightRect.Width, rightRect.Height));

        // Right RichTextEditor Content
        fixedContentEditor.SaveGraphicProperties();
        fixedContentEditor.GraphicProperties.FillColor = new RgbColor(0, 0, 0);
        fixedContentEditor.DrawText(this.radRichTextEditor2.Text, new System.Windows.Size(rightRect.Width, rightRect.Height));
        fixedContentEditor.RestoreGraphicProperties();
    }
}

If you have more complex formatting on the rich text documents, you can use the Control.DrawToBitmap method and draw a bitmap of the RichTextEditor controls. FixedContentEditor class allows you to draw anything you want on the PDF document. For more information about it visit our online documentation: https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/fixedcontenteditor

Here is the result on my side:

I hope this helps. If you have other questions please let me know.

Regards,
Todor Vyagov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Arun
Top achievements
Rank 2
Iron
commented on 06 Jul 2021, 05:00 AM

Hi, I have tested the code in VB.Net and it's working as expected. Thank you.
Tags
GridView
Asked by
Arun
Top achievements
Rank 2
Iron
Answers by
Todor
Telerik team
Share this question
or