Syncfusion AI Assistant

How can I help you?

Preview the newly created PDF file

14 Oct 20252 minutes to read

Use the Syncfusion® Blazor PDF Viewer to display a PDF that is generated at runtime when the viewer initializes using the Created event. The sample below targets the SfPdfViewer component and binds the generated document to the DocumentPath property via a data URI.

The following example demonstrates generating a PDF in memory and rendering it as soon as the viewer is created.

<SfPdfViewer2 ID="pdfviewer" 
              @ref="@PdfViewer" 
              DocumentPath="@documentPath"
              Height="100%"
              Width="100%">
    <PdfViewerEvents Created="created"></PdfViewerEvents>
</SfPdfViewer2>

@code{

    public SfPdfViewer2 PdfViewer { get; set; }
    public string documentPath { get; set; }
    private void created()
    {
        var document = new PdfDocument();
        byte[] bytes;
        //Add a new page to the PDF document.
        PdfPage page = document.Pages.Add();
        //Create a textbox field and add the properties.
        PdfTextBoxField textBoxField = new PdfTextBoxField(page, "FirstName");
        textBoxField.Bounds = new RectangleF(0, 0, 100, 20);
        textBoxField.ToolTip = "First Name";
        //Add the form field to the document.
        document.Form.Fields.Add(textBoxField);
        var stream = new MemoryStream();
        //Save the document.
        document.Save(stream);
        bytes = stream.ToArray();
        string base64string = Convert.ToBase64String(bytes);
        documentPath = "data:application/pdf;base64," + base64string;
        //close the document
        document.Close(true);
    }
}

View sample in GitHub

See also