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

Export to PDF

4 Answers 65 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ranjith
Top achievements
Rank 1
Ranjith asked on 01 Jan 2014, 09:58 AM
Hi,

I am trying to export PDF from my RadGrid and its exporting successfully. But i am facing some design issues in the exported PDF file.

1. All the text are coming with Hyperlinks in the exported PDF.
2. Set the Forecolor and width, which are not getting reflected in the exported PDF.

Below is my c# code 

protected void btnExportToPDF_Click(object sender, ImageClickEventArgs e)
        {
            ApplyStylesToPDFExport(RadGrid1.MasterTableView);
            RadGrid1.ExportSettings.OpenInNewWindow = true;
            RadGrid1.ExportSettings.IgnorePaging = true;
            RadGrid1.ExportSettings.FileName = "Test";
            RadGrid1.MasterTableView.ExportToPdf();
        }
 
private void ApplyStylesToPDFExport(GridTableView view)
        {
            GridItem headerItem = view.GetItems(GridItemType.Header)[0];
            foreach (TableCell cell in headerItem.Cells)
            {
        switch (cell.Text)
                {
                    case "A":
                        cell.Width = Unit.Pixel(5); // Tried like this. Not reflecting
                        break;
                    case "B":
                        cell.Style["width"] = "15px"; // Tried like this. Not reflecting
                        break;
        }
                cell.Style["font-family"] = "Verdana";
                cell.Style["font-bold"] = "true";
                cell.Style["text-align"] = "left";
                cell.Style["vertical-align"] = "middle";
                cell.Style["font-size"] = "8px";
                cell.ForeColor = System.Drawing.Color.Black; // Tried like this. Not reflecting
 
            }
            GridItem[] dataItems = view.GetItems(GridItemType.Item);
            foreach (GridItem item in dataItems)
            {
                foreach (TableCell cell in item.Cells)
                {
                    cell.Style["font-family"] = "Verdana";
                    cell.Style["text-align"] = "left";
                    cell.Style["vertical-align"] = "left";
                    cell.Style["font-size"] = "6px";
                    cell.Style["text-decoration"] = "none"; // Tried like this. Not reflecting
 
                    cell.Style["ForeColor"] = "#000";
                }
            }
            dataItems = view.GetItems(GridItemType.AlternatingItem);
            foreach (GridItem item in dataItems)
            {
                foreach (TableCell cell in item.Cells)
                {
                    cell.Style["font-family"] = "Verdana";
                    cell.Style["text-align"] = "left";
                    cell.Style["vertical-align"] = "middle";
                    cell.Style["font-size"] = "6px";
                    cell.Style["text-decoration"] = "none";
                    cell.ForeColor = System.Drawing.Color.Black;
                }
            }
        }

Please advise.

Thanks
Ranjith

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 02 Jan 2014, 04:26 AM
Hi Ranjith,

Please use the ItemCreated event to call the function ApplyStylesToPDFExport(). Please try the code as follows:

C#:
bool IsExport = false;
protected void PDFbtn_Click(object sender, EventArgs e)
{
    IsExport = true;
    RadGrid1.MasterTableView.GetColumn("ColumnUniqueName").Visible = false; //To hide a column during export
    . . . .
    RadGrid1.MasterTableView.ExportToPdf();
}
 
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (IsExport)
    {
        ApplyStylesToPDFExport(e.Item);
    }
}
private void ApplyStylesToPDFExport(GridItem item)
{
    if (item is GridHeaderItem)
        foreach (TableCell cell in item.Cells)
        {
            cell.Style["font-family"] = "Verdana";
            cell.Style["text-align"] = "left";
            cell.Style["font-size"] = "6pt";
            cell.ForeColor = System.Drawing.Color.Blue;
        }
    if (item is GridDataItem)
    {
        item.Style["font-size"] = "6px";
        item.ForeColor = item.ItemType == GridItemType.AlternatingItem ? Color.Red : Color.BlueViolet;           
    }
}

Hope this helps.Please try and let me know if any concerns.
Thanks,
Princy

0
Ranjith
Top achievements
Rank 1
answered on 02 Jan 2014, 05:37 AM
Hi Princy,

Thanks for your help.

I tried the same, still its not reflecting in the exported PDF. I am attaching the screen shot for your reference how the exported PDF look likes.

Thanks
Ranjith 
0
Princy
Top achievements
Rank 2
answered on 03 Jan 2014, 04:25 AM
Hi Ranjith,

I'm not able to replicate the issue, below is a sample code snippet that i tried, please try and see if the issue exists. Please provide your aspx code to further look into this issue.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="true" OnNeedDataSource="RadGrid1_NeedDataSource"
    ShowFooter="true" ShowHeader="true" OnItemCreated="RadGrid1_ItemCreated">
    <MasterTableView CommandItemDisplay="Top">
    </MasterTableView>
</telerik:RadGrid>
<asp:Button ID="PDFbtn" runat="server" Text="Export To PDF" OnClick="PDFbtn_Click" />

C#:
bool IsExport = false;
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    DateTime dt = DateTime.Now;
 
    dynamic data = new[] {
            new { ID = 1, Name ="Name_1", customdate=dt,NO = 11.00,CustomerId="ALFKI34",Stock=20,IsSysAdmin = true},
            new { ID = 2, Name = "Name2", customdate=dt,NO = 11.00,CustomerId="ALFKI2",Stock=30,IsSysAdmin = false},
            new { ID = 3, Name = "Name3", customdate=dt,NO = 11.00,CustomerId="ALFKI1",Stock=20,IsSysAdmin = true},
            new { ID = 4, Name = "Name4", customdate=dt,NO = 11.44,CustomerId="ALFKI6",Stock=40,IsSysAdmin = true},
            new { ID = 5, Name = "Name5", customdate=dt,NO = 10.4,CustomerId="ALFKI7",Stock=20,IsSysAdmin = true}
        };
 
    RadGrid1.DataSource = data;
}
 
protected void PDFbtn_Click(object sender, EventArgs e)
{
    IsExport = true;
    RadGrid1.ExportSettings.OpenInNewWindow = true;
    RadGrid1.ExportSettings.IgnorePaging = true;
    RadGrid1.ExportSettings.FileName = "Test";
    RadGrid1.MasterTableView.ExportToPdf();
}
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (IsExport)       
        ApplyStylesToPDFExport(e.Item);
         
}
private void ApplyStylesToPDFExport(GridItem item)
{
    if (item is GridHeaderItem)
        foreach (TableCell cell in item.Cells)
        {
            cell.Style["font-family"] = "Verdana";
            cell.Style["text-align"] = "left";
            cell.Style["font-size"] = "6pt";
            cell.ForeColor = System.Drawing.Color.Blue;
        }
    if (item is GridDataItem)
    {
        item.Style["font-size"] = "6px";
        item.ForeColor = item.ItemType == GridItemType.AlternatingItem ? Color.Red : Color.BlueViolet;
    }
}

Thanks,
Princy
0
Ranjith
Top achievements
Rank 1
answered on 09 Jan 2014, 09:48 AM
Hi Princy,

Thanks for your help. I found what's my issue.
Hyperlink will come if we are using TemplateColumn. I changed to GridBoundColumn.

Thanks & Regards
Ranjith
Tags
Grid
Asked by
Ranjith
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Ranjith
Top achievements
Rank 1
Share this question
or