Print in Blazor SfPdfViewer Component

18 Nov 20184 minutes to read

The SfPdfViewer supports printing the loaded PDF by default. Enable or disable the toolbar Print option by setting the EnablePrint property.

@using Syncfusion.Blazor.SfPdfViewer

<SfPdfViewer2 Height="100%"
              Width="100%"
              DocumentPath="@DocumentPath"
              EnablePrint="true" />

@code{
    public string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";
}

Print a PDF using the SfPdfViewer

@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";
}

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 the scale factor set as 1.0 (prints at the on-screen scale). Valid range from 0.5 to 5.0. Higher values can improve output clarity for documents with smaller page dimensions and may increase print processing time.

@using Syncfusion.Blazor.SfPdfViewer

<SfPdfViewer2 Height="100%"
              Width="100%"
              DocumentPath="@DocumentPath"
              PrintScaleFactor="2" />

@code{
    public string DocumentPath { get; set; } = "wwwroot/Data/PDF_Succinctly.pdf";
}

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 is completed.

PrintStart Event

The PrintStart event triggers when the print action is started.

Event Arguments

See PrintStartEventArgs for details such as Filename, Cancel option.

  • If the Cancel property is set to true in the PrintStart event, the print operation is canceled and the print dialog will not open.
  • By default, Cancel is false. Set it to true to prevent printing.

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 an annotation is added to a page in the PDF document.

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}");
    }	 
}

See also