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

Export to pdf background colour

17 Answers 472 Views
Editor
This is a migrated thread and some comments may be shown as answers.
Jeremy
Top achievements
Rank 1
Jeremy asked on 18 Aug 2010, 09:50 AM
Hi

I am using the radeditor and put a background colour on some text. When I export to pdf it doesn't show up? Here is the html it produces:
<span style="background-color: #ffff00;">Hello World</span>

17 Answers, 1 is accepted

Sort by
0
Accepted
Rumen
Telerik team
answered on 19 Aug 2010, 02:32 PM
Hello Jeremy,

The observed behavior is a limitation of the third party export to PDF tool that RadEditor uses to export the produced XHTML content to PDF.
You can print only the background-color attribute of block elements such as <p>,<div>, etc.

<div style="background-color: #ffff00;">Hello World</div>


Best regards,
Rumen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Jeremy
Top achievements
Rank 1
answered on 20 Aug 2010, 04:19 AM
Thanks! :)
0
Chuck
Top achievements
Rank 1
Veteran
answered on 04 Oct 2018, 06:44 PM
Is this limitation still present in 2014 version?
0
Rumen
Telerik team
answered on 05 Oct 2018, 11:56 AM
Hello Chuck,

Yes, it is still limitation of the free built-in library used by RadEditor, but you can use Telerik PdfProcessing Library to export the background-color style. 
You can find an integration example in this blog post: Using an external library for the export to PDF functionality in Telerik’s ASP.NET Editor.

Best regards,
Rumen
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
Chuck
Top achievements
Rank 1
Veteran
answered on 05 Oct 2018, 12:22 PM

Thank you Rumen,

 didn't realize the "RadEditor" was the topic. I was referring to the ability to export cell background color to PDF 

https://www.telerik.com/forums/dynamically-setting-cell-background-color-on-pdf-export

0
Rumen
Telerik team
answered on 05 Oct 2018, 12:36 PM
Hello,

For RadGrid PDF export please review the following article on the matter: Appearance, i.e.

bool isExport = false;
protected void Button1_Click(object sender, EventArgs e)
{
    isExport = true;
    RadGrid1.MasterTableView.ExportToPdf();
}
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem && isExport)
        e.Item.Style["background-color"] = "#888888";
}

In the cases, where Rebind won't be invoked you can apply the styles directly:

protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.Items)
        item.Style["background-color"] = "#888888";
    RadGrid1.MasterTableView.ExportToPdf();
}


Best regards,
Rumen
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
Chuck
Top achievements
Rank 1
Veteran
answered on 05 Oct 2018, 12:44 PM

I've seen and tried the "_ItemCreated" event but for some reason the cell values are not appearing in the controls. The Text= values are empty and I can't figure out why.

           If TypeOf e.Item Is Telerik.Web.UI.GridDataItem Then
                 Dim lbTest As Label = e.Item.FindControl("lbCol1M")

                 'lbTest.Text is always empty
           End If


0
Rumen
Telerik team
answered on 05 Oct 2018, 03:04 PM
Hello,

Can you check whether the IgnorePaging and ExportOnlyData properties are not set to true?

Also try to run the logic inside the ItemDataBound and PreRender events?

Regards,
Rumen
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
Chuck
Top achievements
Rank 1
Veteran
answered on 05 Oct 2018, 06:34 PM

I set both of the Properties to False

nd tried to move the logic to set the back ground to PreRender, It already appears in the ItemDataBound and colors the grid correctly, just seems to loose it on PDF export. It exports to Excel correctly as well.

he attached image shows the PDF output on the Left and the RadGrid displayed on the right.

 

 

0
Rumen
Telerik team
answered on 09 Oct 2018, 12:17 PM
Hi Chuck,

Thank you for the screenshot.

I noticed that the grid colors are actually based on gredients (not sure whether they are CSS3 based or on images). What happens if you just manually set the background color with 

foreach (GridDataItem item in RadGrid1.Items)
        item.Style["background-color"] = "#888888";
 

