I am trying to horizontally align specific cells when exporting the same as their ItemStyle in the markup. I've followed several links and have it working for Excel but PDF does not.
The ExportCellFormatting event handler does not appear to fire when exporting to PDF like it does for Excel. I believe this event is supposed to be a replacement for the deprecated ExcelExportCellFormatting event, but I would think based off the name and the deprecation of its predecessor that this should fire for PDFs as well.
ExportCellFormatting is the only event that I've found thus far where the cell's HorizontalAlign is set (off the e.FormattedColumn.ItemStyle) -- if I check the cell's HorizontalAlign in either the ItemCreated or ItemCommand event handlers it is always HorizontalAlign.NotSet. The PdfExporting event only exposes the raw HTML, which I can manipulate, but that is tedious.
I am able to get basic formatting to occur for PDF so long as it isn't related to checking something set in the markup. For example, I can detect whether the ItemType is an Item or AlternatingItem and set the inline style accordingly for alternating rows, and set the basic font, font size, and header formatting.
How can I format what's exported to PDF based on the formatted columns, specifically the horizontal alignment of individual cells? Below are code samples of what I have working so far.
Regards,
Tim
The ExportCellFormatting event handler does not appear to fire when exporting to PDF like it does for Excel. I believe this event is supposed to be a replacement for the deprecated ExcelExportCellFormatting event, but I would think based off the name and the deprecation of its predecessor that this should fire for PDFs as well.
ExportCellFormatting is the only event that I've found thus far where the cell's HorizontalAlign is set (off the e.FormattedColumn.ItemStyle) -- if I check the cell's HorizontalAlign in either the ItemCreated or ItemCommand event handlers it is always HorizontalAlign.NotSet. The PdfExporting event only exposes the raw HTML, which I can manipulate, but that is tedious.
I am able to get basic formatting to occur for PDF so long as it isn't related to checking something set in the markup. For example, I can detect whether the ItemType is an Item or AlternatingItem and set the inline style accordingly for alternating rows, and set the basic font, font size, and header formatting.
How can I format what's exported to PDF based on the formatted columns, specifically the horizontal alignment of individual cells? Below are code samples of what I have working so far.
<
jha:RadGrid
ID
=
"grdVendors"
runat
=
"server"
DataSourceID
=
"odsVendors"
OnItemDataBound
=
"grdVendors_ItemDataBound"
OnItemCommand
=
"grdVendors_ItemCommand"
OnExportCellFormatting
=
"grdVendors_ExportCellFormatting"
OnItemCreated
=
"grdVendors_ItemCreated"
OnPdfExporting
=
"grdVendors_PdfExporting"
>
<
MasterTableView
DataKeyNames
=
"VendorID"
>
<
Columns
>
<
telerik:GridBoundColumn
HeaderText
=
"ID"
DataField
=
"VendorID"
Visible
=
"false"
/>
<
telerik:GridBoundColumn
HeaderText
=
"First Name"
DataField
=
"FirstName"
/>
<
telerik:GridBoundColumn
HeaderText
=
"Last Name"
DataField
=
"LastName"
/>
<
telerik:GridBoundColumn
HeaderText
=
"City"
DataField
=
"City"
/>
<
telerik:GridBoundColumn
HeaderText
=
"State"
DataField
=
"State"
ItemStyle-HorizontalAlign
=
"Center"
/>
<
telerik:GridBoundColumn
UniqueName
=
"CreatedOn"
HeaderText
=
"Created On"
DataField
=
"CreatedDateTime"
DataFormatString
=
"{0:MM/dd/yyyy}"
ItemStyle-HorizontalAlign
=
"Right"
/>
</
Columns
>
</
MasterTableView
>
</
jha:RadGrid
>
protected
void
grdVendors_ItemCommand(
object
sender, GridCommandEventArgs e)
{
switch
(e.CommandName)
{
case
RadGrid.ExportToExcelCommandName:
case
RadGrid.ExportToPdfCommandName:
grdVendors.CssClass =
"export"
;
string
title =
"Foo"
;
GridExportSettings settings = grdVendors.ExportSettings;
settings.ExportOnlyData =
true
;
settings.OpenInNewWindow =
true
;
settings.IgnorePaging =
true
;
settings.HideStructureColumns =
true
;
settings.FileName =
string
.Format(
"{0} {1:yyyy-MM-dd}"
, title, DateTime.Now);
// PDF
settings.Pdf.DefaultFontFamily =
"Tahoma"
;
settings.Pdf.PageTitle = title;
settings.Pdf.Title = title;
settings.Pdf.AllowPrinting =
true
;
settings.Pdf.AllowAdd =
false
;
settings.Pdf.AllowCopy =
true
;
settings.Pdf.AllowModify =
false
;
settings.Pdf.PaperSize = GridPaperSize.A4;
settings.Pdf.PageHeight = Unit.Parse(
"210mm"
);
settings.Pdf.PageWidth = Unit.Parse(
"297mm"
);
settings.Pdf.PageTopMargin = Unit.Parse(
"20mm"
);
settings.Pdf.PageHeaderMargin = Unit.Parse(
"10mm"
);
settings.Pdf.PageBottomMargin = Unit.Parse(
"10mm"
);
settings.Pdf.PageLeftMargin = Unit.Parse(
"10mm"
);
settings.Pdf.PageRightMargin = Unit.Parse(
"10mm"
);
break
;
}
}
protected
void
grdVendors_ExportCellFormatting(
object
sender, ExportCellFormattingEventArgs e)
{
/// Never fires when exporting to PDF
switch
(e.FormattedColumn.ItemStyle.HorizontalAlign)
{
case
HorizontalAlign.Left:
e.Cell.Style[
"text-align"
] =
"left"
;
break
;
case
HorizontalAlign.Center:
e.Cell.Style[
"text-align"
] =
"center"
;
break
;
case
HorizontalAlign.Right:
e.Cell.Style[
"text-align"
] =
"right"
;
break
;
}
if
(e.FormattedColumn.UniqueName ==
"PostalCode"
)
e.Cell.Style[
"mso-number-format"
] = @
"\@"
;
}
protected
void
grdVendors_ItemCreated(
object
sender, GridItemEventArgs e)
{
if
(grdVendors.CssClass.Contains(
"export"
))
{
if
(e.Item
is
GridDataItem)
{
foreach
(TableCell cell
in
e.Item.Cells)
{
if
(e.Item.ItemType == GridItemType.AlternatingItem)
cell.Style[
"background-color"
] =
"#E6EFF7"
;
else
cell.Style[
"background-color"
] =
"#FFFFFF"
;
}
}
else
if
(e.Item
is
GridHeaderItem)
{
Table table = e.Item.Parent.Parent
as
Table;
table.Style[
"font-family"
] =
"Tahoma"
;
table.Style[
"font-size"
] =
"8pt"
;
foreach
(TableCell cell
in
e.Item.Cells)
{
cell.Style[
"border-color"
] =
"#FFFFFF"
;
cell.Style[
"background-color"
] =
"#EEEEEE"
;
cell.Style[
"text-align"
] =
"left"
;
cell.Style[
"color"
] =
"#274777"
;
cell.Style[
"font-weight"
] =
"bold"
;
}
}
}
}
Regards,
Tim