How to create a pdf file and display it in the viewer

I'm trying to create a pdf file in code, and then display it in the viewer. I know how to create the pdf document but I can't figure out how to display it in the viewer.

var document = new PdfDocument();
//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);
//stream.Position = 0;

var stream = new MemoryStream();

//Save the document.
document.Save(stream);
//close the document
document.Close(true);

<Syncfusion.Blazor.PdfViewer.SfPdfViewer ???></Syncfusion.Blazor.PdfViewer.SfPdfViewer>



3 Replies 1 reply marked as answer

DM Dhivyabharathi Mohan Syncfusion Team May 5, 2021 01:45 PM UTC

Hi Andy, 
 
Thank you for contacting Syncfusion support. 
 
We have shared the sample to view the created PDF document in our PDF Viewer. We have shared the code snippet for your reference. Please find the sample from the below link. 
 
Code snippet: 
 
<SfPdfViewerServer ID="pdfviewer" @ref="@PdfViewer" DocumentPath="@documentPath"> 
    <PdfViewerEvents Created="created"></PdfViewerEvents> 
</SfPdfViewerServer> 
 
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); 
        //stream.Position = 0; 
 
        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); 
    } 
 
 
 
Kindly try this and revert us, if you have any concerns. 
 
Regards, 
Dhivya. 
 
 



AR Andy Richard May 17, 2021 10:24 PM UTC

It doesn't work. The compiler complains that various components (SfTab, etc.) are defined in more than one assembly when I use SfPdfViewerServer. When I use SfPdfViewer, I can see the PDF viewer but the spinner keeps going and nothing loads, even when I open a file by clicking the folder icon on the viewer toolbar. I have attached a sample project that shows the issue.


Attachment: PdfDocumentTest_ff54efd5.zip


DM Dhivyabharathi Mohan Syncfusion Team May 18, 2021 10:13 AM UTC

Hi Andy, 
 
Please find the details, 
 
Query 
Details 
It doesn't work. The compiler complains that various components (SfTab, etc.) are defined in more than one assembly when I use SfPdfViewerServer. 
 
From the release (v18.4.0.30), the Syncfusion Blazor UI components are separately available in individual NuGet packages. The NuGet packages are segregated based on the component usage and its namespace. we suggest you use only Syncfusion.Blazor.PdfViewerServer.Windows instead of Syncfusion.Blazor NuGet packages. Please find the documentation from the below link.  
 
 
 
 
Note : Do not use both Syncfusion.Blazor and individual NuGet packages in the same application. It will throw ambiguous errors while compiling the project.  
 
 
When I use SfPdfViewer, I can see the PDF viewer but the spinner keeps going and nothing loads, even when I open a file by clicking the folder icon on the viewer toolbar. I have attached a sample project that shows the issue. 
 
 
We have checked the sample that you have used the SfPdfViewer tag. Our Blazor client-side PDF Viewer control depends on server-side processing to render the PDF files, it is mandatory to set the serviceUrl property. But you have missed the serviceUrl property. So, the issue occurs. Kindly modify the code snippet as like below. And we have shared the sample for your reference. You can create your own web service using the below KB link:  
  
  
 Blazor UG Documentation: 
 
 
Code snippet: 
 
 
<SfPdfViewer ID="pdfviewer" @ref="@pdfViewer" ServiceUrl="https://ej2services.syncfusion.com/production/web-services/api/pdfviewer" DocumentPath="@documentPath"> 
              <PdfViewerEvents Created="PdfDocumentCreatedHandler"></PdfViewerEvents> 
</SfPdfViewer> 
 
 
 
 
 
 
 
 
Kindly try this and revert us, if you have any concerns. 
 
Regards, 
Dhivya. 


Marked as answer
Loader.
Up arrow icon