or

 
    if (e.Item is GridDataItem && isExport)
        e.Item.Style["background-color"] = "#888888";
 
Does the background-color get applied to the exported PDF?

Best regards,
Rumen
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
Rumen
Telerik team
answered on 09 Oct 2018, 12:20 PM
Please also examine the solution provided in the following demo:  Export to PDF, e.g.

Protected Sub RadGrid1_ItemCreated(sender As Object, e As GridItemEventArgs)
            If RadGrid1.IsExporting Then
                FormatGridItem(e.Item)
            End If
        End Sub
  
        Protected Sub FormatGridItem(item As GridItem)
            item.Style("color") = "#eeeeee"
  
            If TypeOf item Is GridDataItem Then
                item.Style("vertical-align") = "middle"
                item.Style("text-align") = "center"
            End If
  
            Select Case item.ItemType
                'Mimic RadGrid appearance for the exported PDF file
                Case GridItemType.Item
                    item.Style("background-color") = "#4F4F4F"
                    Exit Select
                Case GridItemType.AlternatingItem
                    item.Style("background-color") = "#494949"
                    Exit Select
                Case GridItemType.Header
                    item.Style("background-color") = "#2B2B2B"
                    Exit Select
                Case GridItemType.CommandItem
                    item.Style("background-color") = "#000000"
                    Exit Select
            End Select
  
            If TypeOf item Is GridCommandItem Then
                'needed to span the image over the CommandItem cells
                item.PrepareItemStyle()
            End If
        End Sub



Best regards,
Rumen
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
Chuck
Top achievements
Rank 1
Veteran
answered on 09 Oct 2018, 12:28 PM

Thanks Rumen,

he challenge has been not so much the background coloring but having the cell value present to us in the logic to dynamically set the background coloring. I can't seem to find the cell value in the "_ItemCreated" event.

0
Rumen
Telerik team
answered on 10 Oct 2018, 03:22 PM
Hi Chuck,

Can you change the ItemCreated event with OnItemDataBound, because the ItemCreated does not provide the cell data?

Please check the following articles for more information:

 


Regards,
Rumen
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
Chuck
Top achievements
Rank 1
Veteran
answered on 10 Oct 2018, 05:08 PM

Using the OnItemDataBound event works fine for both the RadGrid and Excel export but disappears in the PDF export.

Here is the ItemCommand I'm using for the PDF Export:

