17 Answers, 1 is accepted
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
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
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
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
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
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
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.
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"
;
Best regards,
Rumen
Progress Telerik
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
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.
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:
- Accessing Raw Field Data and Key Values
- GridTableView Class Members - Client-side alternative.
Regards,
Rumen
Progress Telerik
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()
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
I am glad the suggestions have helped. With that said, I will mark this thread as resolved.
Kind regards,
Attila Antal
Progress Telerik