How can I help you?
Print in Blazor SfPdfViewer Component
12 Feb 20264 minutes to read
The SfPdfViewer component supports printing the loaded PDF by default. Enable or disable the toolbar Print option by setting the EnablePrint property. Code examples in this topic use the local component reference name shown in the snippets (for example, SfPdfViewer2); the component class is SfPdfViewer.
@using Syncfusion.Blazor.SfPdfViewer
<SfPdfViewer2 Height="100%"
Width="100%"
DocumentPath="@DocumentPath"
EnablePrint="true" />
@code{
public string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";
}
@using Syncfusion.Blazor.SfPdfViewer
@using Syncfusion.Blazor.Buttons
<SfButton OnClick="OnClick">Print</SfButton>
<SfPdfViewer2 Width="100%"
Height="100%"
DocumentPath="@DocumentPath"
@ref="@Viewer" />
@code{
SfPdfViewer2 Viewer;
public string DocumentPath { get; set; } = "wwwroot/data/PDF_Succinctly.pdf";
public async void OnClick(MouseEventArgs args)
{
await Viewer.PrintAsync();
}
}EnablePrintRotation
EnablePrintRotation controls whether landscape pages are auto-rotated to best fit when printing. Default: true. Set to false to preserve the original page orientation and suppress automatic rotation during print.
@using Syncfusion.Blazor.SfPdfViewer
<SfPdfViewer2 Height="100%"
Width="100%"
DocumentPath="@DocumentPath"
EnablePrintRotation="true" />
@code{
public string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";
}Print modes
PrintMode specifies how the print dialog is opened. Default: PrintMode.Default (prints from the same window). Supported values:
- PrintMode.Default: Opens the print dialog in the same window.
- PrintMode.NewWindow: Opens the print dialog from a new browser window/tab, which can be useful depending on browser popup policies.
@using Syncfusion.Blazor.SfPdfViewer
<SfPdfViewer2 Height="100%"
Width="100%"
DocumentPath="@DocumentPath"
PrintMode="PrintMode.NewWindow" />
@code{
public string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";
}PrintScaleFactor
PrintScaleFactor sets the scale used when rendering pages for printing. By default, PrintScaleFactor is 1.0 (prints at the on-screen scale). The valid range is 0.5 to 5.0. Increasing the scale can improve clarity for documents with small page dimensions but may increase print processing time and memory usage.
@using Syncfusion.Blazor.SfPdfViewer
<SfPdfViewer2 Height="100%"
Width="100%"
DocumentPath="@DocumentPath"
PrintScaleFactor="2" />
@code{
public string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";
}Print Events
The following events are available for print in the SfPdfViewer component.
| Name | Description |
|---|---|
| PrintStart | Triggers when a print action starts. |
| PrintEnd | Triggers when a print action completes. |
PrintStart Event
The PrintStart event triggers when a print action begins.
Event arguments
See PrintStartEventArgs for details such as FileName and the Cancel option.
- If the Cancel property is set to
truein thePrintStartevent handler, the print operation is canceled and the print dialog does not open. - By default,
Cancelisfalse.
The following example illustrates how to handle the PrintStart event.
@using Syncfusion.Blazor.SfPdfViewer
<SfPdfViewer2 DocumentPath="@DocumentPath" Height="100%" Width="100%">
<PdfViewerEvents PrintStart="@PrintStart"></PdfViewerEvents>
</SfPdfViewer2>
@code{
private string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succintly.pdf";
public async Task PrintStart(PrintStartEventArgs args)
{
Console.WriteLine($"Printed File Name: {args.FileName}");
}
}PrintEnd Event
The PrintEnd event triggers when a print action completes.
Event arguments
See PrintEndEventArgs for details such as FileName.
The following example illustrates how to handle the PrintEnd event.
@using Syncfusion.Blazor.SfPdfViewer
<SfPdfViewer2 DocumentPath="@DocumentPath" Height="100%" Width="100%">
<PdfViewerEvents PrintEnd="@PrintEnd"></PdfViewerEvents>
</SfPdfViewer2>
@code{
private string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succintly.pdf";
public async Task PrintEnd(PrintEndEventArgs args)
{
Console.WriteLine($"Printed File Name: {args.FileName}");
}
}