Case RadGrid.ExportToPdfCommandName
    IsPDFExporting = True
 
    grdScoreCardDashboard.ExportSettings.Pdf.BorderType = GridPdfSettings.GridPdfBorderType.AllBorders
    grdScoreCardDashboard.ExportSettings.OpenInNewWindow = True
    grdScoreCardDashboard.ExportSettings.IgnorePaging = False
    grdScoreCardDashboard.ExportSettings.Pdf.PageHeight = Unit.Parse("162mm")
    grdScoreCardDashboard.ExportSettings.Pdf.PageWidth = Unit.Parse("600mm")
    grdScoreCardDashboard.ExportSettings.Pdf.PageRightMargin = Unit.Parse("10mm")
    grdScoreCardDashboard.ExportSettings.Pdf.PageLeftMargin = Unit.Parse("10mm")
    grdScoreCardDashboard.ExportSettings.Pdf.Title = "Score Card"
    grdScoreCardDashboard.ExportSettings.Pdf.PaperSize = GridPaperSize.Letter
    grdScoreCardDashboard.ExportSettings.FileName = String.Format("ScoreCardOutput-{0}", DateTime.Now.ToString("yyyyMMddhhmmss"))
    grdScoreCardDashboard.ExportSettings.Pdf.AllowPrinting = True
    grdScoreCardDashboard.ExportSettings.Pdf.AllowModify = True
    grdScoreCardDashboard.ExportSettings.Pdf.AllowCopy = True
    grdScoreCardDashboard.ExportSettings.ExportOnlyData = False
    grdScoreCardDashboard.ExportSettings.Pdf.FontType = Telerik.Web.Apoc.Render.Pdf.FontType.Embed
 
 
    For Each item As GridDataItem In grdScoreCardDashboard.Items
 
        Dim cellDisplay As Label = item.FindControl("lblRegion")
        If Not IsNothing(cellDisplay) Then
            If cellDisplay.Text.StartsWith("Plant") Then
                cellDisplay.Style.Add("padding-left", "20px")
 
                LoadAttributes(item.FindControl("lbCol9M"), 10, cellDisplay.Text)
            Else
 
                item.Cells(item.ItemIndex).Style("background-color") = LoadAttributes(item.FindControl("lbCol1M"), 1, cellDisplay.Text)
                LoadAttributes(item.FindControl("lbCol1YTD"), 14, cellDisplay.Text)
 
                item.PrepareItemStyle()
                item.PrepareItemVisibility()
 
            End If
        End If
 
    Next
 
 
 
    'If column.UniqueName.Equals("lbCol2M") And Not DirectCast(sender, Telerik.Web.UI.RadGrid).Items(intRowIdx).KeyValues.Contains("Plant ") Then
    '    e.Cell.Style("background-color") = LoadAttributesGray(e.Cell.Parent.FindControl("lbCol2M"), 17)
    'End If
    'If column.UniqueName.Equals("lbCol2YTD") Then
    '    e.Cell.Style("background-color") = LoadAttributes(e.Cell.Parent.FindControl("lbCol2YTD"), 18, strLocation)
    'End If
 
    'If column.UniqueName.Equals("lbCol3M") Then
    '    e.Cell.Style("background-color") = LoadAttributes(e.Cell.Parent.FindControl("lbCol3M"), 4, strLocation)
    'End If
    'If column.UniqueName.Equals("lbCol3YTD") Then
    '    e.Cell.Style("background-color") = LoadAttributes(e.Cell.Parent.FindControl("lbCol3YTD"), 5, strLocation)
    'End If
 
    'If column.UniqueName.Equals("lbCol4M") Then
 
    '    e.Cell.Style("background-color") = LoadAttributes(e.Cell.Parent.FindControl("lbCol4M"), 8, strLocation)
    'End If
    'If column.UniqueName.Equals("lbCol4YTD") Then
    '    e.Cell.Style("background-color") = LoadAttributes(e.Cell.Parent.FindControl("lbCol4YTD"), 9, strLocation)
    'End If
 
    'If column.UniqueName.Equals("lbCol5M") And Not DirectCast(sender, Telerik.Web.UI.RadGrid).Items(intRowIdx).KeyValues.Contains("Plant ") Then
    '    e.Cell.Style("background-color") = LoadAttributesGray(e.Cell.Parent.FindControl("lbCol5M"), 19)
    'End If
    'If column.UniqueName.Equals("lbCol5YTD") Then
    '    e.Cell.Style("background-color") = LoadAttributes(e.Cell.Parent.FindControl("lbCol5YTD"), 20, strLocation)
    'End If
 
    'If column.UniqueName.Equals("lbCol6M") Then '
    '    e.Cell.Style("background-color") = LoadAttributes(e.Cell.Parent.FindControl("lbCol6M"), 21, strLocation)
    'End If
    'If column.UniqueName.Equals("lbCol6YTD") Then
    '    e.Cell.Style("background-color") = LoadAttributes(e.Cell.Parent.FindControl("lbCol6YTD"), 22, strLocation)
    'End If
 
 
    ''Period Cost
    'If column.UniqueName.Equals("lbCol7M") Then
    '    e.Cell.Style("background-color") = LoadAttributes(e.Cell.Parent.FindControl("lbCol7M"), 23, strLocation)
    'End If
 
    'If column.UniqueName.Equals("lbCol7YTD") Then
    '    e.Cell.Style("background-color") = LoadAttributes(e.Cell.Parent.FindControl("lbCol7YTD"), 24, strLocation)
    'End If
 
    'If column.UniqueName.Equals("lbCol8M") Then
    '    e.Cell.Style("background-color") = LoadAttributes(e.Cell.Parent.FindControl("lbCol8M"), 27, strLocation)
    'End If
    'If column.UniqueName.Equals("lbCol8YTD") Then
    '    e.Cell.Style("background-color") = LoadAttributes(e.Cell.Parent.FindControl("lbCol8YTD"), 28, strLocation)
    'End If
 
    'If column.UniqueName.Equals("lbCol9M") Then
    '    e.Cell.Style("background-color") = LoadAttributes(e.Cell.Parent.FindControl("lbCol9M"), 10, strLocation)
    'End If
    'If column.UniqueName.Equals("lbCol9YTD") Then
    '    e.Cell.Style("background-color") = LoadAttributes(e.Cell.Parent.FindControl("lbCol9YTD"), 11, strLocation)
    'End If
 
    'If column.UniqueName.Equals("lbCol10M") Then
    '    e.Cell.Style("background-color") = LoadAttributes(e.Cell.Parent.FindControl("lbCol10M"), 12, strLocation)
    'End If
 
    'If column.UniqueName.Equals("lbCol10YTD") Then
    '    e.Cell.Style("background-color") = LoadAttributes(e.Cell.Parent.FindControl("lbCol10YTD"), 13, strLocation)
    'End If
 
 
    'End If
 
 
 
    'Next
 
    grdScoreCardDashboard.MasterTableView.ExportToPdf()
