Print and export in Blazor Maps Component

24 Oct 20234 minutes to read

Print

The rendered Maps can be printed directly from the browser by calling the Print method. To use the print functionality, set the AllowPrint property to true.

@using Syncfusion.Blazor.Maps

<button @onclick="PrintMap">Print</button>
<SfMaps @ref="maps" AllowPrint="true">
    <MapsLayers>
        <MapsLayer ShapeData='new {dataOptions= "https://cdn.syncfusion.com/maps/map-data/world-map.json"}' TValue="string">
            <MapsLayerTooltipSettings Visible="true" ValuePath="name">
            </MapsLayerTooltipSettings>
        </MapsLayer>
    </MapsLayers>
</SfMaps>

@code {
    SfMaps maps;
    void PrintMap()
    {
        // using Maps component reference call 'Print' method
        this.maps.Print();
    }
}

Printing in Blazor Maps

Export

Image Export

To use the image export functionality, set the AllowImageExport property as true. The rendered Maps can be exported as an image using the Export method. The method requires two parameters: image type and file name. The Maps can be exported as an image in the following formats.

  • JPEG
  • PNG
  • SVG
@using Syncfusion.Blazor.Maps

<button @onclick="ExportMap">Export</button>
<SfMaps @ref="Maps" AllowImageExport="true">
    <MapsLayers>
        <MapsLayer ShapeData='new {dataOptions= "https://cdn.syncfusion.com/maps/map-data/world-map.json"}' TValue="string">
        </MapsLayer>
    </MapsLayers>
</SfMaps>

@code {
    SfMaps Maps;
    void ExportMap()
    {
        this.Maps.Export(ExportType.PNG, "Maps");
    }
}

Exporting in Blazor Maps

PDF Export

To use the PDF export functionality, set the AllowPdfExport property as true. The rendered Maps can be exported as PDF using the Export method. The Export method requires three parameters: file type, file name and orientation of the PDF document. The orientation of the PDF document can be set as 0 or 1. 0 indicates the portrait and 1 indicates landscape.

@using Syncfusion.Blazor.Maps

<button @onclick="ExportMap">Export</button>
<SfMaps @ref="Maps" AllowPdfExport="true">
    <MapsLayers>
        <MapsLayer ShapeData='new {dataOptions= "https://cdn.syncfusion.com/maps/map-data/world-map.json"}' TValue="string">
        </MapsLayer>
    </MapsLayers>
</SfMaps>

@code {
    SfMaps Maps;
    void ExportMap()
    {
        this.Maps.Export(ExportType.PDF, "Maps", 0);
    }
}

Blazor Maps with PDF Export

Exporting Maps as base64 string of the file

The image can be exported as base64 string for the JPEG, PNG and PDF formats. The rendered Maps can be exported to image as base64 string using the Export method. The arguments that are required for this method is image type, file name, orientation of the exported PDF document which must be set as null for image export and 0 or 1 for the PDF export and finally allowDownload which should be set as false to return base64 string.

@using Syncfusion.Blazor.Maps

<button @onclick="export">Export</button>
<SfMaps @ref="Maps" AllowPdfExport="true">
    <MapsLayers>
        <MapsLayer ShapeData='new {dataOptions= "https://cdn.syncfusion.com/maps/map-data/world-map.json"}' TValue="string">
        </MapsLayer>
    </MapsLayers>
</SfMaps>

@code {
    SfMaps Maps;
    string exportString;
    public async Task export()
    {
        exportString = await this.Maps.Export(ExportType.PDF, "Maps", 0, false);
        Console.WriteLine(exportString);
    }
}

NOTE

Add the below service in Program.cs file if the size of the Maps is too large.
builder.Services.AddServerSideBlazor().AddHubOptions(o => { o.MaximumReceiveMessageSize = 102400000; });

Export the tile Maps

The rendered Maps with providers such as OSM, Bing and other map providers can be exported using the Export method. It supports the following export formats.

  • JPEG
  • PNG
  • PDF
@using Syncfusion.Blazor.Maps

<button @onclick="ExportMap">Export</button>
<SfMaps @ref="Maps" AllowPdfExport="true" AllowImageExport="true">
    <MapsLayers>
        <MapsLayer UrlTemplate="https://tile.openstreetmap.org/level/tileX/tileY.png" TValue="string">
        </MapsLayer>
    </MapsLayers>
</SfMaps>

@code {
    SfMaps Maps;
    void ExportMap()
    {
        this.Maps.Export(ExportType.PNG, "OSM Map");
    }
}

Blazor Maps with OSM Export