blazor-devexpress-dot-blazor-dot-griddocumentexportoptions-dda19646.md
Allows you to customize style and page settings for the exported document.
Namespace : DevExpress.Blazor
Assembly : DevExpress.Blazor.v25.2.dll
NuGet Package : DevExpress.Blazor
public Action<GridDocumentExportCustomizeDocumentEventArgs> CustomizeDocument { get; set; }
| Type | Description |
|---|---|
| Action<GridDocumentExportCustomizeDocumentEventArgs> |
A delegate method that customizes style and page settings.
|
You can access this nested property as listed below:
| Object Type | Path to CustomizeDocument |
|---|---|
| GridExportEventArgs |
.DocumentOptions .CustomizeDocument
|
When exporting Grid data to PDF, handle the CustomizeDocument event to configure style and page settings for the output document:
@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() {
CustomizeDocument = OnCustomizeDocument, // Customizes exported document appearance
CustomizePageHeader = args => args.Text = "Weather Forecast", // Adds a page header
CustomizePageFooter = args => args.Text = "Page {0} of {1}", // Adds a page footer
});
}
void OnCustomizeDocument(GridDocumentExportCustomizeDocumentEventArgs args) {
// Sets default font settings for all elements in the exported document
args.DefaultElementStyle.Font = new DXFont("Times New Roman", 12, DXFontStyle.Regular);
// Switches the page orientation to landscape
args.Landscape = true;
// Sets page margins to 0.5 inches
args.Margins = new DXMargins(50, 50, 50, 50);
// Sets the page size to a custom value (width: 6 inches, height: 8 inches)
args.PaperKind = DevExpress.Drawing.Printing.DXPaperKind.Custom;
args.PageSize = new System.Drawing.Size(600, 800);
}
}
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", "Stormy"
};
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>();
Refer to the following topic for additional information: Export Blazor Grid Data to PDF.
See Also
GridDocumentExportOptions Class