0
Accepted
Attila Antal
Telerik team
answered on 15 Oct 2018, 05:32 PM
Hi Chuck,

Every time there is a need to apply logical formatting based on data that is bound to an item, we recommend using the ItemDataBound event. Please note that the ItemDataBound event will not fire every time there is a PostBack (including the export to PDF command), however, if IgnorePaging is called, it will then force rebind itself causing the ItemDataBound event to fire.

For example, in the ItemCommand event you can identify the type of export and apply different rules if necessary.
Protected Sub RadGrid1_ItemCommand(sender As Object, e As GridCommandEventArgs)
    If e.CommandName = RadGrid.ExportToPdfCommandName Then
        RadGrid1.ExportSettings.IgnorePaging = True
    End If
End Sub

ItemDataBound event handler
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If RadGrid1.IsExporting Then
        If TypeOf e.Item Is GridDataItem Then
            Dim dataItem As GridDataItem = TryCast(e.Item, GridDataItem)
            If CInt(dataItem("OrderID").Text) Mod 2 = 0 Then
                dataItem.Style("background-color") = "#edda85"
                dataItem.Style("color") = "#4F4F4F"
            Else
                dataItem.Style("background-color") = "#4F4F4F"
                dataItem.Style("color") = "#edda85"
            End If
        End If
    End If
End Sub

Or, you can use the PreRender event to Loop through the items
Protected Sub RadGrid1_PreRender(sender As Object, e As EventArgs)
    If RadGrid1.IsExporting Then
        For Each dataItem As GridDataItem In RadGrid1.Items
            If CInt(dataItem("OrderID").Text) Mod 2 = 0 Then
                dataItem.Style("background-color") = "#edda85"
                dataItem.Style("color") = "#4F4F4F"
            Else
                dataItem.Style("background-color") = "#4F4F4F"
                dataItem.Style("color") = "#edda85"
            End If
        Next
    End If
End Sub

I hope this will help resolve the issue.

Kind regards,
Attila Antal
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
Chuck
Top achievements
Rank 1
Veteran
answered on 17 Oct 2018, 12:24 PM
Thank you for clearing the confusion as I didn't think I had to reset the coloring on the export thru the ItemDataBound. It's working fine now.
0
Attila Antal
Telerik team
answered on 17 Oct 2018, 12:40 PM
Hi Chuck,

I am glad the suggestions have helped. With that said, I will mark this thread as resolved.

Kind regards,
Attila Antal
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
Editor
Asked by
Jeremy
Top achievements
Rank 1
Answers by
Rumen
Telerik team
Jeremy
Top achievements
Rank 1
Chuck
Top achievements
Rank 1
Veteran
Attila Antal
Telerik team
Share this question
or