Articles in this section
Category / Section

How to open a generated PDF in browser without saving them locally ?

6 mins read

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can open a generated PDF in a new browser tab without saving locally in the disk.

Steps to open the generated PDF in a new browser tab without saving locally:

  1. Create a new C# ASP.NET MVC application project.

Create a new mvc project

  1. Install the Syncfusion.Pdf.AspNet.MVC5 NuGet packages as a reference to your .NET Framework application from the NuGet.org.

install nuget packages

 

  1. A default controller with name HomeController.cs gets added to the creation of the ASP.NET MVC project. Include the following namespaces in the HomeController.cs file.
    // [C# Code]
    using Syncfusion.Pdf;
    using Syncfusion.Pdf.Graphics;
    

 

'[VB Code]
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics

 

  1. A default action method named Index will be present in the HomeController.cs. Right-click the Index action method and select Go To View, where you will be redirected to its associated view page Index.cshtml.

 

  1. Add a new button in the Index.cshtml as follows.

 

// [C# Code]
<input type="submit" value="Generate PDF" class="Button" id="btn1" />
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
    $('#btn1').click(function () {
       
        $.ajax({
            url: '/Home/GeneratePDF',
            type: "POST",
            data: {
                name: $("#txtName").val()
            },
            datatype: "json",
            success: function (data) {
                let pdfWindow = window.open("")
                pdfWindow.document.write("<iframe width='100%' height='100%' src='" + data + "'></iframe>")          
            }
        }); 
    });
</script>

 

  1. Add a new action method named GeneratePDF in the HomeController.cs and include the following code snippet to create a PDF file and open it in a new tab of the browser.
    // [C# Code]
    //Create a new PDF document
    PdfDocument document = new PdfDocument();
     
    //Add a page to the document
    PdfPage page = document.Pages.Add();
     
    //Create PDF graphics for the page
    PdfGraphics graphics = page.Graphics;
     
    //Set the standard font
    PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20,PdfFontStyle.Bold);
     
    //Draw the text
    graphics.DrawString("Hello World " + name, font, PdfBrushes.Black, new PointF(0, 0));
     
    MemoryStream ms = new MemoryStream();
     
    document.Save(ms);
    ms.Position = 0;
     
    //Close the document
    document.Close(true);
     
    byte[] bytes = ms.ToArray();
    return "data:application/pdf;base64," + Convert.ToBase64String(bytes);
     
    

 

 

 

 

'[VB Code]
'Create a new PDF document.
Dim document As New PdfDocument()
 
'Add a page to the document.
Dim page As PdfPage = document.Pages.Add()
 
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
 
'Set the standard font.
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 20, PdfFontStyle.Bold)
 
'Draw the text.
graphics.DrawString("Hello World " + name, font, PdfBrushes.Black, New PointF(0, 0))
 
Dim ms As New MemoryStream()
 
document.Save(ms)
ms.Position = 0
 
'Close the document.
document.Close(True)
 
Dim bytes As Byte() = ms.ToArray()
Return "data:application/pdf;base64," + Convert.ToBase64String(bytes)

 

  1.  You can download the working sample from the AjaxSample.zip.

 

  1. By executing the program, the generated PDF opened in a new tab of the browser. The output screenshot as follows.

output document

 

Take a moment to peruse the documentation, where you can find other options like working with text, image, forms, and tables with the essential PDF.

 

Refer here to explore the rich set of Syncfusion Essential PDF features.

An online sample link that explains the Essential PDF features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied