blazor-devexpress-dot-blazor-dot-documentexportelementstyle.md
Specifies element font settings.
Namespace : DevExpress.Blazor
Assembly : DevExpress.Blazor.v25.2.dll
NuGet Package : DevExpress.Blazor
public DXFont Font { get; set; }
| Type | Description |
|---|---|
| DXFont |
Font settings.
|
The PDF export engine applies different formatting to document elements for visual clarity. For instance, it applies the bold font attribute to column captions and colors them gray. The image below displays standard document formatting:
You can use PDF export options to customize document appearance. Style options are applied in the following order:
Style settings that you assign to the DefaultElementStyle property in the CustomizeDocument event handler. These settings affect all elements: table cells, document/page headers and footers.
The export engine’s style settings.
Style settings that you assign to the DataCellStyle property in the CustomizeColumn event handler. These settings affect all data cells in the corresponding column.
Style settings that you assign to the ElementStyle property in the following event handlers:
Note
Styles applied later have higher priority. For instance, export engine styles override DefaultElementStyle settings.
Specify Font and ForeColor properties to customize an element’s font.
The following example customizes exported document appearance:
@rendermode InteractiveServer
@using DevExpress.Drawing;
@inject WeatherForecastService ForecastService
<DxGrid @ref="Grid" Data="@forecasts">
<Columns>
<DxGridDataColumn Caption="Date" FieldName="Date" />
<DxGridDataColumn Caption="Temperature (C)" FieldName="TemperatureC" />
<DxGridDataColumn Caption="Temperature (F)" FieldName="TemperatureF" />
<DxGridDataColumn Caption="Summary" FieldName="Summary" />
</Columns>
<ToolbarTemplate>
<DxToolbar>
<DxToolbarItem Text="Export to PDF" Click="ExportPdf_Click" BeginGroup="true" />
</DxToolbar>
</ToolbarTemplate>
</DxGrid>
@code {
IGrid Grid;
private WeatherForecast[]? forecasts;
protected override async Task OnInitializedAsync() {
forecasts = await ForecastService.GetForecastAsync(DateOnly.FromDateTime(DateTime.Now));
}
async Task ExportPdf_Click() {
await Grid.ExportToPdfAsync("ExportResult", new GridPdfExportOptions() {
CustomizeCell = OnCustomizeCell, // Customizes table cell appearance
CustomizeDocument = OnCustomizeDocument, // Customizes overall document appearance
CustomizeDocumentHeader = OnCustomizeDocumentHeader, // Adds and customizes a document header
});
}
void OnCustomizeDocument(GridDocumentExportCustomizeDocumentEventArgs args) {
// Sets default font settings for all elements
args.DefaultElementStyle.Font = new DXFont("Times New Roman", 14, DXFontStyle.Regular);
}
void OnCustomizeCell(GridDocumentExportCustomizeCellEventArgs args) {
// Sets border settings for all table cells
args.ElementStyle.BorderColor = System.Drawing.Color.MediumBlue;
args.ElementStyle.BorderWidth = 3;
args.ElementStyle.BorderDashStyle = DevExpress.XtraPrinting.BorderDashStyle.Dash;
// Sets style settings for header cells
if(args.AreaType == DocumentExportAreaType.Header) {
args.ElementStyle.BackColor = System.Drawing.Color.DimGray;
args.ElementStyle.BorderSides = DevExpress.XtraPrinting.BorderSide.None;
args.ElementStyle.ForeColor = System.Drawing.Color.White;
}
args.Handled = true;
}
void OnCustomizeDocumentHeader(GridDocumentExportCustomizeDocumentHeaderFooterEventArgs args) {
// Adds a document header
args.Text = "Weather Forecast";
// Sets header style settings
args.ElementStyle.Font = new DXFont("Arial", 16, DXFontStyle.Bold);
args.ElementStyle.Padding = new DevExpress.XtraPrinting.PaddingInfo(0, 0, 0, 0);
args.ElementStyle.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
}
}
using System;
public class WeatherForecast {
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public string Forecast { get; set; }
public string CloudCover { get; set; }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
public class WeatherForecastService {
private List<WeatherForecast> Forecast { get; set; }
private static string[] CloudCover = new[] {
"Sunny", "Partly cloudy", "Cloudy", "Storm"
};
Tuple<int, string>[] ConditionsForForecast = new Tuple<int, string>[] {
Tuple.Create( 22 , "Hot"),
Tuple.Create( 13 , "Warm"),
Tuple.Create( 0 , "Cold"),
Tuple.Create( -10 , "Freezing")
};
public WeatherForecastService() {
Forecast = CreateForecast();
}
private List<WeatherForecast> CreateForecast() {
var rng = new Random();
DateTime startDate = DateTime.Now;
return Enumerable.Range(1, 30).Select(index => {
var temperatureC = rng.Next(-10, 30);
return new WeatherForecast {
Date = startDate.AddDays(index),
TemperatureC = temperatureC,
CloudCover = CloudCover[rng.Next(0, 4)],
Forecast = ConditionsForForecast.First(c => c.Item1 <= temperatureC).Item2
};
}).ToList();
}
public List<WeatherForecast> GetForecast() {
return Forecast;
}
public Task<WeatherForecast[]> GetForecastAsync(CancellationToken ct = default) {
return Task.FromResult(Forecast.ToArray());
}
}
// ...
builder.Services.AddSingleton<WeatherForecastService>();
The following example customizes exported document appearance:
@rendermode InteractiveServer
@using DevExpress.Drawing
@inject SpaceObjectDataProvider SpaceObjectDataProvider
<DxTreeList @ref="TreeList" Data="TreeListData" ChildrenFieldName="Satellites">
<Columns>
<DxTreeListDataColumn FieldName="Name" />
<DxTreeListDataColumn FieldName="TypeOfObject" Caption="Type" />
<DxTreeListDataColumn FieldName="Mass10pow21kg" Caption="Mass, kg" DisplayFormat="N2" />
<DxTreeListDataColumn FieldName="MeanRadiusInKM" Caption="Radius, km" DisplayFormat="N2"/>
</Columns>
<ToolbarTemplate>
<DxToolbar>
<DxToolbarItem Text="Export to PDF" Click="ExportPdf_Click" BeginGroup="true" />
</DxToolbar>
</ToolbarTemplate>
</DxTreeList>
@code {
ITreeList TreeList { get; set; }
object TreeListData { get; set; }
protected override async Task OnInitializedAsync() {
TreeListData = SpaceObjectDataProvider.GenerateData();
}
async Task ExportPdf_Click() {
await TreeList.ExportToPdfAsync("ExportResult", new TreeListPdfExportOptions() {
CustomizeCell = OnCustomizeCell, // Customizes table cell appearance
CustomizeDocument = OnCustomizeDocument, // Customizes overall document appearance
CustomizeDocumentHeader = OnCustomizeDocumentHeader, // Adds and customizes a document header
});
}
void OnCustomizeDocument(TreeListDocumentExportCustomizeDocumentEventArgs args) {
// Sets default font settings for all elements
args.DefaultElementStyle.Font = new DXFont("Times New Roman", 14, DXFontStyle.Regular);
}
void OnCustomizeCell(TreeListDocumentExportCustomizeCellEventArgs args) {
// Sets border settings for all table cells
args.ElementStyle.BorderColor = System.Drawing.Color.MediumBlue;
args.ElementStyle.BorderWidth = 3;
args.ElementStyle.BorderDashStyle = DevExpress.XtraPrinting.BorderDashStyle.Dash;
// Sets style settings for header cells
if (args.AreaType == DocumentExportAreaType.Header) {
args.ElementStyle.BackColor = System.Drawing.Color.DimGray;
args.ElementStyle.BorderSides = DevExpress.XtraPrinting.BorderSide.None;
args.ElementStyle.ForeColor = System.Drawing.Color.White;
}
args.Handled = true;
}
void OnCustomizeDocumentHeader(TreeListDocumentExportCustomizeDocumentHeaderFooterEventArgs args) {
// Adds a document header
args.Text = "Space Objects";
// Sets header style settings
args.ElementStyle.Font = new DXFont("Arial", 16, DXFontStyle.Bold);
args.ElementStyle.Padding = new DevExpress.XtraPrinting.PaddingInfo(0, 0, 0, 0);
args.ElementStyle.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
}
}
using System.Collections.Generic;
public class SpaceObject {
public string Name { get; set; }
public double MeanRadiusInKM { get; set; }
public double Volume10pow9KM3 { get; set; }
public double Mass10pow21kg { get; set; }
public double Density { get; set; }
public double SurfaceGravity { get; set; }
public string TypeOfObject { get; set; }
public List<SpaceObject> Satellites { get; set; }
public SpaceObject(
string name,
double meanRadiusInKM,
double volume10pow9KM3,
double mass10pow21kg,
double density,
double surfaceGravity,
string typeOfObject,
List<SpaceObject> satellites = null
) {
Name = name;
MeanRadiusInKM = meanRadiusInKM;
Volume10pow9KM3 = volume10pow9KM3;
Mass10pow21kg = mass10pow21kg;
Density = density;
SurfaceGravity = surfaceGravity;
TypeOfObject = typeOfObject;
Satellites = satellites ?? new List<SpaceObject>();
}
}
public class SpaceObjectDataProvider {
public List<SpaceObject> GenerateData() {
return new List<SpaceObject>() {
new SpaceObject("Sun", 696000, 1412000000, 1989100000, 1.409, 274.0, "Star", new List<SpaceObject>() {
new SpaceObject("Mercury", 2439.7, 60.83, 330.2, 5.43, 3.7, "Planet"),
new SpaceObject("Venus", 6051.8, 928.43, 4868.5, 5.24, 8.872, "Planet"),
new SpaceObject("Earth", 6371.0, 1083.21, 5973.6, 5.515, 9.78033, "Planet", new List<SpaceObject> () {
new SpaceObject("Moon", 1737.1, 21.958, 73.5, 3.3464, 1.625, "Satellite")
}),
new SpaceObject("Mars", 3390.0, 163.18, 641.85, 3.94, 3.7, "Planet"),
new SpaceObject("Jupiter", 69911, 1431280, 1898600, 1.33, 24.79, "Planet", new List<SpaceObject>() {
new SpaceObject("Ganymede", 2631.2, 76.30, 148.2, 1.936, 1.428, "Satellite"),
new SpaceObject("Callisto", 2410.3, 58.65, 107.6, 1.83, 1.23603, "Satellite"),
new SpaceObject("Io", 1821.5, 25.32, 89.3, 3.528, 1.797, "Satellite"),
new SpaceObject("Europa", 1561, 15.93, 48, 3.01, 1.316, "Satellite"),
}),
new SpaceObject("Saturn", 58232, 827130, 568460, 0.70, 10.445, "Planet", new List<SpaceObject>() {
new SpaceObject("Titan", 2576, 71.52, 134.5, 1.88, 1.354, "Satellite"),
new SpaceObject("Rhea", 764.4, 1.87, 2.3166, 1.23, 0.26, "Satellite"),
new SpaceObject("Iapetus", 736, 1.55, 1.9739, 1.08, 0.223, "Satellite"),
new SpaceObject("Dione", 561.6, 0.73, 1.096, 1.48, 0.232, "Satellite"),
new SpaceObject("Tethys", 533, 0.624, 0.6173, 1.15, 0.145, "Satellite"),
new SpaceObject("Enceladus", 252.1, 0.067, 0.108, 1.61, 0.111, "Satellite"),
new SpaceObject("Mimas", 198.3, 0.033, 0.03749, 1.15, 0.06363, "Satellite")
}),
new SpaceObject("Uranus", 25362, 68340, 86832, 1.30, 8.87, "Planet", new List<SpaceObject>() {
new SpaceObject("Titania", 788.9, 2.06, 3.526, 1.72, 0.378, "Satellite"),
new SpaceObject("Oberon", 761.4, 1.85, 3.014, 1.63, 0.347, "Satellite"),
new SpaceObject("Umbriel", 584.7, 0.84, 1.2, 1.4, 0.234, "Satellite"),
new SpaceObject("Ariel", 578.9, 0.81, 1.35, 1.67, 0.269, "Satellite"),
new SpaceObject("Miranda", 235.8, 0.055, 0.0659, 1.20, 0.07910375, "Satellite"),
}),
new SpaceObject("Neptune", 24622, 62540, 102430, 1.76, 11.15, "Planet", new List<SpaceObject>() {
new SpaceObject("Triton", 1353.4, 10.38, 21.5, 2.061, 0.782, "Satellite"),
new SpaceObject("Proteus", 210, 0.038, 0.050, 1.3, 0.0666, "Satellite"),
}),
new SpaceObject("Eris", 1170, 7, 16.7, 2.25, 0.662, "Dwarf planet"),
new SpaceObject("Pluto", 1153, 7.15, 13.105, 2.0, 0.61, "Dwarf planet"),
new SpaceObject("Makemake", 710, 1.8, 3, 2.0, 0.4, "Dwarf planet"),
new SpaceObject("Haumea", 575, 1.3, 4.006, 3, 0.44, "Dwarf planet"),
new SpaceObject("Ceres", 475, 0.437, 0.95, 2.08, 0.27, "Dwarf planet"),
new SpaceObject("Pallas", 266, 0.078, 0.211, 2.8, 0.2, "Asteroid"),
new SpaceObject("Vesta", 264.6, 0.078, 0.262, 3.42, 0.251, "Asteroid")
})
};
}
}
// ...
builder.Services.AddSingleton<SpaceObjectDataProvider>();
See Also
DocumentExportElementStyle Class